The consumption of external web services in domain design

I want to use external third-party web services in my domain-driven project, but I cannot figure out in which layer I should access external web services. In domain services, but I don’t think so, because domain services are only for domain objects. But my requirements are that I have to perform a list of operations based on input from an external webservice, I need to perform another task in the domain service. I am embarrassed.

+4
source share
4 answers

What you can do is present the interface for the required service in terms of your domain model in your domain project. Each time your domain class needs a service, you pass it a link to the implementation of this interface.

Then you create a “connector implementation” that implements this interface and connects to the web service that you want to use. When you launch the application, you provide your domain classes to this implementation, for example, using the dependency injection infrastructure.

This connector has a link to your domain model and web service definition. Your domain model does not have a link to a connector implementation or a web service — it only knows the interface defined in the domain project. This is called control inversion.

Thus, your domain classes do not know anything about the web service, but only about the interface that you defined in your domain model. In this way, your domain logic remains separate from the "evil" outside world.

+5
source

Access to an external web service requires a new Application Service . As suggested earlier, you will have a service implementation embedded in your domain object.

See p. 105 “Domain Managed Project” by Eric Evans. Alternatively, see My answer here to understand the different types of services in DDD.

+4
source

I would say that there are several alternatives:

1), as stated earlier, create an interface that represents some domain service and will create a specific implementation that calls the web service.

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

Let the Order.Confirm () method raise the OrderConfirmed event and have an event handler that responds to the event and is called by the web service. An event handler and a service reference can live at the application level, which consumes the domain layer.

3) If the result of a web service can be considered a concept of a domain, you can create an entity for the result and a repository that create this object from the webservice result, thereby hiding the fact that this is external data.

+2
source

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

Hope this makes sense :-)

+1
source

Source: https://habr.com/ru/post/1332872/


All Articles