Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming external webservices in domain driven design

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.

like image 544
chandra Avatar asked Dec 21 '10 08:12

chandra


People also ask

What is a service in domain driven design?

Domain Services (or just Services in DDD) is used to perform domain operations and business rules. In his DDD book, Eric Evans describes a good Service in three characteristics: The operation relates to a domain concept that is not a natural part of an Entity or Value Object.

What problem does Domain Driven Design Solve?

The domain-driven approach is here to solve the complexity of software development. On the other hand, you can use emergent design when the challenge is simple. However, when your application is complex, the complexity will only grow, and so will your problems. Domain-driven design bases on the business domain.

What are the benefits of Domain Driven Design?

Advantages of domain-driven design The most obvious advantage of DDD is that it gets everybody using the same language. When development teams use the same language as domain experts, it leads to software design that makes sense to the end user.

Is domain driven design still relevant?

Domain Driven Design (DDD) has recently gained additional popularity, as evidenced by new books, conference talks, and even complete conferences dedicated to it), and lots of trainings – including some by our very own colleagues here at INNOQ.


3 Answers

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.

like image 139
Marijn Avatar answered Oct 12 '22 14:10

Marijn


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.

like image 34
Vijay Patel Avatar answered Oct 12 '22 14:10

Vijay Patel


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.

like image 45
Roger Johansson Avatar answered Oct 12 '22 15:10

Roger Johansson