以下代码是否违反了Persistence Ignorance规则 [英] Does the following code violate Persistence Ignorance rule

查看:343
本文介绍了以下代码是否违反了Persistence Ignorance规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a) 域实体不应包含与持久性相关的代码,因此它们应该是 PI 。但假设域 DM 是使用实体框架设计的,假设服务层执行CRUD操作 POCO 域实体通过Linq-to-Entities ,我们是否会认为服务层可以直接或通过<模型:

  class CustomerService 
{
public string doSomething(...)
{
...
var customer = context.Customers.Where(...);
...
}
...
}

b)在 DM 中是否使用 违反 PI 规则?例如,以下客户实体违反PI:

  class Customer 
{
public string InterestedWhatOtherCustomerOrdered(...)
{
...
var orders = context.Orders.Where(...); //这是否违反了PI规则?
...
}
...
}

回答卢克·麦格雷戈:



a)

$ b $是的,它直接指的是上下文。一个更好的方法是
使用客户端的内部导航属性来执行
相同的操作,


因此,导航属性应该联系上下文?但是,由于导航属性也驻留在域模型中,我们是否可以通过直接联系上下文来争辩,他们也会违反 PI ? b)根据Fowler的数据映射器的PEAA章节,可以从数据映射器中提取任何方法域代码所需的接口类,然后可以使用哪个域代码。使用 EF 而不是手写的数据映射器,这样做不能完全如此,我们不会违反 PI



谢谢

解决方案

是的,它是指上下文直。更好的方法是使用客户端的内部导航属性来执行相同的操作。

  class Customer 
{
public string InterestedWhatOtherCustomerOrdered(...)
{
...
var orders = this.Orders;
...
}
...
}

或为此函数创建单独的查询类。


a) Domain entities shouldn't contain code related to persistence, thus they should be Persistence Ignorant PI. But assuming Domain Model DM is designed using Entity Framework and assuming that service layer performs CRUD operation for POCO domain entities via Linq-to-Entities, would we argue that service layer accesses DAL directly or via Domain Model:

class CustomerService
{
       public string doSomething( ... )
       {
                ...
                var customer = context.Customers.Where( ... );
                ...
        }
       ...
}

b) Does using Linq-to-Entities within DM violate PI rule? For example, does the following Customer entity violate PI:

class Customer
{
       public string InterestedWhatOtherCustomerOrdered( ... )
       {
                ...
                var orders = context.Orders.Where( ... ); // does this violate PI rule?
                ...
        }
       ...
}

REPLY TO Luke McGregor:

a)

Yeah it does as it refers to the context directly. A better way would be to use the internal navigation properties on Customer to perform the same action,

So instead navigation properties should contact the context?! But since navigation properties also reside within domain model, couldn't we then argue that by contacting the context directly, they too would violate PI?

b) According to Fowler's PEAA chapter on Data Mapper, it is ok to extract from Data Mapper any methods needed by the domain code into an interface class, which domain code can then use. How exactly could that be done when using EF instead of hand written Data Mapper, in such a manner that we wouldn't violate PI?

thank you

解决方案

Yeah it does as it refers to the context directly. A better way would be to use the internal navigation properties on Customer to perform the same action,

class Customer
{
       public string InterestedWhatOtherCustomerOrdered( ... )
       {
                ...
                var orders = this.Orders;
                ...
        }
       ...
}

or to create a separate query class for this function.

这篇关于以下代码是否违反了Persistence Ignorance规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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