gpt4 book ai didi

design-patterns - 领域驱动设计和工厂类的作用

转载 作者:行者123 更新时间:2023-12-03 06:13:16 26 4
gpt4 key购买 nike

我不清楚工厂类的角色和职责是什么。我知道工厂类应该负责创建域对象(聚合根)及其关联的实体和值对象。

但是我不清楚 DDD 架构的工厂“层”在哪里?工厂应该直接调用存储库来获取其数据还是服务库?

工厂在以下框架中的位置:
UI > 应用程序 > 域 > 服务 > 数据

此外,由于工厂是唯一允许创建对象的地方,如果您想在数据和服务层中创建对象,您不会得到循环引用吗?

如果工厂类的作用是创建对象,那么服务层有什么好处?

我问了很多问题,感谢您的回复。我缺少的是一个示例应用程序,它演示了域驱动设计项目中的所有层如何组合在一起......有什么东西吗?

最佳答案

But what is not clear to me is where the factory "layer" lies with a DDD architecture? Should the factory be calling directly into the repository to get its data or the service library?

工厂应该是构建域对象的一站式商店。需要执行此操作的代码的任何其他部分都应该使用工厂。

通常,至少有三个数据源用作域对象构建工厂的输入:来自 UI 的输入、来自持久性的查询结果以及对域有意义的请求。因此,为了回答您的具体问题,存储库将使用工厂。

这是一个例子。我正在使用Holub's Builder pattern这里。 编辑:忽略此模式的使用。我开始意识到它 doesn't mix too well with DDD factories

// domain layer
class Order
{
private Integer ID;
private Customer owner;
private List<Product> ordered;

// can't be null, needs complicated rules to initialize
private Product featured;

// can't be null, needs complicated rules to initialize, not part of Order aggregate
private Itinerary schedule;

void importFrom(Importer importer) { ... }

void exportTo(Exporter exporter) { ... }

... insert business logic methods here ...

interface Importer
{
Integer importID();
Customer importOwner();
Product importOrdered();
}

interface Exporter
{
void exportID(Integer id);
void exportOwner(Customer owner);
void exportOrdered(Product ordered);
}
}

// domain layer
interface OrderEntryScreenExport { ... }

// UI
class UIScreen
{
public UIScreen(OrderEntryDTO dto) { ... }
}

// App Layer
class OrderEntryDTO implements OrderEntryScreenExport { ... }

OrderFactory 可能如下所示:

interface OrderFactory
{
Order createWith(Customer owner, Product ordered);
Order createFrom(OrderEntryScreenExport to);
Order createFrom(List<String> resultSets);
}

特色产品的逻辑和行程的生成位于 OrderFactory 中。

现在这是工厂在每个实例中的使用方式。

在订单存储库中:

public List<Order> findAllMatching(Criteria someCriteria)
{
ResultSet rcds = this.db.execFindOrdersQueryWith(someCriteria.toString());
List<List<String>> results = convertToStringList(rcds);

List<Order> returnList = new ArrayList<Order>();

for(List<String> row : results)
returnList.add(this.orderFactory.createFrom(row));

return returnList;
}

在您的应用程序层:

public void submitOrder(OrderEntryDTO dto)
{
Order toBeSubmitted = this.orderFactory.createFrom(dto);

this.orderRepo.add(toBeSubmitted);

// do other stuff, raise events, etc
}

在您的域层中,可能需要进行单元测试:

Customer carl = customerRepo.findByName("Carl");
List<Product> weapons = productRepo.findAllByName("Ruger P-95 9mm");
Order weaponsForCarl = orderFactory.createWith(carl, weapons);

weaponsForCarl.place();

assertTrue(weaponsForCarl.isPlaced());
assertTrue(weaponsForCarl.hasSpecialShippingNeeds());

Where does the factory fit into the following framework: UI > App > Domain > Service > Data

域名。

Also, because the factory is the only place allowed for object creation would'nt you get circular references if you wanted to create your objects in your data and service layers?

在我的示例中,所有依赖项都是从上到下流动的。我用了Dependency Inversion Principle (PDF链接)以避免您所说的问题。

If the role of the factory class is for object creation then what benefits does the service layer have?

当您的逻辑不适合任何单个域对象,或者您的算法涉及编排多个域对象时,请使用服务。该服务将封装任何不适合其他任何内容的逻辑,并将其委托(delegate)给适合的域对象。

在我在这里潦草写下的示例中,我想为订单制定行程将涉及多个域对象。 OrderFactory 可以委托(delegate)给这样的服务。

顺便说一句,您描述的层次结构可能应该是 UI > 应用程序 > 域服务 > 域 > 基础设施(数据)

I've asked a lot of questions and appreciate any response. What i'am lacking is a sample application which demonstrates how all the layers in a domain driven design project come together...Is there anything out there?

Applying Domain Driven Design and Patterns Jimmy Nilsson 的作者是对 Eric Evans 的 Domain-Driven Design 的极大赞美。它有很多代码示例,但我不知道是否强调分层。分层可能很棘手,并且几乎是一个独立于 DDD 的主题。

在埃文斯的书中,有一个非常小的分层示例,您可能想查看一下。分层是一种企业模式,Martin Fowler 写道 Patterns of Enterprise Application Architecture ,您可能会发现它也很有用。

关于design-patterns - 领域驱动设计和工厂类的作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/555241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com