EF代码第一:你是如何打算从实体的集合中删除一行,而下面的DDD? [英] EF code first: How are you meant to delete a row from an entity's Collection while following DDD?

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

问题描述

所以这里的情景:



DDD表明您使用存储库来获得聚合根,然后用它来添加/删除有任何集合<。 / p>

添加很简单,你简单的通话。新增(项目项)收藏要添加到。当您保存一个新行添加到数据库中。但是,删除是不同的 - 调用上卸下摆臂(项目项)不从数据库中删除的项目,它只是删除外键。所以,虽然是的,这是技术上不再是集合的一部分了,它仍然在数据库中。



阅读四周,唯一的解决办法是使用数据删除上下文。但根据DDD域对象不应该知道的数据上下文等于是删除将有域之外做的。



什么是正确的方式去呢?或者离开数据库完全孤儿接受(可能运行一个程序来清理出来)的?


解决方案

我已经解决了这个在我目前正在通过使用域事件;一个DDD概念的埃里克·埃文斯说应该已经在他的书。



虽然不允许域对象了解对象的上下文中, IDomainEventHandler 是 - 所以我有一个 DomainObjectDeletionHandler 它删除'删除'从对象上下文之前控制返回到我的对象应用层和改变被保存。



有关更多信息,我写的博客中了解我实现领域事件,以及如何我走近挂钩在一起的一切。



希望有所帮助:)



修改



例如,如果有一个具有类型的的OrderItems 集合的订单 OrderItem的

 公共类订单
{
//其他的东西

酒店的公共空间RemoveOrderItem(INT orderItemId)
{
VAR orderItemToRemove = OrderItems.First(OI = GT; oi.Id == orderItemId)

OrderItems.Remove(orderItemToRemove);

DomainEvents.Raise(新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天全站免登陆