Problem
I have a protobuf message definition with a MessageType
field, which is an enum. Given an incoming protobuf message, I would like to resolve some IMessageHandler
s from an IoC container based on the MessageType
. The problem is twofold: How do I express the MessageType
constraint when writing an IMessageHandler
, and how do I resolve only the desired handlers from the IoC container?
I'm using Autofac but am interested in hearing solutions for any container.
My thoughts:
For expressing the constraint, I see two options: a property or an attribute (can't use generics because it is an enum value). I like the property because it makes it impossible to write an IMessageHandler
without specifying the constraint, but the downside is that it has to be instantiated before you can see the property value.
In my current code, I'm using the property approach. I resolve all IMessageHandler
s and do 开发者_如何学Pythonthe filtering manually, but it seems like there should be a better way. Not that I'm too worried about performance, it just kind of smells that I'm resolving instances that don't get used.
Update
To make it a little more clear, here's what I'm doing now
public interface IMessageHandler
{
MessageType TargetType { get; }
void Handle(IMessage message);
}
public class SomeHandler : IMessageHandler
{
public MessageType TargetType
{
get { return MessageType.Type1; }
}
public void Handle(IMessage message)
{
// implementation
}
}
So when I receive a protobuf message, I resolve all IMessageHandler
s and invoke the ones whose TargetType
matches the MessageType
of the incoming message.
I suggest you register a single IMessageHandler-Factory class with autofac and implement a method on that factory that takes your enum and returns the right IMessageHandler implementation.
I realized that Autofac's Keyed Services can help me out. I think I'm going to go with this approach.
- Use an attribute to declare what
MessageType
a givenIMessageHandler
is interested in. - Register every
IMessageHandler
keyed to theMessageType
- Use
ResolveKeyed
to get only theIMessageHandler
s I'm interested int.
The nice thing is if someone forgets to use the attribute, we can catch it while building the container. Here's a full example. Any suggestions are most welcome!
class Program
{
static IContainer container;
static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
.AssignableTo<IMessageHandler>()
.Keyed<IMessageHandler>(t => GetMessageType(t));
container = builder.Build();
InvokeHandlers(MessageType.Type1);
InvokeHandlers(MessageType.Type2);
Console.ReadKey();
}
static MessageType GetMessageType(Type type)
{
var att = type.GetCustomAttributes(true).OfType<MessageHandlerAttribute>().FirstOrDefault();
if (att == null)
{
throw new Exception("Somone forgot to put the MessageHandlerAttribute on an IMessageHandler!");
}
return att.MessageType;
}
static void InvokeHandlers(MessageType type)
{
using (var lifetime = container.BeginLifetimeScope())
{
// I'm impressed that Autofac knows what I mean here!
var handlers = lifetime.ResolveKeyed<IEnumerable<IMessageHandler>>(type);
foreach (var handler in handlers)
{
handler.Handle();
}
}
}
}
public enum MessageType
{
Type1,
Type2,
}
public interface IMessageHandler
{
void Handle();
}
public class MessageHandlerAttribute : Attribute
{
public MessageHandlerAttribute(MessageType messageType)
{
MessageType = messageType;
}
public MessageType MessageType { get; private set; }
}
[MessageHandler(MessageType.Type1)]
public class Handler1 : IMessageHandler
{
public void Handle()
{
Console.WriteLine("A handler for Type1");
}
}
[MessageHandler(MessageType.Type1)]
public class Handler2 : IMessageHandler
{
public void Handle()
{
Console.WriteLine("Another handler for Type1");
}
}
[MessageHandler(MessageType.Type2)]
public class Handler3 : IMessageHandler
{
public void Handle()
{
Console.WriteLine("A handler for Type2");
}
}
精彩评论