Advertisement
Guest User

Mediator helper

a guest
Feb 20th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.07 KB | None | 0 0
  1. public void MediatorRegister()
  2.         {
  3. _mediator.Register(
  4.                 MediatorMessages.CreateOrderToSupplierSave, param =>
  5.                 {
  6.                    
  7.                     // Create order code.
  8.                 });
  9. }
  10. private void CreateOrder()
  11.         {
  12.             _mediator.NotifyColleagues(MediatorMessages.CreateOrderToSupplierSave, "");
  13.         }
  14.  
  15. public static readonly object CreateOrderToSupplierSave = "CreateOrderToSupplierSave";
  16.  
  17.  
  18. __________________________________________________________________________________
  19.  
  20.  
  21.  
  22.  public class Mediator : DispatcherObject
  23.     {
  24.         #region Fields
  25.  
  26.         readonly MessageToActionsMap _messageToCallbacksMap;
  27.  
  28.         #endregion // Fields
  29.  
  30.         #region Constructor
  31.  
  32.         public Mediator()
  33.         {
  34.             if (base.Dispatcher == null)
  35.                 throw new InvalidOperationException("Cannot create Mediator on a thread that does not have a Dispatcher.");
  36.             _messageToCallbacksMap = new MessageToActionsMap();
  37.         }
  38.  
  39.         #endregion // Constructor
  40.  
  41.         #region Methods
  42.         /// <summary>
  43.         /// Method that register class for listening for message
  44.         /// </summary>
  45.         /// <param name="message">Message name - list of them are inside MediatorMessages.cs</param>
  46.         /// <param name="callback">Action - method for writing parameter to some variable or property</param>
  47.         public void Register(object message, Action<object> callback)
  48.         {
  49.             if (message == null)
  50.                 throw new ArgumentNullException("message");
  51.  
  52.             if (callback == null)
  53.                 throw new ArgumentNullException("callback");
  54.  
  55.             if (callback.Target == null)
  56.                 throw new ArgumentException("The 'callback' delegate must reference an instance method.");
  57.  
  58.             _messageToCallbacksMap.AddAction(message, callback);
  59.         }
  60.         /// <summary>
  61.         /// Method sends message with parameter to listeners
  62.         /// </summary>
  63.         /// <param name="message">Message name from MediatorMessages.cs</param>
  64.         /// <param name="parameter">Value that was send</param>
  65.         public void NotifyColleagues(object message, object parameter)
  66.         {
  67.             if (base.CheckAccess())
  68.             {
  69.                 List<Action<object>> actions =
  70.                     _messageToCallbacksMap.GetActions(message);
  71.  
  72.                 if (actions != null)
  73.                     actions.ForEach(action => action(parameter));
  74.             }
  75.             else
  76.             {
  77.                 base.Dispatcher.BeginInvoke((Action)delegate
  78.                 {
  79.                     this.NotifyColleagues(message, parameter);
  80.                 },
  81.                 DispatcherPriority.Send);
  82.             }
  83.         }
  84.  
  85.         #endregion // Methods
  86.     }
  87. public class MessageToActionsMap
  88.     {
  89.         #region Fields
  90.  
  91.         readonly Dictionary<object, List<WeakAction>> _map;
  92.  
  93.         #endregion // Fields
  94.  
  95.         #region Constructor
  96.  
  97.         internal MessageToActionsMap()
  98.         {
  99.             _map = new Dictionary<object, List<WeakAction>>();
  100.         }
  101.  
  102.         #endregion // Constructor
  103.  
  104.         #region Methods
  105.         /// <summary>
  106.         /// Creates new connection of message and action
  107.         /// </summary>
  108.         /// <param name="message">Message name</param>
  109.         /// <param name="callback">Action</param>
  110.         internal void AddAction(object message, Action<object> callback)
  111.         {
  112.  
  113.             if (message == null)
  114.                 throw new ArgumentNullException("message");
  115.  
  116.             if (callback == null)
  117.                 throw new ArgumentNullException("callback");
  118.  
  119.             if (!_map.ContainsKey(message))
  120.                 _map[message] = new List<WeakAction>();
  121.  
  122.             _map[message].Add(new WeakAction(callback));
  123.         }
  124.         /// <summary>
  125.         /// Gets list of actions
  126.         /// </summary>
  127.         /// <param name="message">Message name</param>
  128.         /// <returns>List of actions binded to message</returns>
  129.         internal List<Action<object>> GetActions(object message)
  130.         {
  131.             if (message == null)
  132.                 throw new ArgumentNullException("message");
  133.  
  134.             if (!_map.ContainsKey(message))
  135.                 return null;
  136.  
  137.             List<WeakAction> weakActions = _map[message];
  138.             List<Action<object>> actions = new List<Action<object>>();
  139.             for (int i = weakActions.Count - 1; i > -1; --i)
  140.             {
  141.                 WeakAction weakAction = weakActions[i];
  142.                 if (!weakAction.IsAlive)
  143.                     weakActions.RemoveAt(i);
  144.                 else
  145.                     actions.Add(weakAction.CreateAction());
  146.             }
  147.  
  148.             this.RemoveMessageIfNecessary(weakActions, message);
  149.  
  150.             return actions;
  151.         }
  152.         /// <summary>
  153.         /// Removes actions for message
  154.         /// </summary>
  155.         /// <param name="weakActions">Action</param>
  156.         /// <param name="message">Message name</param>
  157.         void RemoveMessageIfNecessary(List<WeakAction> weakActions, object message)
  158.         {
  159.             if (weakActions.Count == 0)
  160.                 _map.Remove(message);
  161.         }
  162.  
  163.         #endregion // Methods
  164.     }
  165. internal class WeakAction : WeakReference
  166.     {
  167.         readonly MethodInfo _method;
  168.  
  169.         internal WeakAction(Action<object> action)
  170.             : base(action.Target)
  171.         {
  172.             _method = action.Method;
  173.         }
  174.  
  175.         internal Action<object> CreateAction()
  176.         {
  177.             if (!base.IsAlive)
  178.                 return null;
  179.  
  180.             try
  181.             {
  182.                 // Rehydrate into a real Action
  183.                 // object, so that the method
  184.                 // can be invoked on the target.
  185.  
  186.                 return Delegate.CreateDelegate(
  187.                     typeof(Action<object>),
  188.                     base.Target,
  189.                     _method.Name)
  190.                     as Action<object>;
  191.             }
  192.             catch
  193.             {
  194.                 return null;
  195.             }
  196.         }
  197.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement