Why does the entity framework need an ICollection for lazy loading?

Posted by Akk on Stack Overflow See other posts from Stack Overflow or by Akk
Published on 2010-05-19T15:21:55Z Indexed on 2010/05/19 16:50 UTC
Read the original article Hit count: 234

I want to write a rich domain class such as

public class Product    
{    
   public IEnumerable<Photo> Photos {get; private set;}    
   public void AddPhoto(){...}    
   public void RemovePhoto(){...}
 }

But the entity framework (V4 code first approach) requires an ICollection type for lazy loading! The above code no longer works as designed since clients can bypass the AddPhoto / RemovePhoto method and directly call the add method on ICollection. This is not good.

public class Product    
{    
   public ICollection<Photo> Photos {get; private set;} //Bad    
   public void AddPhoto(){...}    
   public void RemovePhoto(){...}    
 }

It's getting really frustrating trying to implement DDD with the EF4. Why did they choose the ICollection for lazy loading?

How can i overcome this? Does NHibernate offer me a better DDD experience?

© Stack Overflow or respective owner

Related posts about ddd

Related posts about entity-framework