Rich domain model with behaviours and ORM

前端 未结 4 1299
孤城傲影
孤城傲影 2021-02-02 13:54

After watching NDC12 presentation \"Crafting Wicked Domain Models\" from Jimmy Bogard (http://ndcoslo.oktaset.com/Agenda), I was wandering how to persist that kind of domain mod

4条回答
  •  温柔的废话
    2021-02-02 14:04

    This is actually a very good question and something I have contemplated. It is potentially difficult to create proper domain objects that are fully encapsulated (i.e. no property setters) and use an ORM to build the domain objects directly.

    In my experience there are 3 ways of solving this issue:

    • As already mention by Luka, NHibernate supports mapping to private fields, rather than property setters.
    • If using EF (which I don't think supports the above) you could use the memento pattern to restore state to your domain objects. e.g. you use entity framework to populate 'memento' objects which your domain entities accept to set their private fields.
    • As you have pointed out, using CQRS with event sourcing eliminates this problem. This is my preferred method of crafting perfectly encapsulated domain objects, that also have all the added benefits of event sourcing.

提交回复
热议问题