我一直在读蒂姆·麦卡锡的书关于 .NET 中的 DDD 的精彩书籍 http://www.amazon.co.uk/NET-Domain-Driven-Design-Solution-Programmer/dp/0470147563。但在他的示例应用程序中,他的底层数据访问使用 SqlCE,并且他正在手工制作 SQL 内联。
我一直在尝试一些利用实体框架的模式,但我一直困惑于如何准确地将 IRepository linq 查询映射到底层数据访问层。
我有一个名为的具体存储库实现。
public EFCustomerRepository : IRepository<DomainEntities.Customer>
{
IEnumerable<DomainEntities.Customer> GetAll(
Expression<Func<DomainEntities.Customer, bool>> predicate)
{
//Code to access the EF Datacontext goes here...
}
}
在我的 EF 模型中,我使用 POCO 实体,但即便如此,我的 DomainEntity.Customer 和 DataAccessLayer.Customer 对象之间也不会存在本机映射。
所以我不能就这么过去Expression<Func<DomainEntities.Customer, bool>> predicate
作为参数EFContext.Customers.Where(...);
有没有一种简单的方法来绘制地图Expression<Func<T, bool>> predicate
=> Expression<Func<TOTHER, bool>> predicate
还是我的做法全错了?
任何建议/指示表示赞赏。
从您的示例中提供的代码来看,我猜您没有使用通用存储库模式?
我使用 EF CodeFirst(但它适用于旧的 EF)和通用存储库模式...http://average-uffe.blogspot.com/2011/03/repository-pattern-with-ef-code-first.html http://average-uffe.blogspot.com/2011/03/repository-pattern-with-ef-code-first.html
我没有Expression<Func<DomainEntities.Customer, bool>>
在那篇文章中,但我总是有一个 Find 方法IRepository<T>
界面。
界面:
IEnumerable<T> Find(Expression<Func<T, bool>> expression, int maxHits = 100);
以及抽象baserepository中的实现:
public virtual IEnumerable<T> Find(Expression<Func<T, bool>> expression, int maxHits = 100) {
return this.DataContext.DbSet<T>().Where(expression).Take(maxHits);
}
现在您可以通过 lambda 表达式在任何实体上调用 Find...
如果您不明白,我可以发布一个完整的示例,只需说明时间即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)