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 IMessageHandlers 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 IMessageHandlers and do the 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 IMessageHandlers and invoke the ones whose TargetType matches the MessageType of the incoming message.
I realized that Autofac’s Keyed Services can help me out. I think I’m going to go with this approach.
MessageTypea givenIMessageHandleris interested in.IMessageHandlerkeyed to theMessageTypeResolveKeyedto get only theIMessageHandlers 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!