Event Broker: collaborate between objects by publishing Event sources and subscribing to Event sources

Source: Internet
Author: User

In practical applications, we can use EventBroker to publish and subscribe to Event Notifications to implement communication between objects. Generally, EventBroker is implemented as the singleton mode. In the project, we use MEF to implement the singleton mode of EventBroker:

public class EventSubscriptionToken{    private EventSubscriptionToken(){}    public static EventSubscriptionToken GetNewToken()    {        var guid = Guid.NewGuid().ToString();        var token = new EventSubscriptionToken {Value = guid};        return token;    }    public string Value { get; private set; }}public interface IEventBroker{    void Publish<T>(T eventArgs);    EventSubscriptionToken Subscribe<T>(Action<T> callback);    bool Unsubscribe(EventSubscriptionToken subscriptionToken);}[Export(typeof(IEventBroker))]public class EventBroker : IEventBroker{    private readonly IDictionary<string, object> _callbacksDictionary;    [ImportingConstructor]    public EventBroker()    {        _callbacksDictionary = new Dictionary<string, object>();    }    public void Publish<T>(T eventArgs)    {        var callbacksToBeInvoked = _callbacksDictionary.Where(x => x.Value is Action<T>);        foreach (var callback in callbacksToBeInvoked)        {            var actionCallback = (Action<T>)callback.Value;            try            {                actionCallback(eventArgs);            }            catch (Exception)            { }        }    }    public EventSubscriptionToken Subscribe<T>(Action<T> callback)    {        var token = EventSubscriptionToken.GetNewToken();        _callbacksDictionary.Add(token.Value, callback);        return token;    }    public bool Unsubscribe(EventSubscriptionToken subscriptionToken)    {        return _callbacksDictionary.Remove(subscriptionToken.Value);    }}

Event subscription instance:

_eventBroker.Subscribe<ModelNameChangedEvent>(EntityChangedEventHandler);private void EntityChangedEventHandler(ModelNameChangedEvent obj){    ...}

The subscriber of an event does not need to know which object the event was published. Both parties interact through parameters. The Subscriber performs corresponding operations based on the input parameter object to complete the collaboration between objects.

Event release instance:

_eventBroker.Publish(new ModelNameChangedEvent(entity.ID, entity.SpaceType, entity.Name));   

When interaction is required, events are published through parameter objects.

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.