Domain events, abpdomain

Source: Internet
Author: User
Tags eventbus

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.