Persistence encapsulated via the domain, or persistence via the Repository?

送分小仙女□ 提交于 2019-12-04 19:58:14

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)

If your domain model doesn't include persistence, then it doesn't include the operation of storing something. If your entity is something from the domain model, then it has no business persisting itself.

You say:

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

But i think that's mistaken. Your domain objects have no more responsibility for persisting themselves than they do printing themselves, drawing themselves on screen, etc. Your domain class should not have a UpdateOrder method.

Now, you might not want to expose the raw repository (from your persistence implementation layer) to other code, but that just means wrapping it in something suitable. It sounds like you do have code that needs to talk about persistence, so figure out what level of discourse it needs to work at, and expose a suitable interface to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!