用行为和ORM丰富的域模型 [英] Rich domain model with behaviours and ORM

查看:220
本文介绍了用行为和ORM丰富的域模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看NDC12 presentation手工艺邪恶的领域模型,从吉米·博加德后( http://ndcoslo.oktaset.com/Agenda ),我是游荡如何坚持那种领域模型。
这是presentation示例类:

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 model.
This is sample class from presentation:

public class Member
{
    List<Offer> _offers;

    public Member(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
        _offers = new List<Offer>();
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public IEnumerable<Offer> AssignedOffers { 
        get { return _offers; }
    }

    public int NumberOfOffers { get; private set; }

    public Offer AssignOffer(OfferType offerType, IOfferValueCalc valueCalc)
    {
        var value = valueCalc.CalculateValue(this, offerType);
        var expiration = offerType.CalculateExpiration();
        var offer = new Offer(this, offerType, expiration, value);
        _offers.Add(offer);
        NumberOfOffers++;
        return offer;
    }
}

所以有包含在此领域模型的一些规则:
  - 会员必须有姓和名
  - 书数目不会向外
改变   - 会员负责创建新的报价,计算其价值和分配

so there are some rules contained in this domain model:
- Member must have first and last name
- Number of offers can't be changed outside
- Member is responsible for creating new offer, calculating its value and assignment

如果,如果尝试将此映射到一些ORM像实体框架或NHibernate的,它不会工作。 那么,什么是映射这种模型数据库与ORM最好的方法?
例如,如何从数据库加载AssignedOffers如果没有二传手?

If if try to map this to some ORM like Entity Framework or NHibernate, it will not work. So, what's best approach for mapping this kind of model to database with ORM?
For example, how do I load AssignedOffers from DB if there's no setter?

唯一的,它对于我来说是使用命令/查询体系结构意义:查询总是与DTO做的结果是,没有域的实体,并命令在域模型完成的。此外,事件采购是非常适合的领域模型的行为。但这种CQS结构并不可能适合于每一个项目,特别是棕地。还是不行?

Only thing that does make sense for me is using command/query architecture: queries are always done with DTO as result, not domain entities, and commands are done on domain models. Also, event sourcing is perfect fit for behaviours on domain model. But this kind of CQS architecture isn't maybe suitable for every project, specially brownfield. Or not?

我知道这里类似的问题,也没有找到具体的例子和解决方案。

I'm aware of similar questions here, but couldn't find concrete example and solution.

推荐答案

这其实是一个很好的问题,这是我所预期的。这可能是很难创建一个完全封装(即无属性setter)正确的域对象,并使用ORM直接建立域对象。

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.

在我的经验中有3种方式解决这个问题:

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

  • 在前面已经提到由卢卡,NHibernate的支持映射到私人领域,而不是属性setter。
  • 如果使用EF(我不认为支持以上),你可以使用 Memento模式来恢复状态你的域对象。例如您使用实体框架,填充,你的域实体接受设置自己的私人领域的纪念的对象。
  • 正如你所指出的那样,使用CQRS与事件外包解决了这个问题。这是我的preferred各具特色完全封装领域对象,也有事件采购的全部增值收益的方法。
  • 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.

这篇关于用行为和ORM丰富的域模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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