.net implementation of communication between aggregate roots in different bounded contexts

This is the first time I have applied DDD concepts to a real problem.

I started with only 1 bounded context as the project is relatively small. However, I found myself with classes that are almost identical, that is, very similar names, very similar properties, but different behavior. I'm starting to think that they really belong to different bounded contexts, since the entities are the same and have different meanings in a different context. This is supported by the fact that the application basically has two completely different user groups.

I did a little bit of work on how two objects in different bounded contexts can interact with each other. I think I understand the concept ... but have no idea how to implement it? Is there a .net example somewhere? those. an aggregate root in one bounded context publishing an event for an aggregate root in another bounded context? as well as an aggregate root, causing an aggregate root in another bounded context.

And should each bounded context have its own: service level? storage and data layer?

+3


source to share


2 answers


It might help, this is a quote from "Implementing a Domain Driven Project" by Vaughn Vernon.

"Perhaps the most common use of domain events is when an aggregate creates an event and publishes it. The publisher is in the model module, but does not model any aspect. Rather, it provides a simple service for aggregates to notify subscribers of events."

Events are published using the service, and the implementation of the handler depends on a reasonable amount of time during which the rest of the model will eventually agree. Given my specific requirements, this is acceptable for a small time delay. My domain events are published to a queue using MSMQ, and then in an external process that I read from the queue and prepare the job. This project allows me to offload this work to an external host and free up IIS. I use the same mechanism to save changes to the repository. When the transaction in my aggregate is complete, I post the event to MSMQ and have 2 queues in the multicast. One queue for handling extra work and another for persistence.

If you haven't read this yet, I highly recommend this book. I'm sure my design will bring some criticism, but your implementation will depend on your requirements and how comfortable you are with the possible consistency.



Hope this helps, and if you decide to use MSMQ, here is the link to get you started. http://msdn.microsoft.com/en-us/library/aa480407.aspx

Here is my implementation of a domain event publisher.

public class DomainEventPublisher
{
    string DomaineEventMessageQueue = @"FormatName:MULTICAST=234.1.1.1:8001";

    public void PublishEvent(DomainEvent domainEvent, string correlationId)
    {
        MessageQueue eventQueue;

        eventQueue = new MessageQueue(DomaineEventMessageQueue);

        Message message = DomainEventMessage.CreateDomainEventMessage(domainEvent);

        message.CorrelationId = correlationId;

        eventQueue.Send(message);
    }

}

      

+2


source


So, I have 4 possibilities for you.

  • Revisit your domain model. The fact that you need to communicate between aggregates may indicate a problem with the model. The key point here is that everything in the bounded context remains consistent, but there are no guarantees between aggregates.

  • Use a process manager (or SAGA). Similar to the above answer, you can respond to domain events and coordinate cross-aggregate communication from the process manager.

  • Introduce a service: Be careful with this as you want to set premium values ​​to "no" or "few" on your domain. Having said that, this may be the right or, if not the most pragmatic way to solve the problem.

  • Change your perspective: do you already have information from another aggregate before interacting? If this can be reported with any command eliminating the need for a message?



I'll go into much more detail in my blog on the topic. If you find it useful, you can jump to it from here: 4 Secrets of Mutual Collections of Messages in the Event Sources System

+2


source







All Articles