What is the best way to implement DDD/CQRS for Web API project as I still don't quite understand how to get the response when calling other service from a Web API. I read about Rx observable stuff but I would like a more clear explanation and simple sample link would be great. I'm using Udi Dahan style of domain events in ASP.NET Web API and Autofac for the injection. Here is what I try to use domain events in Web API in Controller. The code that I do are all working.
public class MyApiController : ApiController
{
// POST: api/myapi
public void Post([FromBody]string value)
{
DomainEvents.Raise(new HelloMessage(value));
}
}
This raises the events handler:
public void HelloMessagesHandler : IDomainEventHandler<HelloMessage>
{
public void Handle(HelloMessage @event)
{
Log.Write($"Hello {@event.Value}");
{
{
Of course the HelloMessage event name should be using ubiquitous language which will be more appropriate. This is just a simple sample and that was easy.
Now, I wanted to call another, let's say 3rd part web API services which is taking quite a seconds (long) to response. And how do I retrieve the responses and return to the MyApiController. According to Udi Dahan, this not a good idea to be use directly in the handler. For example:
public void HelloMessagesHandler : IDomainEventHandler<HelloMessage>
{
private readonly IService _service;
public HelloMessagesHandler (IService service)
{
_service = service;
{
public void Handle(HelloMessage @event)
{
var response = _service.DoSomethingWithLongProcess(@event.Value);
// return response <-- to MyApiController
{
{
My questions are:
- How do I do this in simple and better way?
- How to retrieve back the response from the 3rd party web API to MyApiController after the domain has been raised?
- What is the best way to implement DDD for Web API project?