Nope this is NOT a bad idea, in fact this is a great idea, let me explain.
First consider your bounded contexts, in reality they should know nothing about each other, even when multiple Contexts
are working together to create a complete solution, all a context knows
about is itself, its OWN concerns.
Take an OnboardingContext
responsible for when a new employee starts at a company, here an Employee
entity is added for the first time into the system. Here the employee has a Name, Phonenumber, Start date, Address, Marital Status and more.
Consider the PayrollContext
, it too has an Employee
entity but here all this entity has is an Id, a Salary, Start Date and End Date - here it doesn't care about the address, or marital status, it doesn't even necessarily care about the Name, becuase in THIS context name is not important, only the Salary, Start Date and End Date.
So if the two contexts, shouldn't know about each other, but maybe care about some information relating to both, how do we get the fact that a new Employee
has started and needs to get paid?
Domain Events
Domain events are used with distributed systems. The model of course, will become more complex, but also more scalable. Domain Events are used in an event driven architecture
Heres how it might work
- A new employee starts and is added to the system in the
OnboardingContext
, everything is correct and the model is persisted successfully.
- The
OnboardingContext
raises an event, the event is called NewEmployeeEvent
Thats it for the OnboardingContext
as far as it is concerned it is done. It's handled the new employee, saved it, and raised a nifty system wide event to say that something (an event) has occured.
Now to the PayrollContext
- The
PayrollContext
is interested in a couple of things, in particular it want's to know when a new emplyee starts.
- It subscribes to the
NewEmployeeEvent
this event is of a common type, it doesn't know where the event comes from, in fact it could come from anywhere, but it is interested in a little bag of data, or inormation on that event.
- When the event is raised this context
handles
that event, in this case grabbing pertinent info about the Salary and Employee Number, it persists this to it's own data store for use later during payroll.
Boom done.
Your system now listens to and responds to events, these flow throughout your system, in any direction, in all directions.
When something of interest happens and event is raised, anyone (context) interested in that event subscribes, handles and does what it want's with the data relating to that event.
So how do you do this?
There is lots of reading to do, just google DDD and Domain Events.
You will come across a lot of articles from Jimmy Bogard (a better domain events pattern) and Udi Dahan (Domain Events Salvation) on the subject
Take a look at nservicebus (paid for) and masstransit (open source) they are fantastic out of the box eventing, and messaging systems.
Nservice bus has some great videos on the subject at https://docs.particular.net/nservicebus/architecture/principles