Is there a suggested pattern for using LINQ between the Model & DataAccess Layers in a DDD based Layered Architecture

我的梦境 提交于 2019-12-02 20:55:59
Uffe

From the code provided in your example I guess you are not using a generic repository pattern?

I use EF CodeFirst (but it works for old EF to) with a generic repository pattern... http://average-uffe.blogspot.com/2011/03/repository-pattern-with-ef-code-first.html

I do not have the Expression<Func<DomainEntities.Customer, bool>> in that post, but I always have a Find metod in the IRepository<T> interface.

The interface:

IEnumerable<T> Find(Expression<Func<T, bool>> expression, int maxHits = 100);

And the implementation in the abstract baserepository:

public virtual IEnumerable<T> Find(Expression<Func<T, bool>> expression, int maxHits = 100) {
    return this.DataContext.DbSet<T>().Where(expression).Take(maxHits);
}

And now you can call Find on any entity by a lambda expression...

I can post a full example if you do not get it right, just say when.

In such case you must implement your own custom convertor of one expression tree to another one (probably more then one) which will fully involve your mapping logic. Generally your expression is at the moment just specification (specification pattern) and you must transform that specification to store expression.

Btw. it is wrong. There should not be separate data access layer objects - data access layer should load and save your domain objects directly but EF is not fully capable to do it correctly because its mapping functionality is limited and it pushes its own requirements to entities. You should check NHibernate or other ORM if you want to do DDD seriously (by book).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!