问题描述
如果我的域模型不应该了解/关心存储库,那么封装了CRUD-Update的某些行为(如.UpdateOrder(...)
)如何与存储库接口?通过域服务?
If my Domain Model is not supposed to know/care about the Repository, then how does some behaviour like .UpdateOrder(...)
, that encapsulates a CRUD-Update, interface with the Repository? Through a Domain Service?
好,那么我的存储库中有一个有效的CRUD更新,可与我的.UpdateOrder(...)
一起使用.没关系.但是我不希望有人在存储库上使用Update方法,而是希望他们经历实体上的行为(改用UpdateOrder()).我更喜欢通过类似于域模型满足不变式的方式-通过其设计(私有集属性等)-我的存储库 not 公开另一种方法来更新"/持久化实体.
Ok, then my Repository has an effective CRUD-Update that's used in conjunction with my .UpdateOrder(...)
. That's fine. But i don't want someone to use the Update method on the Repository, i want them to go through the behaviour on the Entity (use UpdateOrder() instead). I'd prefer that in likeness to the way my Domain Model satisfies invariants - by it's design (private set properties, etc) - my Repository not expose an alternate method to "updating"/persisting the Entity.
这是一个简单的访问修饰符问题,由我在Repo公共场所中没有Update方法解决.还是有一个更好"的答案?请帮我DDD忍者.
Is this simply a access modifier problem that is solved by me not having the Update method in the Repo public. Or is there a 'better' answer? Please help me DDD ninjas.
推荐答案
DDD中的严格顺序为:
The strict sequence in DDD would be:
var entityRepository = MyServiceLocator.Get<IEntityRepository>();
var myEntity = entityRepository.Load(<some criteria>);
myEntity.Change(something);
entityRepository.Save(myEntity);
存储库始终负责检测/保留实体中的所有更改.
The repository is always responsible for detecting/persisting all of the changes within the entity.
(顺便说一句,我假设您的实体是一个聚合根)
(btw, I'm assuming that your entity is an aggregate root)
这篇关于通过域封装的持久性,还是通过存储库封装的持久性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!