12 Using entity events to solve business problems
- Understanding what types of events work well with EF Core
- Using domain events to trigger extra business rules
- Using integration events to synchronize two parts of your application
- Implementing an Event Runner and then improving it
In software the term event covers a wide range of architectures and patterns, in general it means ‘action A triggers action B’. You have seen some C# events in the last chapter, such as events when an entity state changes (section 11.4.4). But this chapter is about another, quite difference type of event I call entity events because they are held in your entity classes. They are more like putting a message in the entity class for someone to read later.
The purpose of these entity events is to trigger business logic when something changes in an entity class. One example you will see later shows a change an address’s details causes the sales tax on a quote to be updated. This works by the entity class detecting a change to the address details and sending an entity event (message) which causes some business logic to run that will update the sales tax for quotes at that address.
12.1 Using events to solve business problems
12.1.1 Example of using domain events
12.1.2 Example of integration events
12.2 Defining where domain events and integration events are useful
12.3 Where might you use events with EF Core?
12.3.1 PRO: Follows a separation of concerns design principal
12.3.2 PRO: The design is robust around database updates
12.3.3 CON: Makes your application more complex
12.3.4 CON: Makes following the flow of the code more difficult
12.4 Implementing a domain event system with EF Core
12.4.1 You create some domain events classes to be triggered
12.4.2 Add code to the entity classes to hold the domain events
12.4.3 Alter the entity class to detect a change we want to trigger an event on
12.4.4 Create event handlers that are matched to the domain events
12.4.5 Build an Event Runner that finds and runs the correct event handler
12.4.6 Override SaveChanges and insert the Event Runner before SaveChanges is called
12.4.7 Register the Event Runner and all the event handlers
12.5 Implementing an integration event system with EF Core
12.5.1 Building a service which communicates with the warehouse
12.5.2 Overriding SaveChanges to handle the integration event
12.6 Improving the domain event and integration event implementations
12.6.1 Generalizing events: running before, during, and after the call to SaveChanges
12.6.2 Adding support for async event handlers
12.6.3 Handling multiple event handers for the same event
12.6.4 Handling event sagas where one event kicks off another event
12.7 Summary