>“产品”很显然不应该关联“订单”的
我没有说“产品”要关联“订单”。而是在对产品进行删除的类里,必须要对订单进行查询,才能知道是否有订单引用到要删除的产品,这是客观存在的双向关联,难道这是“需求分析”的错误吗?现在的目的就是为了避免模块间的这种双向关联。
例如(不好意思,下面是用C的代码,意思大家应该能明白吧):
产品模块:
public class Production
{
}
public class ProductionRepository
{
public static int Delete(Production prod)
{
if(OrderRepository.QueryOrderByProduction(prod).Count > 0)
{
//....提示不可以删除
}
//...执行删除
}
}
订单模块:
public class Order
{
public IList OrderItem;
}
public class OrderItem
{
public Production Prod;
public decimal Quantity;
}
public class OrderRepository
{
public IList QueryOrderByProduction(Production prod)
{
// ...查询订购prod的所有订单
}
}
大家可以看出,产品模块和订单模块,应该是订单模块依赖于产品模块,但是为了要判断是否有订单引用到要删除的产品,则在ProductionRepository.Delete里必须去调用OrderRepository。那么我现在的一种解决办法是:
产品模块:
public class ProductionRepository
{
public static event EventHandle BeforeDelete;
public static int Delete(Production prod)
{
if(BeforeDelete != null)
{
BeforeDelete(this, new ProductionDeleteArgs(prod));
}
}
}
public class ProductionDeleteArgs : EventArgs
{
private Production m_prod;
public ProductionDeleteArgs(Production prod)
{
m_prod = prod;
}
}
订单模块:
public class OrderRepository
{
public OrderRepository()
{
ProductionRepository.BeforeDelete += new EventHandle(this.OnBeforeDeleteProduction);
}
public IList QueryOrderByProduction(Production prod)
{
// ...查询订购prod的所有订单
}
public void OnBeforeDeleteProduction(object sender, EventArgs e)
{
if(OrderRepository.QueryOrderByProduction(prod).Count > 0)
{
//....提示不可以删除
}
}
}
上面用到了C里的委托的技术,实际上就是一个观察者模式的做法。通过BeforeDelete这个事件委托,在ProductionRepository.Delete里,只需要判断是否有存在删除前的事件,如果有则执行。而OrderRepository则将OnBeforeDeleteProduction注册到BeforeDelete上。这样实现了在删除产品里对订单的引用进行判断。同样的,在系统增加其它的模块的时候,可以采用同样的机制来增加产品删除前的引用判断。而在产品模块就不需反复变更代码。
用“产品模块”和“订单模块”是我想表达我的问题的一个简单例子,并不是我实际工作中的需求。但我想大家应该都会遇上这样的问题,不知道大家是用什么方式来解决的。
>“产品”很显然不应该关联“订单”的,避免双向关联,同时也避免双向依赖,一般产生双向依赖,主要分析方法没有采取Evans DDD这样建模方法来实现,将行为和属性混淆在一起,说到底,还是面向围绕数据表分析设计思维习惯造成的。
请问Banq兄,我是思想里真的有“面向围绕数据表分析设计思维习惯”吗?本身我是最反对用“表分析思维”的,也许是我的思维真的有错,还请指教,谢谢!