I'm implementing a Mediator and I'd like to simplify the callback notation a bit.
My mediator has Register and Notify methods :
public class Mediator {
private Dictionary<Messages, Action<object>> registeredCallback;
public void Register(Messages message, Action callback) {
registeredCallback[message] = callback;
}
public void Notify(Messages message, object parameter) {
callback(parameter);
}
public Mediator() {
registeredCallback = new Dictionary<Messages, Action<object>>();
}
}
It uses a Messages enumerator to sanitize events between registered objects, for example :
public enum Messages {
UserCreated,
UserDeleted,
UserChangedPassword
};
So far, so good. However, when I use this mediator I'm making ugly callback definitions :
mediator.Register(Messages.UserCreated, (object user) => GreetUser(((User)user).name));
with the following User:
public class User {
public string name;
}
Type casting? Lambda expressions? This looks obviously filthy. How can I clean this up? Is my architecture at fault?
registeredCallbacks[message]in a loop but the value is anAction<object>and it cannot be enumerated. \$\endgroup\$MultiDictionarywhich stores multiple callbacks in aHashSet<Action<object>>for each message. I didn't include it in my example to simplify a bit. I removed the loop, sorry for the confusion. \$\endgroup\$