Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void MediatorRegister()
- {
- _mediator.Register(
- MediatorMessages.CreateOrderToSupplierSave, param =>
- {
- // Create order code.
- });
- }
- private void CreateOrder()
- {
- _mediator.NotifyColleagues(MediatorMessages.CreateOrderToSupplierSave, "");
- }
- public static readonly object CreateOrderToSupplierSave = "CreateOrderToSupplierSave";
- __________________________________________________________________________________
- public class Mediator : DispatcherObject
- {
- #region Fields
- readonly MessageToActionsMap _messageToCallbacksMap;
- #endregion // Fields
- #region Constructor
- public Mediator()
- {
- if (base.Dispatcher == null)
- throw new InvalidOperationException("Cannot create Mediator on a thread that does not have a Dispatcher.");
- _messageToCallbacksMap = new MessageToActionsMap();
- }
- #endregion // Constructor
- #region Methods
- /// <summary>
- /// Method that register class for listening for message
- /// </summary>
- /// <param name="message">Message name - list of them are inside MediatorMessages.cs</param>
- /// <param name="callback">Action - method for writing parameter to some variable or property</param>
- public void Register(object message, Action<object> callback)
- {
- if (message == null)
- throw new ArgumentNullException("message");
- if (callback == null)
- throw new ArgumentNullException("callback");
- if (callback.Target == null)
- throw new ArgumentException("The 'callback' delegate must reference an instance method.");
- _messageToCallbacksMap.AddAction(message, callback);
- }
- /// <summary>
- /// Method sends message with parameter to listeners
- /// </summary>
- /// <param name="message">Message name from MediatorMessages.cs</param>
- /// <param name="parameter">Value that was send</param>
- public void NotifyColleagues(object message, object parameter)
- {
- if (base.CheckAccess())
- {
- List<Action<object>> actions =
- _messageToCallbacksMap.GetActions(message);
- if (actions != null)
- actions.ForEach(action => action(parameter));
- }
- else
- {
- base.Dispatcher.BeginInvoke((Action)delegate
- {
- this.NotifyColleagues(message, parameter);
- },
- DispatcherPriority.Send);
- }
- }
- #endregion // Methods
- }
- public class MessageToActionsMap
- {
- #region Fields
- readonly Dictionary<object, List<WeakAction>> _map;
- #endregion // Fields
- #region Constructor
- internal MessageToActionsMap()
- {
- _map = new Dictionary<object, List<WeakAction>>();
- }
- #endregion // Constructor
- #region Methods
- /// <summary>
- /// Creates new connection of message and action
- /// </summary>
- /// <param name="message">Message name</param>
- /// <param name="callback">Action</param>
- internal void AddAction(object message, Action<object> callback)
- {
- if (message == null)
- throw new ArgumentNullException("message");
- if (callback == null)
- throw new ArgumentNullException("callback");
- if (!_map.ContainsKey(message))
- _map[message] = new List<WeakAction>();
- _map[message].Add(new WeakAction(callback));
- }
- /// <summary>
- /// Gets list of actions
- /// </summary>
- /// <param name="message">Message name</param>
- /// <returns>List of actions binded to message</returns>
- internal List<Action<object>> GetActions(object message)
- {
- if (message == null)
- throw new ArgumentNullException("message");
- if (!_map.ContainsKey(message))
- return null;
- List<WeakAction> weakActions = _map[message];
- List<Action<object>> actions = new List<Action<object>>();
- for (int i = weakActions.Count - 1; i > -1; --i)
- {
- WeakAction weakAction = weakActions[i];
- if (!weakAction.IsAlive)
- weakActions.RemoveAt(i);
- else
- actions.Add(weakAction.CreateAction());
- }
- this.RemoveMessageIfNecessary(weakActions, message);
- return actions;
- }
- /// <summary>
- /// Removes actions for message
- /// </summary>
- /// <param name="weakActions">Action</param>
- /// <param name="message">Message name</param>
- void RemoveMessageIfNecessary(List<WeakAction> weakActions, object message)
- {
- if (weakActions.Count == 0)
- _map.Remove(message);
- }
- #endregion // Methods
- }
- internal class WeakAction : WeakReference
- {
- readonly MethodInfo _method;
- internal WeakAction(Action<object> action)
- : base(action.Target)
- {
- _method = action.Method;
- }
- internal Action<object> CreateAction()
- {
- if (!base.IsAlive)
- return null;
- try
- {
- // Rehydrate into a real Action
- // object, so that the method
- // can be invoked on the target.
- return Delegate.CreateDelegate(
- typeof(Action<object>),
- base.Target,
- _method.Name)
- as Action<object>;
- }
- catch
- {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement