2

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?
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Riza Marhaban
  • 368
  • 6
  • 20

1 Answers1

0

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.

IlliakaillI
  • 1,510
  • 13
  • 25
  • Thanks for the highlight. The reason I show a simple example is because I need to now the fundamental on how to do it. Any simple example for using DDD in ASP.NET Web API with one controller, perhaps a link or GitHub would be fine for me to learn. – Riza Marhaban Jan 20 '17 at 04:43
  • DDD has nothing to do with Web API specifically. You will be calling methods of the Application Services layer from your controllers, but there is nothing special about ASP.NET Web API here. Consider taking a look at Jeffrey Palermo's series of articles about ONION with examples in C# http://jeffreypalermo.com/blog/the-onion-architecture-part-2/ which may be exactly what you need. – IlliakaillI Jan 20 '17 at 04:52
  • I'm aware of ASP.NET Web API that uses DI/IoC, CQRS, etc. I would like to know more using DDD concept for Web API. I'm not a beginner and I'm learning DDD for ASP.NET Web API best practice. And no, not the Onion for me to use. I want to use DDD. – Riza Marhaban Jan 20 '17 at 05:01
  • There is no such thing as "best practice of DDD for Web API". Those two things may be used together, however, there is nothing special about Web API that will influence the way you design your DDD application, especially if you are not a beginner as you mentioned. Jeffrey's article is just about using DDD in a web application, so please consider taking a closer look. – IlliakaillI Jan 20 '17 at 05:37
  • This is what I've found regarding a specific example that you've been asking about: https://github.com/robzhu/Library.WebApi It is not without its flaws, but you can get the rough idea about how people are doing it. However, I didn't notice any domain events there, unfortunately. – IlliakaillI Jan 20 '17 at 05:50
  • Thanks for the link – Riza Marhaban Jan 20 '17 at 05:57