12 Using entity events to solve business problems

 

This chapter

  • Understanding the types of events that 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. Generally, it means “Action A triggers action B.” You saw some C# events in chapter 11, such as events in which an entity state changes (section 11.4.4). But this chapter is about another, quite different type of event, which I call an entity event because it is held in your entity classes. Using an entity event is like putting a message in the entity class for someone to read later.

The purpose of entity events is to trigger business logic when something changes in an entity class. In section 12.1.1 I show an example where a change in an address’s details causes the sales tax on a quote to be updated. This example is implemented by detecting a change to the address details and sending an entity event (message) that runs some business logic that updates 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 the SoC design principle

12.3.2 Pro: Makes database updates robust

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 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 to trigger an event on