Eventbus implementation, publish subscriptions, and XML loading

Source: Internet
Author: User
Tags eventbus

Affected by CQRS, wrote a eventbus, can implement the Publish subscription mode execution event, in the DDD model, you can use the following code to trigger the event:

Eventbus bus = Eventbus.instance ();
Bus. Publish (New Orderaddedevent ());

The solution structure is as follows, which is simple and understandable:

The XML Event configuration code sample is as follows (1 event can define multiple subscribers, such as the following):

<?xml version= "1.0" encoding= "Utf-8"?> <Events> <Event> <publishevent>consoleapplication 2.code. events.orderaddedevent</publishevent> <SubscribedEvents> <subscribedevent>consoleapplicati On2.code.EventHandlers.OrderAdde deventhandler_cacheprepare</subscribedevent> <subscribedevent>cons OleApplication2.code.EventHandlers.OrderAdde deventhandler_pdfgenerate</subscribedevent> </ subscribedevents> </Event> <Event> <publishevent>consoleapplication2.code.events.orderdelet Edevent&l t;/publishevent> <SubscribedEvents> <subscribedevent>consoleapplication2.code.ev Enthandlers.orderdele tedeventhandler_notifyclient</subscribedevent> <subscribedevent>consoleapplica Tion2.code.EventHandlers.OrderDele tedeventhandler_removcache</subscribedevent> </SubscribedEvents> & Lt;/event> </Events>

Let's start by defining the event:

public class BaseEvent         //empty, no need to implement other methods
{
} public
class Orderaddedevent:baseevent      //order has been added, triggering

Event
{
} public
class Orderdeletedevent:baseevent     //order deleted, triggered

event {public
        Guid OrderId { Get Set
        orderdeletedevent (Guid orderId) {This
            . OrderId = OrderId;
        }

To define handlers for an event:

public interface ieventhandler<t>
        where t:baseevent
{
        void Handle (T evt);
}
public class Orderdeletedeventhandler_notifyclient: 

ieventhandler<orderdeletedevent>
{
        public void Handle (orderdeletedevent evt)
        {
            Console.WriteLine ("[Order deleted] notified.");
        }

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.