C # Event Event aggregator

Source: Internet
Author: User

The event aggregator is used to centrally manage events ' subscriptions (Subscribe) and processing (Handle), and to use event aggregators, it is first understood that the event is essentially a class.

Traditional + = and-= deficiencies:

1, management is very troublesome, 2, inconvenient to expand.

So try to use event aggregator to solve this problem.

First, use a unified interface to implement the unified markup for the event:

public interface ievent{  }

event, you need to have a corresponding event handler (EventHandler), which is unified using a unified interface Ieventhandler, Ieventhandler contains only one method for handling events Handle

public interface Ieventhandler<in tevent> where tevent:class, ievent{    void Handler (Tevent @event);}

Event aggregator Interface Ieventaggregator needs to contain at least two methods: Subscription (Subscribe) and release (Publish)

public interface ieventaggregator{    void subscribe<tevent> (ieventhandler<tevent> handler) where Tevent:class, IEvent;    void publish<tevent> (Tevent @event) where Tevent:class, IEvent;}

The next step is to implement this interface: Eventaggregator

Example:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceeventaggregatortest{ Public InterfaceIEvent {} Public Interfaceieventhandler<inchTevent>whereTevent:class, IEvent {voidHandler (tevent @event); }     Public InterfaceIeventaggregator {voidSubscribe<tevent> (ieventhandler<tevent> handler)whereTevent:class, IEvent; voidPublish<tevent> (tevent @event)whereTevent:class, IEvent; }     Public classEventaggregator:ieventaggregator {//all the event and the corresponding handler are registered in this dictionary .        Private ReadOnlyDictionary<type, list<Object>> _eventhandlers =NewDictionary<type, list<Object>>();  Public voidSubscribe<tevent> (ieventhandler<tevent> handler)whereTevent:class, IEvent {varEventType =typeof(tevent); if(_eventhandlers.containskey (EventType)) {varHandlers =_eventhandlers[eventtype]; if(Handlers! =NULL) {handlers.                ADD (handler); }                Else{handlers=Newlist<Object>{handler}; }            }            Else{_eventhandlers.add (EventType,Newlist<Object>{handler}); }        }         Public voidPublish<tevent> (tevent @event)whereTevent:class, IEvent {varEventType =@event.            GetType (); if(_eventhandlers.containskey (EventType)&& _eventhandlers[eventtype]! =NULL&& _eventhandlers[eventtype]. Count >0)            {                varHandlers =_eventhandlers[eventtype]; foreach(varHandlerinchhandlers) {                    varEventHandler = Handler asIeventhandler<tevent>; EventHandler?.                Handler (@event); }            }        }    }     Public classmealdoneevent:ievent { Public stringDishname {Get;Private Set; }  PublicMealdoneevent (stringdishname) {Dishname=Dishname; }    }     Public classMealdoneeventhandler:ieventhandler<mealdoneevent>    {        Private ReadOnly string_name;  PublicMealdoneeventhandler (stringname) {_name=name; }        #regionImplementation of Ieventhandler<in Mealdoneevent> Public voidHandler (Mealdoneevent @event) {Console.WriteLine ($"{_name} is {@event}. Dishname}"); }        #endregion    }}

Call:

usingeventaggregatortest;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceeventaggregatordemo{classProgram {Static voidMain (string[] args) {            varEventaggregator =NewEventaggregator (); Eventaggregator.subscribe (NewMealdoneeventhandler ("Zhang San")); Eventaggregator.subscribe (NewMealdoneeventhandler ("John Doe")); Eventaggregator.subscribe (NewMealdoneeventhandler ("Harry")); Eventaggregator.publish (NewMealdoneevent ("have a big meal"));        Console.readkey (); }    }}

C # Event Event aggregator

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.