I am trying to decide on an ORM tool for my project, and I am thinking about EF4.
What are the major (if any) annoyances/limitations with this product? Also, does it support caching?
Thanks
I am trying to decide on an ORM tool for my project, and I am thinking about EF4.
What are the major (if any) annoyances/limitations with this product? Also, does it support caching?
Thanks
这是一个很好的起点。 当然,他是 NHibernate 的主要贡献者之一,因此特定的帖子可能看起来有点偏颇,但评论中有一些很好的链接和论据。
看起来有人在几个月前就SO提出了一个非常相似的问题。
无法使用私有支持字段来延迟加载集合。 例如上这个课:
public class Account
{
private IList<Customer> _customers = new List<Customer>();
public IEnumerable<Customer> Customers
{
get { return _customers ; }
}
public void AddCustomer(Customer customer)
{
//Perform some biz rules
_customers.Add(customer)
}
}
通过使用 IEnumerable 集合并在类上具有 AddCustomer / RemoveCustomer 方法来限制对客户集合的访问。通常,您希望在添加或删除新客户之前进行一些业务检查。
当前版本的 EF 要求延迟加载集合类型为 ICollection(或任何实现 ICollection 的类型)。所以上面的类现在看起来像:
public class Account
{
private IList<Customer> _customers = new List<Customer>();
public virtual ICollection<Customer> Customers
{
get { return _customers ; }
}
public void AddCustomer(Customer customer)
{
//Perform some biz rules
_customers.Add(customer)
}
}
使用公共 ICollection 客户完全破坏了良好的 OO 设计主体,因为消费者可以通过调用 ICollection 上的添加操作直接访问。
Account acc = new Account();
acc.Customers.Add(new Customer()); //Bad code
域类的初衷是使用公共的add方法:
Account acc = new Account();
acc.AddCustomer(new Customer());
NHibernate 可以通过配置来处理这种情况。我非常希望看到 EF 支持这种方案。
但是请注意,通过将支持字段声明为受保护并通过配置对其进行映射,可以解决此限制:
public class Account
{
protected virtual ICollection<Customer> _customers = new Collection<Customer>();
public IEnumerable<Customer> Customers
{
get { return _customers ; }
}
public void AddCustomer(Customer customer)
{
//Perform some biz rules
_customers.Add(customer)
}
}
但是,如果您的应用程序具有分层架构(即您的域模型与 EF 配置类分离),这将不起作用,因为外部类无法访问受保护的类型。
为了使其工作,需要 EF 类与您的域模型在同一个程序集中!
延迟加载集合需要实现 ICollection 的公共或受保护类型。