HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Shoot The Messenger Part 4

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
messengerparttheshoot

Problem

This is a follow up of:

  • Messenger supporting notifications and requests



  • Shoot the Messenger pt. 2



  • Shoot the Messenger Part 3




I've written a lightweight (I think) class that acts as a messenger
service between classes for both notifications (fire and forget
updates to other classes) and requests (a notification sent out that
expects a returned value).

I've split the Messenger and Requester functionality into two separate classes. I'm leaving off the dragon of thread-safety until the next time, for now I'd like to make sure what I have is ok, before worrying about multi-threading.

Messenger

```
///
/// Strongly-typed messenger system.
///
public class Messenger : IMessenger
{
///
/// The actions. These are called when a message is sent.
///
private readonly IDictionary actions = new Dictionary();

///
/// Register an action for a message.
///
/// Type of message to receive.
/// The action that is executed when the message is received.
public void Register(Action action)
{
if (action == null)
{
throw new ArgumentNullException("action");
}

var messageType = typeof(T);

if (actions.ContainsKey(messageType))
{
actions[messageType] = Delegate.Combine(actions[messageType], action);
}
else
{
actions.Add(messageType, action);
}
}

///
/// Sends the specified message.
///
/// The type of message.
/// The message to send.
public void Send(T message)
{
var messageType = typeof(T);

if (actions.ContainsKey(messageType))
{
((Action)actions[messageType])(message);
}
}

///
/// Unregister an action.
///
/// The type of message.
/// The action to unregister.
public void Unregister(Action action)
{
var messageType = typeof(T);

if (actions.ContainsKey(messag

Solution

There isn't much to pick on, really - code is clean, naming is unambiguous, scopes all have their braces, you're yielding enumerables, comments are useful, and you even have XML documentation everywhere... I can't find anything in the code itself that's off-putting, confusing, or just wrong. Nothing.

Except perhaps this:

/// 
/// The actions. These are called when a message is sent.
/// 
private readonly IDictionary actions = new Dictionary();


XML comments don't quite belong on private fields (XML documentation is for the public API, right?), I think this should be a regular comment.

Now thread safety -wise, there might be something to say, like, why not use a ConcurrentDictionary instead of an IDictionary? Coding against abstractions is great, but sometimes you do need a specific implementation ;)

Code Snippets

/// <summary>
/// The actions. These are called when a message is sent.
/// </summary>
private readonly IDictionary<Type, Delegate> actions = new Dictionary<Type, Delegate>();

Context

StackExchange Code Review Q#78234, answer score: 7

Revisions (0)

No revisions yet.