title: Domain Driven Design (Domain Events) for ASP.NET Web API
body: <p>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.</p>
  <pre><code>public class MyApiController : ApiController
  {
      // POST: api/myapi
      public void Post([FromBody]string value)
      {
          DomainEvents.Raise(new HelloMessage(value));
      }
  }
  </code></pre>
  <p>This raises the events handler:</p>
  <pre><code>public void HelloMessagesHandler : IDomainEventHandler&lt;HelloMessage&gt;
  {
      public void Handle(HelloMessage @event)
      {
          Log.Write($&quot;Hello {@event.Value}&quot;);
      {
  {
  </code></pre>
  <p>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.</p>
  <p>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:</p>
  <pre><code>public void HelloMessagesHandler : IDomainEventHandler&lt;HelloMessage&gt;
  {
      private readonly IService _service;
  
      public HelloMessagesHandler (IService service)
      {
          _service = service;
      {
  
      public void Handle(HelloMessage @event)
      {
          var response = _service.DoSomethingWithLongProcess(@event.Value);
          // return response &lt;-- to MyApiController
      {
  {
  </code></pre>
  <p>My questions are:</p>
  <ol>
  <li>How do I do this in simple and better way?</li>
  <li>How to retrieve back the response from the 3rd party web API to MyApiController after the domain has been raised?</li>
  <li>What is the best way to implement DDD for Web API project?</li>
  </ol>