简体   繁体   English

实体框架映射到域对象

[英]Entity Framework Mapping to Domain Object

I have a rich domain, so I want to separate the domain model from the persistence model, which is recommended from all Domain Driven Design practitioners. 我有一个丰富的域,因此我想将域模型与持久性模型分开,所有领域驱动设计从业人员都建议这样做。 Suppose i have a following rich domain model: 假设我有以下富域模型:

public class Order
{
    Public Guid Id {get; private set;}
    Public Property1 {get; private set;}
    Public Property2 {get; private set;}

    private List<Item>  Items;
    Public AddItem() { }
    ....
}

This is my Data Transfer Obect (Entity Framework is using it): 这是我的数据传输对象(实体框架正在使用它):

public class OrderDTO 
{
    public Guid Id {get; set;}
    Public Property1 {get; set;}
    Public Property2 {get; set;}
    ...
}

Entity Framework DbContext: 实体框架DbContext:

public partial class EntityFrameworkContext : DbContext
{
    ...
    public virtual DbSet<OrderDTO> Orders{ get; set; }
    ...
}

Repository: 仓库:

public class OrderRepository : IOrderRepository 
{
    private EntityFrameworkContext _context;
    ....
    public IQueryable<Order> FindBy(Expression<Func<Order, bool>> predicate)
    {
        //how to implement this ??
    }
}

How can i implement the Method FindBy ?? 我如何实现FindBy方法? I cannot use _context.Orders.where(predicate) because entitiy framwork context works with OrderDTO and not with Order. 我不能使用_context.Orders.where(predicate),因为实体框架上下文适用于OrderDTO,而不适用于Order。 I need somehow a mapping between Func<Order, bool> to Func<OrderDTO, bool> . 我需要以某种方式在Func<Order, bool>Func<OrderDTO, bool>之间的映射。

Any help would be appriciated. 任何帮助将被申请。

What if you use OrderDTO to build the predicate expression 如果使用OrderDTO构建谓词表达式该怎么办

public class OrderRepository : IOrderRepository 
{
    private EntityFrameworkContext _context;
    ....
    public IQueryable<Order> FindBy(Expression<Func<OrderDTO, bool>> predicate)
    {
        return ConvertToDomainModel(_context.Orders.where(predicate);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM