Search code examples
c#asp.netapidomain-driven-designdomain-events

Domain Driven Design (Domain Events) for ASP.NET Web API


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:

  1. How do I do this in simple and better way?
  2. How to retrieve back the response from the 3rd party web API to MyApiController after the domain has been raised?
  3. What is the best way to implement DDD for Web API project?

Solution

  • How to retrieve back the response from the 3rd party web API to MyApiController after the domain has been raised?

    What you have demonstrated in your example is a typical publish-subscribe pattern. You can not return data from event handlers because it defies the whole purpose of "fire and forget" approach. Part of application that raises event should not be dependent on parts which may consume the event. An Event is an indication of something that already happened, therefore after publishing an event, you should just do nothing most of the time.

    What is the best way to implement DDD for Web API project?

    There is no "best way" to do that. Every decision has its pros and cons. Why have you decided to use DDD in the first place? Consider giving a real-world example.

    How do I do this in simple and better way?

    There is nothing complicated about the code that you've shown. In order to simplify it even more, you can call the method of your service directly from the controller, there is no need for the domain event, at least in that example above.

    Another thing to mention is that "Hello" looks more like a command, because events usually formulated in the past tense, e.g. "HelloHappened". Some real-world scenario would be more desirable because now it is pretty hard to understand what your problem is about.

    Take a look at Jeffrey Palermo's series of articles about ONION with examples in C# which may be exactly what you need.