Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the domain event Domain events at the domain level in the ABP framework

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use Domain events about domain events in the ABP framework. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

In C #, a class can define its own event and other classes can register the event and listen, and get event notification when the event is triggered. This is very useful for desktop applications or stand-alone Windows Service. However, it can be a bit of a problem for Web applications because objects are created on request (request) and their lifecycles are short. It is difficult for us to register other categories of events. Similarly, directly registering other categories of events also creates coupling between classes.

In an application system, domain events are used to decouple and re-use business logic.

Event bus

The event bus is a singleton object that is shared by all other classes and through which events can be triggered and handled. To use this event bus, you need to reference it. You can do this in two ways:

Get the default instance (Getting the default instance)

You can use EventBus.Default directly. It is a global event bus and can be used in the following ways:

EventBus.Default.Trigger (...); / / trigger event

Injection IEventBus event Interface (Injecting IEventBus)

In addition to using EventBus.Default directly, you can also use dependency injection (DI) to get a reference to IEventBus. This facilitates unit testing. Here, we use the paradigm of attribute injection:

Public class TaskAppService: ApplicaService {public IEventBus EventBus {get; set;} public TaskAppService () {EventBus = NullEventBus.Instance;}}

Injection event bus, attribute injection is more suitable than constructor injection. The event is described by the 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;}}

This class contains properties that the class needs to handle events. The EventData class defines the EventSource (which object triggered the event) and the EventTime (when) properties.

Define event

ABP defines the AbpHandledExceptionData event and automatically triggers it when an exception occurs. This is especially useful if you want to get more information about exceptions (even if ABP has automatically logged all exceptions). You can register the event and set the trigger time when the exception occurs.

ABP also provides many common event data classes for entity changes: EntityCreatedEventData, EntityUpdatedEventData, and EntityDeletedEventData. They are defined in the Abp.Events.Bus.Entitis namespace. When an entity is added / updated / deleted, these events are automatically triggered by ABP. If you have a Person entity, you can register with EntityCreatedEventData, and the event will be triggered after the new Person entity is created and inserted into the database. These events also support inheritance. If the Student class inherits from the Person class and you register with EntityCreatedEventData, then you will receive a trigger when Person or Student is added.

Trigger event

Examples of triggered events are as follows:

Public class TaskAppService: ApplicationService {public IEventBus EventBus {get; set;} public TaskAppService () {EventBus = NullEventBus.Instance;} public void CompleteTask (CompleteTaskInput input) {/ / TODO: completed the task on the database EventBus.Trigger (new TaskCompletedEventData {TaskId = 42});}}

Here are some overloads of trigger methods:

EventBus.Trigger (new TaskCompletedEventData {TaskId = 42}); EventBus.Trigger (this, new TaskCompletedEventData {TaskId = 42}); EventBus.Trigger (typeof (TaskCompletedEventData), this, new TaskCompletedEventData {TaskId = 42})

Event handling

To handle events, you should implement the IEventHandler interface as follows:

Public class ActivityWriter: IEventHandler, ITransientDependency {public void HandleEvent (TaskCompletedEventData eventData) {WriteActivity ("A task is completed by id =" + eventData.TaskId);}}

EventBus has been integrated into the dependency injection system. As we implemented ITransientDependency in the example above, when the TaskCompleted event is triggered, it creates a new entity of the ActivityWriter class and calls its HandleEvent method, which in turn releases it. See dependency injection (DI) for details.

1. Handling of underlying events (Handling base events)

EventBus supports inheritance of events. For example, you can create TaskEventData and two inherited classes: TaskCompletedEventData and TaskCreatedEventData:

Public class TaskEventData: EventData {public Task Task {get; set;}} public class TaskCreatedEventData: TaskEventData {public User CreatorUser {get; set;}} public class TaskCompletedEventData: TaskEventData {public User CompletorUser {get; set;}}

However, you can implement IEventHandler to handle these two events:

Public class ActivityWriter: IEventHandler, ITransientDependency {public void HandleEvent (TaskEventData eventData) {if (eventData is TaskCreatedEventData) {...} else {...}

Of course, you can also implement IEventHandler to handle all events, if you really want to do so.

two。 Handle multiple events (Handling multiple events)

We can handle multiple events in a single processor (handler). At this point, you should implement IEventHandler for different events. Examples are as follows:

Public class ActivityWriter: IEventHandler, IEventHandler, ITransientDependency {public void HandleEvent (TaskCompletedEventData eventData) {/ / TODO: handling event} public void HandleEvent (TaskCreatedEventData eventData) {/ / TODO: handling event}}

Register processor

We must register the processor (handler) into the event bus to handle events.

1. Automatic Automatically

ABP scans all classes that implement the IEventHandler interface and automatically registers them to the event bus. When an event occurs, it takes the reference object of the processor (handler) through dependency injection (DI) and releases it after the event is processed. This is the more recommended way to use the event bus in ABP.

two。 Manual (Manually)

You can also register events manually, but there are some problems. In Web applications, events should be registered when the application starts. Register the event when a Web request (request) arrives, and repeat this behavior. This may cause some problems in your application because registered classes can be called multiple times. It is also important to note that manual registration cannot be used with the dependency injection system.

ABP provides overloading (overload) of multiple event bus registration methods. One of the easiest overloading methods is to wait for a delegate or Lambda.

EventBus.Register (eventData = > {WriteActivity ("A task is completed by id =" + eventData.TaskId);})

Therefore, the event: task completed will occur, and the Lambda method will be called. The second overloaded method waits for an object that implements IEventHandler:

Eventbus.Register (new ActivityWriter ())

The same example, if ActivityWriter is called because of an event. This method also has a non-generic overload. Another overload accepts two generalized parameters:

EventBus.Register ()

At this point, the event bus creates a new ActivityWriter for each event. When it is released, it calls the ActivityWriter.Dispose method.

Finally, you can register an event handler factory (event handler factory) to create the processor. The processor factory has two methods: GetHandler and ReleaseHandler. Examples are as follows:

Public class ActivityWriterFactory: IEventHandlerFactory {public IEventHandler GetHandler () {return new ActivityWriter ();} public void ReleaseHandler (IEventHandler handler) {/ / TODO: release ActivityWriter entity (processor)}}

ABP also provides a special factory class, IocHandlerFactory, and through the dependency injection system, IocHandlerFactory can be used to create or dispose processors. ABP can register IocHandlerFactory automatically. Therefore, if you want to use the dependency injection system, please use automatic registration directly.

Unregister event

When you register the event bus manually, you may want to unregister later. The easiest way to unregister an event is registration.Dispose (). Examples are as follows:

/ / register an event Var registration = EventBus.Register (eventData = > WriteActivity ("A task is completed by id =" + eventData.TaskId)); / / unregister an event registration.Dispose ()

Of course, cancellation of registration can be done anywhere and at any time. Save (keep) the registered object and dispose it when you want to unregister it. The overload of all registration methods returns a disposable object to unregister the event.

The event bus also provides a method for unregistering. Examples of use:

/ / create a processor var handler = new ActivityWriter (); / / register an event EventBus.Register (handler); / / unregister EventBus.Unregister (handler) for this event

It also provides overloaded methods for unregistered delegates and factories. The unregistered processor object must be the same as the previously registered object.

Finally, EventBus provides a UnregisterAll () method to unregister all handlers of an event, while the UnregisterAll () method is all handlers of all events.

This is the end of this article on "how to use domain events Domain events in the ABP framework". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report