Domain events, abpdomain
In C #, a class can define its exclusive event, and other classes can register and listen to this event. When an event is triggered, an event notification can be obtained. This is useful for desktop applications or independent Windows Services. However, it may be a problem for Web applications because objects are created based on requests and their lifecycles are short. It is difficult for us to register events of other categories. Similarly, directly registering events of other categories also results in coupling between classes.
In application systems, domain events are used to decouple and reuse commercial logic.
Event Bus
The event bus is a singleton object that is shared by all other classes and can be used to trigger and process events. To use this event bus, you need to reference it. You can achieve this in two ways:
Getting the default instance)
You can directly use EventBus. Default. It is a global event bus and can be used as follows:
EventBus. Default. Trigger (...); // Trigger the event
Injecting IEventBus)
In addition to using EventBus. Default, you can also use the dependency injection (DI) method to obtain the IEventBus reference. This facilitates unit testing. Here, we use the attribute injection paradigm:
public class TaskAppService : ApplicaService { public IEventBus EventBus { get; set; } public TaskAppService() { EventBus = NullEventBus.Instance; } }
Injection event bus, which is more suitable for property injection than constructor injection. An event is described by a class and the event object inherits from EventData. Suppose we want to trigger an event after a task is completed:
public class TaskCompletedEventData : EventData { public int TaskId { get; set; } }
All the attributes of this class are required by the class to process events. The EventData class defines the EventSource (the object triggers this event) and EventTime (when to trigger) attributes.
Define events
ABP defines the AbpHandledExceptionData event and automatically triggers this event when an exception occurs. This is what you want to get