首先使用EF代码:如何在执行DDD时从实体的集合中删除行? [英] EF code first: How to delete a row from an entity's Collection while following DDD?

查看:92
本文介绍了首先使用EF代码:如何在执行DDD时从实体的集合中删除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这里是这种情况:



DDD声明您使用存储库获取聚合根,然后使用该存储根将其添加/删除到它拥有的任何集合中。 / p>

添加很简单,您只需调用集合上的 .Add(Item item) 您要添加到。保存时,新行将添加到数据库中。但是,删除是不同的-调用 .Remove(Item item)不会从数据库中删除该项目,而只是删除了外键。因此,虽然是的,但是从技术上讲,它不再是集合的一部分,而是仍在数据库中。



仔细阅读,唯一的解决方案是使用数据删除它上下文。但是根据DDD,域对象不应该知道数据上下文,因此删除必须在域外部进行。



正确的方法是去这个?还是让数据库中充满了孤立的孤儿(可以运行例行程序将其清除)?

解决方案

我已经解决了这个问题我目前正在使用域事件; DDD概念埃里克·埃文斯(Eric Evans)说应该在他的书中。



虽然不允许域对象知道对象上下文,但是 IDomainEventHandler 是-因此,我有一个 DomainObjectDeletionHandler ,它在控制权返回之前从对象上下文中删除了已删除的对象。我的应用程序层和所做的更改已保存。



有关更多信息,我写了关于域事件的实现以及如何将所有内容挂钩的博客



希望有帮助:)



编辑



例如,如果您有一个 Order 类,该类具有一个 OrderItems 集合类型为 OrderItem 的离子:

 公共类订单
{
//其他内容

public void RemoveOrderItem(int orderItemId)
{
var orderItemToRemove = OrderItems.First(oi => oi.Id == orderItemId)

OrderItems.Remove(orderItemToRemove);

DomainEvents.Raise(new OrderItemRemoved(orderItemToRemove));
}
}


So here's the scenario:

DDD states that you use a repository to get the aggregate root, then use that to add/remove to any collections it has.

Adding is simple, you simple call .Add(Item item) on the Collection you wish to add to. A new row is added to the database when you save. However, deleting is different - calling .Remove(Item item) doesn't remove the item from the database, it simply removes the foreign key. So while, yes, it is technically no longer part of the collection anymore, it's still in the database.

Reading around, the only solution is to delete it using the data context. But according to DDD the domain object shouldn't be aware of the data context so therefore deleting will have to be done outside of the domain.

What is the right way to go about this? Or Is leaving the database full of orphans acceptable (perhaps running a routine to clear them out)?

解决方案

I've solved this problem in the application I'm currently working on by using domain events; a DDD concept Eric Evans said should have been in his book.

While domain objects aren't allowed to know about the object context, an IDomainEventHandler is - I've therefore got a DomainObjectDeletionHandler which deletes 'removed' objects from the object context before control returns to my application layer and the changes are saved.

For more information, I've written a blog about my implementation of domain events and how I approached hooking everything together.

Hope that helps :)

Edit

For example, if you have an Order class which has an OrderItems collection of type OrderItem:

public class Order
{
    // Other stuff

    public void RemoveOrderItem(int orderItemId)
    {
        var orderItemToRemove = OrderItems.First(oi => oi.Id == orderItemId)

        OrderItems.Remove(orderItemToRemove);

        DomainEvents.Raise(new OrderItemRemoved(orderItemToRemove));
    }
}

这篇关于首先使用EF代码:如何在执行DDD时从实体的集合中删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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