Skip to content

Instantly share code, notes, and snippets.

@loneshark99
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loneshark99/855209c7aa5d645516c5 to your computer and use it in GitHub Desktop.
Save loneshark99/855209c7aa5d645516c5 to your computer and use it in GitHub Desktop.
Simple Message Router based on type
void Main()
{
var fooInstance = new Foo();
fooInstance.Id = "FOOOO";
var barInstance = new Bar();
barInstance.Id = "BARRR";
MessageRouter router = new MessageRouter();
router.AddHandler(new Handler1());
router.AddHandler(new Handler2());
router.RouteMessage(fooInstance);
router.RouteMessage(barInstance);
}
public class MessageRouter
{
List<IHandler> handlers = new List<IHandler>();
public void AddHandler(IHandler handler)
{
if (handlers != null)
{
handlers.Add(handler);
}
}
public void RemoveHandler(IHandler handler)
{
if (handlers != null)
{
var hdlr = handlers.Where(h => h.Name == handler.Name).FirstOrDefault();
if(hdlr != null)
{
handlers.Remove(hdlr);
}
}
}
public void RouteMessage(object message)
{
foreach(var h in handlers)
{
if(h.CanHandle(message.GetType()))
{
h.ProcessMesaage(message);
}
}
}
}
public class Foo
{
public string Id { get; set;}
}
public class Bar
{
public string Id { get; set;}
}
public interface IHandler
{
List<Type> HandledTypes {get;}
bool CanHandle(Type type);
void ProcessMesaage(object message);
string Name {get;}
}
public class Handler1 : IHandler
{
List<Type> types;
public Handler1()
{
types = new List<Type>();
types.Add(typeof(Foo));
}
public string Name
{
get
{
return "Handler1";
}
}
public List<Type> HandledTypes
{
get
{
return types;
}
}
public bool CanHandle(Type type)
{
if(this.HandledTypes != null)
{
return this.HandledTypes.Where(t => t.Name == type.Name).Count() > 0;
}
return false;
}
public void ProcessMesaage(object message)
{
dynamic msg = message;
if (message != null)
{
"******************".Dump("Handler 1");
Console.WriteLine("Handler1 handled the message of type..." + msg.GetType());
Console.WriteLine("Message Handler1 received has a Id of = " + msg.Id);
"******************".Dump("");
}
}
}
public class Handler2 : IHandler
{
List<Type> types;
public Handler2()
{
types = new List<Type>();
types.Add(typeof(Bar));
types.Add(typeof(Foo));
}
public List<Type> HandledTypes
{
get
{
return types;
}
}
public string Name
{
get
{
return "Handler2";
}
}
public bool CanHandle(Type type)
{
if(this.HandledTypes != null)
{
return this.HandledTypes.Where(t => t.Name == type.Name).Count() > 0;
}
return false;
}
public void ProcessMesaage(object message)
{
dynamic msg = message;
if (message != null)
{
"******************".Dump("Handler 2");
Console.WriteLine("Handler2 handled the message of type..." + msg.GetType());
Console.WriteLine("Message Handler2 received has a Id of = " + msg.Id);
"******************".Dump("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment