using System; namespace CRM.Core.Common.EventBus { /// /// Represents the event handler which delegates the event handling process to /// a given delegated method. /// /// The type of the event to be handled by current handler. public sealed class ActionDelegatedEventHandler : IEventHandler where TEvent : IEvent { #region Private Fields private readonly Action action; #endregion #region Ctor /// /// Initializes a new instance of ActionDelegatedEventHandler{TEvent} class. /// /// The instance that delegates the event handling process. public ActionDelegatedEventHandler(Action action) { this.action = action; } #endregion #region Public Methods /// /// Returns a value which indicates whether the current /// ActionDelegatedEventHandler{T} equals to the given object. /// /// The which is used to compare to /// the current ActionDelegatedEventHandler{T} instance. /// If the given object equals to the current ActionDelegatedEventHandler{T} /// instance, returns true, otherwise, false. public override bool Equals(object obj) { if (ReferenceEquals(this, obj)) return true; if (obj == null) return false; ActionDelegatedEventHandler other = obj as ActionDelegatedEventHandler; if (other == null) return false; return Delegate.Equals(this.action, other.action); } #endregion #region IHandler Members /// /// Handles the specified message. /// /// The message to be handled. public void Handle(TEvent message) { action(message); } #endregion } }