域驱动设计(Linq to SQL)-如何删除聚合的一部分? [英] Domain Driven Design (Linq to SQL) - How do you delete parts of an aggregate?

查看:56
本文介绍了域驱动设计(Linq to SQL)-如何删除聚合的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎对整个DDD \ LinqToSql业务有些困惑.我正在建立使用POCOS和linq到sql的系统,并且我有用于聚合根的存储库. 因此,例如,如果您有Order-> OrderLine类,则有一个Order的存储库,但没有OrderLine,因为Order是聚合的根.存储库具有用于删除订单的delete方法,但是如何删除OrderLines? 您可能以为您在Order上有一个名为RemoveOrderLine的方法,该方法从OrderLines集合中删除了该行,但它还需要从底层的l2s表中删除OrderLine.由于没有OrderLine的存储库,您应该怎么做?

I seem to have gotten myself into a bit of a confusion of this whole DDD\LinqToSql business. I am building a system using POCOS and linq to sql and I have repositories for the aggregate roots. So, for example if you had the classes Order->OrderLine you have a repository for Order but not OrderLine as Order is the root of the aggregate. The repository has the delete method for deleting the Order, but how do you delete OrderLines? You would have thought you had a method on Order called RemoveOrderLine which removed the line from the OrderLines collection but it also needs to delete the OrderLine from the underlying l2s table. As there isnt a repository for OrderLine how are you supposed to do it?

也许有专门的公共存储库,用于查询域对象实际用于删除聚合中内容的根和内部通用存储库?

Perhaps have specialized public repostories for querying the roots and internal generic repositories that the domain objects actually use to delete stuff within the aggregates?

public class OrderRepository : Repository<Order> {
    public Order GetOrderByWhatever();
}

public class Order {
    public List<OrderLines> Lines {get; set;} //Will return a readonly list
    public RemoveLine(OrderLine line) {
        Lines.Remove(line);
        //************* NOW WHAT? *************//
        //(new Repository<OrderLine>(uow)).Delete(line) Perhaps??
        // But now we have to pass in the UOW and object is not persistent ignorant. AAGH!
    }
}

我很想知道其他人做了什么,因为我不能成为唯一一个为此而苦苦挣扎的人....希望....谢谢

I would love to know what other people have done as I cant be the only one struggling with this.... I hope.... Thanks

推荐答案

您在Order上调用RemoveOrderLine,从而调用了相关的逻辑.这不包括对它的持久版本进行更改.

You call the RemoveOrderLine on the Order which call the related logic. This does not include doing changes on the persisted version of it.

稍后,您将在存储库上调用保存/更新方法,该方法将接收修改后的订单.特定的挑战在于了解域对象中发生了什么变化,其中有几个选项(我敢肯定我列出的选项不止这些):

Later on you call a Save/Update method on the repository, that receives the modified order. The specific challenge becomes in knowing what has changed in the domain object, which there are several options (I am sure there are more than the ones I list):

  • 让域对象跟踪更改,这包括跟踪需要从订单行中删除x.与实体跟踪类似的内容也可能会被排除在外.
  • 加载持久版本.在存储库中有代码可以识别持久版本和内存中版本之间的差异,然后运行更改.
  • 加载持久版本.在根聚合中包含代码,可以在给定原始根聚合的情况下获得差异.

这篇关于域驱动设计(Linq to SQL)-如何删除聚合的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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