Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've been reading Tim McCarthy's 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.

like image 768
Eoin Campbell Avatar asked Aug 10 '11 13:08

Eoin Campbell


1 Answers

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).

like image 160
Ladislav Mrnka Avatar answered Sep 29 '22 07:09

Ladislav Mrnka