Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5951893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:36:11+00:00 2026-05-22T17:36:11+00:00

Problem I have a protobuf message definition with a MessageType field, which is an

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:36:11+00:00Added an answer on May 22, 2026 at 5:36 pm

    I realized that Autofac’s Keyed Services can help me out. I think I’m going to go with this approach.

    1. Use an attribute to declare what MessageType a given IMessageHandler is interested in.
    2. Register every IMessageHandler keyed to the MessageType
    3. Use ResolveKeyed to get only the IMessageHandlers 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");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have an address field from an Access database which has been converted
Problem I have timestamped data, which I need to search based on the timestamp
Problem: We have a web app that calls some web services asynchronously (from the
Problem: I have to support users who need to edit web pages. Some of
I have problem in some JavaScript that I am writing where the Switch statement
Maybe some of you could have ran into the same problem i did. Imagine
Problem : I have to create a number of tables for caching some amount
PROBLEM: I have a Child class which uses DataContractSerialization and raises a Changed event
Problem We have a server over which we have FULL control We have a
I have the following problem. This is a simple example of some classes I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.