Exemple #1
0
        public void FinalizerEvent()
        {
            var  participants = new Participant[] {
                new Participant("Participant 1"),
                new Participant("Participant 2"),
                new Participant("Participant 3")
            };

            IMediator mediator = null;
            try {
                mediator = new Mediator();
                mediator.Register<ChatMessageEventArgs>(participants[0].InterceptMessage);
                mediator.RegisterFinalizer<ChatMessageEventArgs>(participants[2].OnRecieveMessage);

                var args = new ChatMessageEventArgs("Hello there!");
                mediator.Post(this, args);

                Assert.That(participants[0].HasInterceptedMessageOnce(this, new ChatMessageEventArgs("Hello there!")), Is.True);
                Assert.That(participants[2].HasReceivedMessageOnce(this, new ChatMessageEventArgs("Message has been intercepted.")), Is.True);

            } finally {
                for (int i = 0; i < participants.Length; i++) {
                    participants[i].Dispose();
                }

                var disposableMediator = mediator as IDisposable;
                if(disposableMediator != null) {
                    disposableMediator.Dispose();
                }
            }
        }
Exemple #2
0
 public void InterceptMessage(object sender, ChatMessageEventArgs args)
 {
     lock(_syncHandle) {
         var argClone = new ChatMessageEventArgs(args.Message);
         _onInterceptTracker.OnFired(sender, argClone);
         args.Message = "Message has been intercepted.";
     }
 }
Exemple #3
0
        public void PostEventCallsHandlersForParentTypes()
        {
            Participant participant = null;
            IMediator mediator = null;
            try {
                participant = new Participant("Test");
                mediator = new Mediator();
                mediator.Register<ChatMessageEventArgs>(participant.OnRecieveMessage);
                mediator.Register<EventArgs>(participant.OnRecieveEvent);

                var args = new ChatMessageEventArgs("Hello there!");
                mediator.Post(this, args);

                Assert.That(participant.HasReceivedMessageOnce(this, args), Is.True);
                Assert.That(participant.HasReceivedEventOnce(this, args), Is.True);

            } finally {
                if(participant != null) {
                    participant.Dispose();
                    participant = null;
                }

                var disposableMediator = mediator as IDisposable;
                if(disposableMediator != null) {
                    disposableMediator.Dispose();
                }
            }
        }
Exemple #4
0
 public void OnRecieveMessage(object sender, ChatMessageEventArgs args)
 {
     lock(_syncHandle) {
         _onRecievedMessageTracker.OnFired(sender, args);
     }
 }
Exemple #5
0
 public bool HasReceivedMessageOnce(object sender, ChatMessageEventArgs args)
 {
     lock(_syncHandle) {
         return _onRecievedMessageTracker.WasFiredOnce(sender, args);
     }
 }
Exemple #6
0
 public bool HasInterceptedMessageOnce(object sender, ChatMessageEventArgs args)
 {
     lock(_syncHandle) {
         return _onInterceptTracker.WasFiredOnce(sender, args);
     }
 }