聚合根的工厂方法签名

发布于 2024-08-06 18:31:07 字数 523 浏览 5 评论 0原文

我想编写一个工厂方法来实例化作为聚合根的实体。

该方法是否应该接受聚合的子实体和值作为实例化对象,还是应该只接受原始类型?

例如,如果我有一个由 Processor 和 Memory 对象组成的实体 Computer,工厂方法是否应该采用以下形式:

public Computer NewComputer(
    string computerName, 
    int processorCores, 
    int processorClockSpeed, 
    string memoryType, 
    int memoryRam) 
{
    ...
}

或者

public Computer NewComputer(
    string computerName, 
    Processor processor, 
    Memory memory) 
{
    ...
}

这只是一个品味问题,还是这里有任何认真的考虑因素?

I want to write a factory method to instantiate an entity that is an aggregate root.

Should the method accept the aggregated child entities and values as instantiated objects, or should it accept only primitive types?

For example, if I had an entity Computer composed of a Processor and a Memory object, should the factory method take the form:

public Computer NewComputer(
    string computerName, 
    int processorCores, 
    int processorClockSpeed, 
    string memoryType, 
    int memoryRam) 
{
    ...
}

or

public Computer NewComputer(
    string computerName, 
    Processor processor, 
    Memory memory) 
{
    ...
}

Is it only a matter of taste, or are there any serious considerations here?

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

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

发布评论

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

评论(3

随遇而安 2024-08-13 18:31:07

这只是一个品味问题,尽管它可能取决于您的对象创建策略,而且您可能会混合搭配它们。

如果您的聚合根已经为其子对象提供了工厂方法(例如,如果已经存在 CreateProcessor() 来支持添加其他处理器),那么您的第一种方法可能是合适的。

或者,如果您使用ComputerFactory(或存储库)来创建或重建聚合根,则该工厂可能已经知道如何创建子对象,在这种情况下,它将在构建过程中创建它们上你的聚合图表,你的第二种方法将是合适的。

It's just a matter of taste, though it can depend on your object-creation strategy, plus you might mix and match them.

Where your aggregate root already has factory methods for its child objects (e.g., if CreateProcessor() already exists to support adding additional processors), your first approach might be appropriate.

Alternately, if you're using a ComputerFactory (or repository) to create or reconstitute your aggregate root, that factory may already knows how to create child objects, in which case it will create them en route to building up your aggregate's graph and your second approach will be appropriate.

九命猫 2024-08-13 18:31:07

“聚合根”是对象图的最顶层节点。所有其他对象都是通过此根对象创建和访问的。换句话说:如果您通过外部工厂方法创建 Computer 类的组件,那么您不能再将其称为“聚合根”。
这并不是说你的第二个例子有些不好或有臭味之类的,只是它不符合“聚合根”的概念......

An 'aggregate root' is the topmost node of an object graph. All other objects are created and accessed via this root object. In other words: If you create the components of the Computer class by an external factory method, then you cannot call it 'aggregate root' any more.
This is not to say that your second example is somehow bad or smelly or something, it's just that it doesn't meet the 'aggregate root' concept...

梦罢 2024-08-13 18:31:07

关于使用工厂方法的好处(我不太熟悉聚合和根的规则):

  • 我想处理器和内存是具有某些行为的对象,您希望将它们与计算机类分开。
  • 计算机类构造函数可以是

    public Computer(字符串计算机名,IProcessor处理器,IMemory内存) 
    {
    }
    

您的计算机类,现在不依赖于处理器和内存的具体实现。其他类负责使用具有特定内存和处理器的计算机。

通过这种方法,您将获得更多可维护代码的好处,并且能够在不更换计算机的情况下升级内存和处理器。

不知道这是否能回答您在特定情况下的问题,但希望这会有所帮助。其他资源包括:

  1. http://www.objectmentor.com/resources/articles/inheritanceVsDelegation。 pdf
  2. 实体电子书:http://www.lostechies.com/content/pablo_ebook。 ASPX

Regarding to the benefits of using the Factory Method (I am not very familiar with the rules of Aggregates and Roots):

  • I imagine Processor and Memory are objects having certain behaviors that you want to separate from your Computer Class.
  • The computer class constructor could be

    public Computer(string computerName, IProcessor processor, IMemory memory) 
    {
    }
    

Your Computer class now does not depend on specific implementation of Processor and Memory. Other class is responsible on using a computer with specific Memory and Processor.

With this approach you will have benefits of having more maintainable code, and being able to upgrade the Memory and Processor without changing the Computer.

Dont know if this answear your question in your particular scenario, but Hope this helps. Other resources are:

  1. http://www.objectmentor.com/resources/articles/inheritanceVsDelegation.pdf
  2. SOLID ebook: http://www.lostechies.com/content/pablo_ebook.aspx
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文