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 8717407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:17:55+00:00 2026-06-13T06:17:55+00:00

I have two decorators: class DbCommandWithTransactionHandlerDecorator<TCommand> : IDbCommandHandler<TCommand> { … } class DbOptimisticConcurrencyRetryDecorator<TCommand> :

  • 0

I have two decorators:

class DbCommandWithTransactionHandlerDecorator<TCommand>
    : IDbCommandHandler<TCommand> { ... }

class DbOptimisticConcurrencyRetryDecorator<TCommand>
    : IDbCommandHandler<TCommand> { ... }

These decorators add transaction management and optimistic-concurrency retry capability to a database command.

I am using Autofac as my IoC container. I would like to setup Autofac such that it will auto-wire all IDbCommandHandler<> found in an assembly, such that when I request say an IDbCommandHandler<CreateNewNotificationCommand>, it will automatically decorate it with first a DbCommandWithTransactionHandlerDecorator, and then a DbOptimisticConcurrencyRetryDecorator.

I’ve been trying to get this with Autofac’s builder.RegisterGenericDecorator(), but haven’t yet managed. The main issue is that a decorator requires a ‘named’ argument to work. Below is the sample code that is most ‘near’ to what I want to achieve – However the main flaw is that I still had to manually register the types.

var builder = new ContainerBuilder();
var a = Assembly.GetExecutingAssembly();

// I need to find a way how these can be 'auto-wired', 
// rather than having to manually wire each command.

builder.RegisterType<CreateNewNotificationCommandHandler>()
    .Named<IDbCommandHandler<CreateNewNotificationCommand>>("command");
builder.RegisterType<CreateNewNotificationCommandHandler_2>()
    .Named<IDbCommandHandler<CreateNewNotificationCommand_2>>("command");

builder.RegisterGenericDecorator(
    typeof(DbCommandWithTransactionHandlerDecorator<>),
    typeof(IDbCommandHandler<>),
    fromKey: "command");

var container = builder.Build();
var handler1 =
  container.Resolve<IDbCommandHandler<CreateNewNotificationCommand>>();
var handler2 =
  container.Resolve<IDbCommandHandler<CreateNewNotificationCommand_2>>();
handler1.Handle(null); //these are correctly decorated
handler2.Handle(null); //these are correctly decorated
  • 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-06-13T06:17:56+00:00Added an answer on June 13, 2026 at 6:17 am

    I did manage to find a workaround via reflection, which although it works is not really elegant. I will post it below for completeness sake:

     public interface IDbCommandHandler<in TCommand>: IDbCommandHandlerStub
        where TCommand : IDbCommand
    {
        void Handle(TCommand command);
    }
    
    public interface IDbCommandHandlerStub
    {
    
    }
    
        private List<Type> getTypesThatImplementIDbCommandHandler(IEnumerable<Assembly> assemblyList)
        {
            List<Type> list = new List<Type>();
            foreach (var a in assemblyList)
            {
                var matches = a.GetTypes().Where(t => typeof(IDbCommandHandlerStub).IsAssignableFrom(t));
                list.AddRange(matches);
            }
            return list;
        }
    
    private void registerDbCommands(List<Type> dbCommandHandlerTypes, ContainerBuilder builder)
        {
            foreach (var t in dbCommandHandlerTypes)
            {
                var interfaces = t.GetInterfaces();
                foreach (var i in interfaces)
                {
                    builder.RegisterType(t).Named("dbCommand", i);
                }
    
            }
        }
    
    
       public void Test1()
       {
            ContainerBuilder builder = new ContainerBuilder();
            var dbCommandHandlerTypes = getTypesThatImplementIDbCommandHandler(assemblies);
            registerDbCommands(dbCommandHandlerTypes, builder);            
    
            builder.RegisterGenericDecorator(typeof(DbCommandWithTransactionHandlerDecorator<>),
                                            typeof(IDbCommandHandler<>),
                                            fromKey: "dbCommand", toKey:"dbCommandWithTransaction").SingleInstance();
    
            builder.RegisterGenericDecorator(typeof(DbOptimisticConcurrencyRetryDecorator<>),
                                            typeof(IDbCommandHandler<>),
                                            fromKey: "dbCommandWithTransaction").SingleInstance();
    
            var container = builder.Build();
            var handler1 = container.Resolve<IDbCommandHandler<CreateNewNotificationCommand>>();
    
    }
    

    First, I get via reflection all the types that implement IDbCommandHandler. Then, I register them as named types for all the interfaces which they implement, giving them a name of ‘dbCommand’.

    Then, I register the generic decorator to decorate types named ‘dbCommand’. This decorater is named ‘dbCommandWithTransaction’, and is used to then register another generic decorator for the concurrency-retry.

    Considering this is something which would be done once and ‘forgotten’, I was ready go with this workaround. However, I was trying out other IoC containers and came upon Simple Injector, and all this can be done in just two lines of code – And have since then won me over.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
Have two events: $('body').mouseup(function(e){} and $('.toggle').click(function(e){} I only want one of these to trigger.
have two three tables one for student, second for class, and the third for
How to combine two functions together I have a class controlling some hardware: class
Have two folders with approx. 150 java property files. In a shell script, how
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two tables images2 and image_data So the goal is to have 1
I have two files client.php and server.php. The client file send a HTTP request
I have two columns say Main and Sub . (they can be of same
I have two tables 1.Products prod_id prod_name 1 honda 2 hero 3 marcedes 4

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.