在模型和模型之间是否有使用LINQ的建议模式?基于DDD的分层体系结构中的DataAccess层 [英] Is there a suggested pattern for using LINQ between the Model & DataAccess Layers in a DDD based Layered Architecture

查看:117
本文介绍了在模型和模型之间是否有使用LINQ的建议模式?基于DDD的分层体系结构中的DataAccess层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Tim McCarthy的 .NET中有关DDD的出色书籍.不过,在他的示例应用程序中,他的底层数据访问使用SqlCE,并且他正在手工内联SQL.

我一直在研究一些利用Entity Framework的模式,但是我一直在坚持如何将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)... awesome book on DDD in .NET. In his example application though, his underlying data access is using SqlCE and he's handcrafting the SQL inline.

I've been playing with some patterns for leveraging Entity Framework but I've gotten stuck on how exactly to map the IRepository linq queries to the underlying data access layer.

I have a concrete repository implementation called.

public EFCustomerRepository : IRepository<DomainEntities.Customer> 
{
    IEnumerable<DomainEntities.Customer> GetAll(
                     Expression<Func<DomainEntities.Customer, bool>> predicate)
    {
        //Code to access the EF Datacontext goes here...
    }
}

In my EF Model, I'm using POCO Entities but even so there's going to be no native mapping between my DomainEntity.Customer & my DataAccessLayer.Customer objects.

so I can't just pass Expression<Func<DomainEntities.Customer, bool>> predicate as the parameter for an EFContext.Customers.Where(...);

Is there an easy way to map an Expression<Func<T, bool>> predicate => Expression<Func<TOTHER, bool>> predicate

Or am I going about this all wrong ? Any suggestions / pointers appreciated.

解决方案

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.

这篇关于在模型和模型之间是否有使用LINQ的建议模式?基于DDD的分层体系结构中的DataAccess层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆