在领域驱动设计中使用外部 Web 服务

发布于 2024-10-08 18:11:39 字数 144 浏览 0 评论 0原文

我想在域驱动设计项目中使用外部第三方 Web 服务,但我无法理解应该在哪一层访问外部 Web 服务。在领域服务中,但我不这么认为,因为领域服务仅适用于领域对象。但我的要求是,我必须根据外部 webservice 的输入执行操作列表,我必须在域服务中执行另一项任务。我很困惑。

I want to consume external third party web services in my domain driven design project, but i am not able to understand in which layer i should access external web services. In domain services but I don't think so , because domain services are for domain objects only. But my requirements is that, I have to perform list of operation based on the input from external webservice , I have to perform another task in domain service. I am confused.

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

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

发布评论

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

评论(4

月下凄凉 2024-10-15 18:11:39

您可以做的是根据域项目中的域模型引入所需服务的接口。每次您域中的类需要该服务时,您都可以向它传递对此接口的实现的引用。

然后,您创建一个“连接器实现”,它实现此接口并连接到您需要使用的 Web 服务。当您启动应用程序时,您可以为域类提供此实现,例如使用依赖项注入框架。

该连接器引用了您的域模型和 Web 服务定义。您的域模型没有引用连接器实现或 Web 服务 - 它只知道域项目中定义的接口。这称为控制反转。

这样,您的域类对 Web 服务一无所知,而只了解您在域模型中定义的接口。因此,您的领域逻辑与“邪恶”的外部世界保持分离。

What you could do, is introduce an interface to the required service in terms of your domain model in your domain project. Every time a class in your domain needs the service, you pass it a reference to an implementation of this interface.

Then you create a "connector implementation" which implements this interface and connects to the webservice you are required to use. When you start your application, you provide your domain classes with this implementation, for instance using a dependency injection framework.

This connector has a reference to your domain model and to the web service definition. Your domain model has no reference to the connector implementation or the webservice - it only knows of the interface defined in the domain project. This is called inversion of control.

This way, your domain classes know nothing about the web service, but only about the interface you defined in your domain model. Thus your domain logic stays separated from the 'evil' outside world.

知你几分 2024-10-15 18:11:39

您需要新的基础设施服务来访问外部Web 服务。正如前面所建议的,您可以将服务实现注入到域对象中。

请参阅 Eric Evans 的《领域驱动设计》第 105 页。或者,请参阅我的答案此处了解不同的DDD 中的服务类型。

You need a new Infrastructure Service to access the external Web Service. As suggested previously, you would have the service implementation injected into your Domain Object.

See page 105 of "Domain Driven Design" by Eric Evans. Alternatively, see my answer here to understand the different types of Services within DDD.

辞取 2024-10-15 18:11:39

我想说有几种选择:

1)如前所述,创建一个代表某种域服务的接口,并创建一个调用 Web 服务的具体实现。

2) 如果该服务只需要在发生事件时调用,例如订单确认时,那么您可以使用“域事件”(参见http://www.udidahan.com/2009/06/14/domain-events-salvation/)

让Order.Confirm()方法引发 OrderConfirmed 事件并有一个事件处理程序响应该事件并从那里调用 Web 服务。
事件处理程序和服务引用可以位于使用域层的应用程序层中。

3)如果Web服务的结果可以被认为是一个领域概念,那么您也许能够为结果创建一个实体,并创建一个从Web服务结果创建该实体的存储库,从而隐藏它是外部数据的事实。

I'd say there are a couple of alternatives:

1) as stated before, make an interface that represents some sort of domainservice and make a concrete implementation that calls the webservice.

2) If the service only needs to be called when something happens, e.g. when an order is confirmed, then you could use "Domain Events" (see http://www.udidahan.com/2009/06/14/domain-events-salvation/)

Let the Order.Confirm() method raise a OrderConfirmed event and have a event handler that responds to the event and call the webservice from there.
The event handler and service reference can live in the application layer that consumes the domain layer.

3) If the result of the webservice can be considered a domain concept, you might be able to create an entity for the result and a repository that createss this entity from the webservice result, thus hiding the fact that it is external data.

上课铃就是安魂曲 2024-10-15 18:11:39

据我猜测,您需要使用外部 Web 服务来执行某些操作。如果对于操作来说,您指的是您的业务逻辑,我认为正确的位置应该是您的业务逻辑层。在您的上下文中,您只需要使用它们即可。
如果您需要计算产品价格,您会在哪里调用计算增值税的外部 dll?

我希望这是有道理的:-)

From what I can guess you need to use the external web services just to perform some operations. If for operation you mean your business logic I think the right place would be at your business logic layer. In your context you need just use them.
where would you put a call to an external dll that calculate the VAT in the case you need it to calculate a product price?

I hope it makes sense:-)

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