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