聚合根的工厂方法签名
我想编写一个工厂方法来实例化作为聚合根的实体。
该方法是否应该接受聚合的子实体和值作为实例化对象,还是应该只接受原始类型?
例如,如果我有一个由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这只是一个品味问题,尽管它可能取决于您的对象创建策略,而且您可能会混合搭配它们。
如果您的聚合根已经为其子对象提供了工厂方法(例如,如果已经存在 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.“聚合根”是对象图的最顶层节点。所有其他对象都是通过此根对象创建和访问的。换句话说:如果您通过外部工厂方法创建 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...
关于使用工厂方法的好处(我不太熟悉聚合和根的规则):
计算机类构造函数可以是
您的计算机类,现在不依赖于处理器和内存的具体实现。其他类负责使用具有特定内存和处理器的计算机。
通过这种方法,您将获得更多可维护代码的好处,并且能够在不更换计算机的情况下升级内存和处理器。
不知道这是否能回答您在特定情况下的问题,但希望这会有所帮助。其他资源包括:
Regarding to the benefits of using the Factory Method (I am not very familiar with the rules of Aggregates and Roots):
The computer class constructor could be
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: