Advertisement
Guest User

Messenger C#

a guest
Jan 23rd, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace MvvmBase.MessengerPattern
  6. {
  7.     ///<inheritdoc />
  8.     public class Messenger : IMessenger
  9.     {
  10.         private static Messenger _instance;
  11.         private static readonly object LockObject = new object();
  12.         private readonly Dictionary<Type, List<ActionIdentifier>> _references;
  13.  
  14.         private Messenger()
  15.         {
  16.             _references = new Dictionary<Type, List<ActionIdentifier>>();
  17.         }
  18.  
  19.         public static Messenger Instance
  20.         {
  21.             get
  22.             {
  23.                 lock (LockObject)
  24.                 {
  25.                     return _instance ?? (_instance = new Messenger());
  26.                 }
  27.             }
  28.         }
  29.  
  30.         ///<inheritdoc />
  31.         public void Register<TNotification>(object recipient, Action<TNotification> action)
  32.         {
  33.             Register(recipient, null, action);
  34.         }
  35.  
  36.         ///<inheritdoc />
  37.         public void Register<TNotification>(object recipient, string identCode, Action<TNotification> action)
  38.         {
  39.             var messageType = typeof(TNotification);
  40.  
  41.             if (!_references.ContainsKey(messageType))
  42.                 _references.Add(messageType, new List<ActionIdentifier>());
  43.  
  44.             var actionIdent = new ActionIdentifier
  45.             {
  46.                 Action = new WeakReferenceAction<TNotification>(recipient, action),
  47.                 IdentificationCode = identCode
  48.             };
  49.  
  50.             _references[messageType].Add(actionIdent);
  51.         }
  52.  
  53.         ///<inheritdoc />
  54.         public void Send<TNotification>(TNotification notification)
  55.         {
  56.             var type = typeof(TNotification);
  57.             var typeActionIdentifiers = _references[type];
  58.  
  59.             foreach (var ai in typeActionIdentifiers)
  60.                 if (ai.Action is IActionParameter actionParameter)
  61.                     actionParameter.ExecuteWithParameter(notification);
  62.                 else
  63.                     ai.Action.Execute();
  64.         }
  65.  
  66.         ///<inheritdoc />
  67.         public void Send<TNotification>(TNotification notification, string identCode)
  68.         {
  69.             var type = typeof(TNotification);
  70.             var typeActionIdentifiers = _references[type];
  71.             foreach (var ai in typeActionIdentifiers)
  72.                 if (ai.IdentificationCode == identCode)
  73.                 {
  74.                     if (ai.Action is IActionParameter actionParameter)
  75.                         actionParameter.ExecuteWithParameter(notification);
  76.                     else
  77.                         ai.Action.Execute();
  78.                 }
  79.         }
  80.  
  81.         ///<inheritdoc />
  82.         public void Unregister<TNotification>(object recipient)
  83.         {
  84.             var lockTaken = false;
  85.             try
  86.             {
  87.                 Monitor.Enter(_references, ref lockTaken);
  88.                 foreach (var targetType in _references.Keys)
  89.                     foreach (var wra in _references[targetType])
  90.                         if (wra.Action != null && wra.Action.Target == recipient)
  91.                             wra.Action.Unload();
  92.             }
  93.             catch (Exception e)
  94.             {
  95.                 Console.WriteLine(e);
  96.             }
  97.             finally
  98.             {
  99.                 if (lockTaken)
  100.                     Monitor.Exit(_references);
  101.             }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement