bovender framework
C# framework that implements MVVM and more
Message.cs
1 /* Message.cs
2  * part of Daniel's XL Toolbox NG
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 
23 namespace Bovender.Mvvm.Messaging
24 {
31  public class Message<T> : IMessage<T> where T : MessageContent, new()
32  {
33  #region IViewModelMessage interface
34 
39  public event EventHandler<MessageArgs<T>> Sent;
40 
41  #endregion
42 
43  #region Protected methods
44 
52  public virtual void Send(
53  T messageContent,
54  Action<T> respond)
55  {
56  EventHandler<MessageArgs<T>> h = Sent;
57  if (h != null)
58  {
59  Logger.Info("Sending message to {0} subscriber(s)", h.GetInvocationList().Length);
60  Logger.Debug("Message content is a {0}",
61  messageContent == null ? "null reference" : messageContent.GetType().FullName);
62  h(this,
63  new MessageArgs<T>(
64  messageContent,
65  () =>
66  {
67  if (respond != null) respond(messageContent);
68  }
69  )
70  );
71  };
72  }
73 
80  public virtual void Send(T messageContent)
81  {
82  Send(messageContent, null);
83  }
84 
88  public virtual void Send()
89  {
90  Send(new T(), null);
91  }
92 
93  #endregion
94 
95  #region Class logger
96 
97  private static NLog.Logger Logger { get { return _logger.Value; } }
98 
99  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
100 
101  #endregion
102  }
103 }
virtual void Send(T messageContent)
Raises the Sent event with a MessageArgs instance that encapsulates the messageContent ...
Definition: Message.cs:80
virtual void Send(T messageContent, Action< T > respond)
Calling this method will raise the Sent event with a message content and a callback method that can b...
Definition: Message.cs:52
EventHandler< MessageArgs< T > > Sent
Consumers of the view model subscribe to this event if they want to listen for the message...
Definition: Message.cs:39
Conveys a message from a view model to a consumer (typically, a view) that has subscribed to the Sent...
Definition: Message.cs:31
Simple object that encapsulates a boolean value; to be used in MVVM interaction with MessageArgs...
virtual void Send()
Sends a simple message that does not need responding to.
Definition: Message.cs:88
Defines a Sent event that consumers of a view model can subscribe to in order to listen to the view m...
Definition: IMessage.cs:29