2

Initially, There is an app runs in Desktop, however, the app will run in web platform in the future.

There are some bounded contexts in the app and some of them needs to retrieve data from another. In this case, I don't know which approach I have to use for this case.

I thought of using mediator pattern that a bound context "A" requests data "X" and then mediator call another bound context, like B" " and gets the correct data "X". Finally, The mediator brings data "X" to BC "A".

This scenario will be change when the app runs in web, then I've thought of using a microservice requests data from another microservice using meaditor pattern too.

Do the both approaches are interest or there is another better solution?

Could anyone help me, please?

Thanks a lot!

Kadu
  • 343
  • 7
  • 17
  • Possible duplicate of [Communicating between two Bounded Contexts in DDD](https://stackoverflow.com/questions/16713041/communicating-between-two-bounded-contexts-in-ddd) – guillaume31 Oct 01 '18 at 08:25

4 Answers4

6

If you're retrieving data from other bounded contexts through either DB or API calls, your architecture might potentially fall to death star pattern because it introduces unwanted coupling and knowledge to the client context.

A better approach might be is looking at event-driven mechanisms like webhooks or message queues as a way of emitting data that you want to share to subscribing context(s). This is good because it reduces coupling of your bounded context(s) through data replication across contexts which results to higher bounded contexts independence.

This gives you the feeling of "Who cares if bounded context B is not available ATM, bounded context A and C have data they need inside them and I can resume syncing later since my data update related events are recorded on my queue"

fourpastmidnight
  • 4,032
  • 1
  • 35
  • 48
Allan Chua
  • 9,305
  • 9
  • 41
  • 61
  • Hi Allan! I understand you. I didn't know how to see the events can be right for my scenario neither publisher and subscriber too. The system has three different bounded context, including "importer", "data management" and "analyzer". Importer has responsability to read file data and pass these data to data management. The Analyzer has responsability to get data from Data Managment and perform different type of analysis. I dont know how to see that using events or publish/subscriber resolve that, because the the Analyzer has to get some data not all depending on user's choice. – Kadu Oct 07 '18 at 03:02
2

The answer to this question breaks down into two distinct areas:

  • the logical challenge of communicating between different contexts, where the same data could be used in very different ways. How does one context interpret the meaning of the data?
  • and the technical challenge of synchronizing data between independent systems. How do we guarantee the correctness of each system's behavior when they both have independent copies of the "same" data?

Logically, a context map is used to define the relationship between any bounded contexts that need to communicate (share data) in any way. The domain models that control the data are only applicable with a single bounded context, so some method for interpreting data from another context is needed. That's where the patterns from Evan's book come in to play: customer/supplier, conformist, published language, open host, anti-corruption layer, or (the cop-out pattern) separate ways.

Using a mediator between services can be though of as an implementation of the anti-corruption layer pattern: the services don't need to speak the same language, because there's an independent buffer between them doing the translation. In a microservice architecture, this could be some kind of integration service between two very different contexts.

From a technical perspective, direct API calls between services in different bounded contexts introduce dependencies between those services, so an event-driven approach like what Allan mentioned is preferred, assuming your application is okay with the implications of that (eventual consistency of the data). Picking a messaging platforms that gives you the guarantees necessary to keep the data in sync is important. Most asynchronous messaging protocols guarantee "at least once" delivery, but ordering of messages and de-duplication of repeats is up to the application.

Sometimes it's simpler to use a synchronous API call, especially if you find yourself doing a lot of request/response type messaging (which can happen if you have services sending command-type messages to each other).

A composite UI is another pattern that allows you to do data integration in the presentation layer, by having each component pull data from the relevant service, then share/combine the data in the UI itself. This can be easier to manage than a tangled web of cross-service API calls in the backend, especially if you use something like an IT/Ops service, NGINX, or MuleSoft's Experience API approach to implement a "backend-for-frontend".

aschuell
  • 71
  • 5
  • Hi Aschuell. The user can perform different analysis that user wants. In this case the user can select a specific analysis and data from data managment context and also the data from analyzer context that generated another analysis. Because of this, I don't see how to use events or publish/subscriber nither queue too. I would not retrive this data from data managment context because this data is used just a type of analysis not all and also because of amount of data. – Kadu Oct 07 '18 at 03:13
  • "Data management", "analysis" and "import" are not bounded contexts. They sound like utility services within a single bounded context. The importer could even be nothing more than a generic service -- not even part of your domain. Remember, a bounded context is nothing more than the solution space in which your domain models are applicable. Domain models implement your business logic in a OOP manner. I think what you need to do is figure out if the different types of analysis belong in different contexts, and divide the data management and analysis apps along those functional lines. – aschuell Oct 09 '18 at 14:31
  • Hi Aschuell. You right! I have different type of analysis belong in different contexts. – Kadu Oct 26 '18 at 10:53
1

What you need is a ddd pattern for integration. BC "B" is upstream, "A" is downstream. You could go for an OHS PL in upstream, and ACL in downstream. In practice this is a REST API upstream and an adapter downstream. Every time A needs the data from B , the adapter calls the REST API and adapts the info returned to A domain model. This would be sync. If you wanna go for an async integration, B would publish events to MQ with the info, and A would listen for those events and get the info.

choquero70
  • 4,470
  • 2
  • 28
  • 48
0

I want to add-on a comment about analysis in DDD. Exist e several approaches for sending data to analytic. 1) If you have a big enterprise application and you should collect a lot of statistic from all bounded context better move analytic in separate service and use a message queue for send data there. 2) If you have a simple application separate your Analytic from your App in other context and use an event or command to speak with there.