I want to create my own collection which is inherited from ReadOnlyCollection in order to encapsulate my domain model logic in it.
public class Product : Entity
{
public string Name { get; set; }
private readonly List<ProductAttachment> _attachments =
new List<ProductAttachment>();
public Attachment<ProductAttachment> Attachments => new Attachment<ProductAttachment>(_attachments);
}
public class Attachment<TFileEntity> : ReadOnlyCollection<TFileEntity> where TFileEntity : FileEntity<int>
{
public void AddAttachment(TFileEntity fileEntity)
{
Items.Add(fileEntity);
}
public Attachment(IList<TFileEntity> list) : base(list)
{
}
}
Next, I try to add an attachment
foreach (var attachment in request.Attachments)
{
product.Attachments.AddAttachment(new ProductAttachment(attachment.FullName, attachment.Content))
}
And after I try to add the object via _context.Products.Add(product) or get, I get an error
No coercion operator is defined between types 'System.Collections.Generic.List1[Domain.Entities.ProductAttachment]' and 'Domain.Common.Attachment1[Domain.Entities.ProductAttachment]'.
How can I solve this problem? I would like to end up with a custom collection where the logic is encapsulated. Maybe I'm doing something wrong?