Part 7: Domains
...
- explain domains
- show how to add "domains" to facade
In this part, we will explain the concept of domains and show how to add "domains" to the facade. We will also show how to add new domains and write tests for them.
What is a Domain?
In the CQRS pattern, the architecture of the application is often divided based on domains. Each domain represents a distinct area of business logic and data requirements. The concept of domains in CQRS is closely related to Domain-Driven Design (DDD), a methodology that emphasizes the importance of a deep understanding of the domain to create effective software solutions.
Create new Domain
How to add to Facade?
// in main.go
ctx := context.Background()
fc := facade.NewFacade(...)
// register all events from all domains to the Facade
if err := anyDomainFirst.RegisterEvents(ctx, fc); err != nil {
panic(err)
}
...
if err := anyDomainLast.RegisterEvents(ctx, fc); err != nil {
panic(err)
}
// register all Command- and EventHandler to the Facade
if err := anyDomainFirst.Register(ctx, fc); err != nil {
panic(err)
}
...
if err := anyDomainLast.Register(ctx, fc); err != nil {
panic(err)
}
To be absolutely sure that we do not create race conditions, we first register all events from all domains and only then do we register the command and event handlers. This can be important because the ReadModels within a domain could start the loading process independently and listen for events from external domains, which is very often the case in practice.