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.
-
There are many ways to solve a problem, and there will always be differences of opinion. Maybe you should add more detail to your original question? Do you want the DO to access the data when it needs, or is the data provided by some other trigger? Please provide more detail. – Vijay Patel Dec 22 '10 at 09:33
4 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.

- 10,367
- 5
- 59
- 80
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.

- 17,094
- 6
- 31
- 35
-
3You are telling me to create application service, and inject that in domain object. Is that possible ? Can a domain object access to app service. How ? Others are telling me to do in domain service. Total mess here. – chandra Dec 22 '10 at 09:24
-
@ Vijay In my domain i have advertiser and publisher entity, i want to allow advertiser to deposit fund in his balance which is part of Advertiser( Transfer fund from PayPal, on successful transfer we increase his balance) same for with draw and fro Publisher entity. Now to support transfer Paypal requires various parameters, for that i have created various entities, which gets filled For eg -: PaymentInfo and we pass them to PayPal or any payment gateway. So where do I put all those entities in my domain. Do I create Payment Aggregate and create domain service for transfer or in app layer. – chandra Dec 22 '10 at 10:28
-
This is very wrong @chandra. The dependencies go from the inside to the outside, not the other way around. The Domain can NEVER use application services, but, application services can and should use the domain as they orquestrate its execution. Read more about clean achitecture by uncle bob, he makes it really clear. One thing you could do is to launch domain events from the domain and the corresponding event handlers in the application layer do their thing. – Gustavo Andrade Ferreira Aug 08 '19 at 12:24
-
Looks like infrastructure service or domain service based on your other answer, definitely not application service. Even that writes that external services consumer applicaiton services and not the other way... – inf3rno Dec 12 '19 at 03:49
-
@inf3rno: Thanks for pointing that out! I must have been winding down for Chrismas that year (or possibly drunk). – Vijay Patel Dec 12 '19 at 11:38
-
@vijay this answer is not being accepted as an answer! why can't i inject IHttpClient directly to my application service ? why do i have to inject ihttpclient to Iexternalservice (in my infra service layer) , and then inject Iexternalservice into my application service ? and the link you given does not provide answer this question specifically ! – bet Jun 19 '20 at 01:47
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.

- 22,764
- 18
- 97
- 193
-
1Actually i want to create payment processor for my application paypal,authorize.net . I created that, it also has a lot of entities like IPaymentMethod, PaymentInfo like these. But I am not able to conclude whether I create separate aggregate and put all this entities there and create domain services which will take data filled entities and pass them to either paypal or authorize.net, and wait for status message. What I do. Is payment processing part is internal to my domain or not i am not able to conclude that. Please help. – chandra Dec 22 '10 at 09:33
-
1IMO, Whichever solution you choose is fine, as long as your domain classes don't have a dependency on paypal and authorize.net. So it _could_ be that payment processing is part of your domain: if so make it so and choose appropriate abstractions and interfaces that you can realize with paypal/auth.net implementations. It sounds like you're on the right track; even more so if you get the dependencies right. – Marijn Dec 22 '10 at 15:28
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:-)

- 26,379
- 6
- 61
- 70