领域模型中的设置器

发布于 2024-09-18 21:31:44 字数 565 浏览 2 评论 0原文

在领域模型上的 DDD 设置器的上下文中,存在代码味道。 应该避免使用它们,原因很简单,因为它们并不是真正的域的一部分。其中没有领域专家可以理解的名词,对数据的更改应该通过特定的方法来代替。

示例:

customer.StreetName = ...
customer.City = ...

虽然正确的方法是使用一个 customer.ChangeAddress 方法,然后可以发布事件等。至少从我的理解来看,这都是合理的理论,我完全可以理解为什么领域模型中的设置者并不是真正需要的。

但: 如果域模型上没有设置器,这些方法将很难测试。

如果我无法在没有接受所有参数的大构造函数或执行一些反射魔法的情况下构建一个 Customer 实例,那么我如何获得一个 Customer 实例来运行我的测试?我在后端使用 NHibernate,因此 NHibernate 已经做了一些反射魔法来首先填充这些字段。

但是拥有一个带有 10 个参数的 ctor 感觉真的很糟糕..(对于工厂方法也是如此)。

对此有什么建议吗?

问候丹尼尔

In the context of DDD setters on a domain model are a code smell.
They should be avoided for the simple reason that they are not really part of the domain. There are no nouns in it that a Domain Expert may understand and changes to the data should go through specific methods instead.

Example:

customer.StreetName = ...
customer.City = ...

While the right way to do that would be to have a customer.ChangeAddress method that could then publish an event etc etc.. At least from my understanding this is all sound theory and I can totally understand why setters in a domain model are not really desireable.

BUT:
Without setters on your domain model, those methods get pretty hard to test.

How do I get a Customer instance to run my tests against if I can't construct one without having either a big-ass constructor that takes in ALL arguments, or do some reflection magic? I use NHibernate in the backend so NHibernate already does some reflection magic to populate these fields in the first place.

But it feels really bad to have a ctor with 10 arguments.. (And the same would be true for a factory method).

Any advice on this?

greetings Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷月断魂刀 2024-09-25 21:31:44

在经典(非 CQRS)DDD 中,将所有数据分解为值对象是一种很好的做法,这样您的实体就可以简化为其主要功能:维护身份。

在您的示例中,客户应该引用 Address ValueObject 并具有 ChengeAddress 方法,该方法应该简单如下:

public void ChangeAddress(Address address)
{
   //Consistency rules are here
   _address = address;
}

尝试将尽可能多的逻辑从实体移动到值对象。它们本质上更容易测试,因为好的价值对象很小且不可变。您使用构造函数在给定状态下实例化 VO 并执行它(通常通过调用返回另一个经过转换的 VO 实例的方法)。

最后但并非最不重要的一点是,根据我的经验,我可以说,如果测试您的领域模型需要额外的基础设施(如反射或任何其他工具),那么您就做错了(通过引入不必要的耦合)。

In classic (non-CQRS) DDD it is a good practice to factor out all the data to Value Objects so that your entities are reduced to their primary function: maintaining identity.

In you example Customer should reference an Address ValueObject and have a ChengeAddress method which would should be as simple as:

public void ChangeAddress(Address address)
{
   //Consistency rules are here
   _address = address;
}

Try to move as much logic as you can from your entities to your value objects. They are inherently more testable since good value object is small and immutable. You use a constructor to instantiate VO in a given state and exercise it (usually by invoking a method which returns another, transformed, VO instance).

Last but not least, from my experience I can say that if testing your domain model requires additional infrastructure (like reflection or any other tool), you're doing it wrong (by introducing unnecessary coupling).

荆棘i 2024-09-25 21:31:44

您可能想尝试AutoFixture

混合一点反射爱和领域变得相当可测试:

namespace Unit{
  using System;
  using System.Linq.Expressions;
  public static class ObjectExtensions{
    public static T Set<T,TProp>(this T o,
      Expression<Func<T,TProp>> field,TProp value){

      var fn=((MemberExpression)field.Body).Member.Name;
      o.GetType().GetProperty(fn).SetValue(o,value,null);
      return o;
    }
  }
}

用法:

myUberComplexObject.Set(x=>x.PropertyOfIt, newValueOfIt);

并且您至少应该尝试将那些“大屁股”对象划分为较小的对象。尝试建立一个层次结构(只需确保它符合通用语言)。

You might want to try AutoFixture.

Mix in a bit reflection love and domain gets quite testable:

namespace Unit{
  using System;
  using System.Linq.Expressions;
  public static class ObjectExtensions{
    public static T Set<T,TProp>(this T o,
      Expression<Func<T,TProp>> field,TProp value){

      var fn=((MemberExpression)field.Body).Member.Name;
      o.GetType().GetProperty(fn).SetValue(o,value,null);
      return o;
    }
  }
}

Usage:

myUberComplexObject.Set(x=>x.PropertyOfIt, newValueOfIt);

And You should at least try to divide those "big-ass" objects into smaller ones. Try to make a hierarchy (just make sure it's compliant with ubiquitous language).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文