贫血领域模型和领域服务

发布于 2024-12-25 08:45:49 字数 109 浏览 3 评论 0原文

如果领域实体并不贫乏,因此它们在自身内部嵌入了特定的使用行为,那么是否需要/点来使用/构建特定的领域服务?验证应该进入实体内部怎么样?

单元测试用什么方式比较灵活?

谢谢!

If domain entities aren't anemic, so they embed specific-usage behavior inside themselfes, is there a need/point to use/build specific domain services? How about validation should it go inside an entity?

What way is more flexible for unit testing?

Thanks!

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

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

发布评论

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

评论(3

橘虞初梦 2025-01-01 08:45:49

通常,当不使用贫乏模型时,您仍然需要特定领域的服务。这是因为非贫血模型(或者只是模型,如果你愿意的话)应该包含允许自己被操纵的代码,但应该避免依赖于其他模型,特别是那些不通过父/子关系直接相关的模型。

单独的域服务将允许您保持这种分离并仍然提供丰富的功能,因为它们的域服务可能会了解整个域模型的更大视图。

至于验证,这些模型提供自己的验证并不罕见,只需记住,有时模型的有效状态取决于模型可能不知道的更大上下文,因此外部验证可能仍然存在。

最后,单元测试的灵活性在很大程度上取决于您的应用程序和底层技术(例如语言选择)。我还没有看到很多案例,其中任何一种方法本身都对单元测试有足够的影响。

Typically when an anemic model is not being used, you'll still have needs that require domain specific services. That's because the non-anemic models (or just, models, if you will) should contain code that allows themselves to be manipulated, but should refrain from taking dependencies on other models, especially models that are not directly related through a parent/child relationship.

Separate domain services will allow you to maintain that separation and still provide rich functionality, since they domain services can potentially be aware of a larger view of the entire domain model.

As for validation, it is not uncommon for these models to provide their own validation, just remember that sometimes the valid state of a model depends on a larger context that the model may not be aware of, so external validation is probably still going to exist.

Lastly, unit-testing flexibility is going to depend quite a bit on your application, and the underlying technology (such as language choice). I haven't seen many cases where either approach has enough influence on unit testing by itself.

幸福%小乖 2025-01-01 08:45:49

当您拥有域模型时,域服务是必需的,因为有些功能不属于您的实体。

例如,考虑一下存储库工厂存储库的接口可能位于您的域层中,但实现位于您的基础设施层中。使用工厂,实现和接口都将位于您的域层中。

这些域服务从您的应用程序层使用。应用程序层的目标是确保一切就绪,以便领域模型可以完成其工作。这可能意味着从存储库加载特定实体,然后在它们上调用特定于域的函数。

验证应该在实体内部进行。例如,假设您有一个 Money 类。

public class Money
{
    public Money(string currency, int amount)
    {
        Currency = currency;
        Amount = amount;
    }

    public int Amount { get; set; }

    public string Currency { get; set; }
}

如果 Money 类不允许有负数,您将在哪里验证这一点?

做到这一点的最佳地点是课堂内。一个实体对其自己的状态负责。
Money 类中,这很容易看到,但例如使用带有 OrderLinesOrder 类,Order 是负责检查是否存在应合并的重复 OrderLine 商品(节省运费!)

Domain Services are necessary when you have a Domain Model because there is functionality that's not a part of your entities.

Think for example about a Repository or a Factory. The interface for a Repository will probably be in your Domain Layer but the implementation in your Infrastructure Layer. With a Factory, both the implementation and interface will be in your Domain Layer.

These Domain Services are used from your Application Layer. The goal of an application layer is to make sure that everything is in place so the Domain Model can do his work. This could mean loading specific entities from Repositories and then calling domain specific functions on them.

Validation should go inside an Entity. For example, lets suppose you have a Money class.

public class Money
{
    public Money(string currency, int amount)
    {
        Currency = currency;
        Amount = amount;
    }

    public int Amount { get; set; }

    public string Currency { get; set; }
}

If the Money class is not allowed to have a negative amount, where would you validate this?

The best place to do this is inside the class. An entity is responsible for its own state.
In a Money class, this is easy to see but for example with an Order class with OrderLines the Order is responsible for checking if there are duplicate OrderLine items that should be merged (saves shipping costs!)

阳光①夏 2025-01-01 08:45:49

领域服务通常包含并不自然适合您的实体之一的领域功能。它通常是许多其他域对象所需的功能,可能使域服务成为许多对象连接的重要纽带。

至于验证,它可以位于不同的位置,具体取决于您想要验证的时间和内容:

  • 工厂在创建实体时强制执行不变量

  • 聚合根强制执行不变量涉及整个聚合的聚合

  • 基本验证也经常在实体本身内部执行

  • 您可以有需要与应用程序当前状态相关的数据的自定义验证,在这种情况下,验证更有可能在应用程序层中完成。

Domain Services usually contain domain functionality that doesn't naturally fit into one of your entities. It's often functionality that is required by many other domain objects, potentially making the domain service a big nexus where many objects connect.

As for validation, it can be in various places depending on when and what you want to validate :

  • Factories enforce invariants upon creation of an entity

  • Aggregate roots enforce invariants that concern the whole aggregate

  • Basic validation is also often performed inside the entities themselves

  • You can have custom validation that requires data related to the current state of the application, in which case the validation is more likely to be done in the Application layer.

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