Let's say I have this POCO:
public class Shelf : Entity //Entity is a base-class that holds the ID-property and related things
{
public string Name { get; private set; }
private ICollection<Book> _books;
public virtual ICollection<Book> Books
{
get
{
return _books;
}
private set
{
_books = value;
}
}
public void AddBook(Book book)
{
_books.Add(book);
}
//More code follows....
}Now, the problem is that the consumer of this class can grab the collection of books at any time and add & delete books at will, bypassing my accessor-methods (Add, Remove...)
I have tried to do this:
get
{
return _books.ToList();
}However, when doing this, leaving the set-accessor as-is, it isn't compatible with Entity Framework it seems (the collection gets pushed to the DB, but is left NULL when loading the data back in).
I've read about alternative ways to work around this, however, as far as I understand, the ways I've found (but not tried) involve using reflection, and this isn't available for my ASP.NET MVC application where it will be hosted. Another thing I'm considering is adding another layer of abstraction made up of DTOs, but I would prefer to avoid that.
Any and all suggestions how to solve my problem are welcome! 