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

  • Home
  • SEARCH
  • 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 4537222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:38:43+00:00 2026-05-21T14:38:43+00:00

Related to this question , but this question is about StructureMap. I have got

  • 0

Related to this question, but this question is about StructureMap.

I have got the following interface definition:

public interface ICommandHandler
{
    ILogger Logger { get; set; }
    bool SendAsync { get; set; }
}

I have multiple implementations that implement ICommandHandler and need to be resolved. I want to automatically inject these two properties in instances that implement ICommandHandler.

I found a way to do inject the ILogger by letting all implementations inherit from a base type and the [SetterProperty] attribute:

public abstract class BaseCommandHandler : ICommandHandler
{
    [SetterProperty]
    public ILogger Logger { get; set; }

    public bool SendAsync { get; set; }
}

This however, doesn’t help me with injecting the primitive bool type in all implementations.

The command handlers implement a generic interface that inherits from the base interface:

public interface ICommandHandler<TCommand> : ICommandHandler
    where TCommand : Command
{
     void Handle(TCommand command);
}

Here is my current configuration:

var container = new Container();

container.Configure(r => r
    .For<ICommandHandler<CustomerMovedCommand>>()
    .Use<CustomerMovedHandler>()
    .SetProperty(handler =>
    { 
        handler.SendAsync = true;
    }));

container.Configure(r => r
    .For<ICommandHandler<ProcessOrderCommand>>()
    .Use<ProcessOrderHandler>()
    .SetProperty(handler =>
    { 
        handler.SendAsync = true;
    }));

container.Configure(r => r
    .For<ICommandHandler<CustomerLeftCommand>>()
    .Use<CustomerLeftHandler>()
    .SetProperty(handler =>
    { 
        handler.SendAsync = true;
    }));

// etc. etc. etc.

As you can see, I set the property for each and every configuration, which is quite cumbersome (however, it is pretty nice that the SetProperty accepts a Action<T> delegate).

What ways are there to do this more efficiently with StructureMap?

  • 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-21T14:38:44+00:00Added an answer on May 21, 2026 at 2:38 pm

    Maybe you could use this?

    container.Configure(r => r.For<ICommandHandler>()
        .OnCreationForAll(handler =>
        {
            handler.Logger = container.Resolve<ILogger>();
            handler.SendAsync = true;
        }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somewhat related to this question , but in the absence of any answer about
This question is kind of related to another question but I have a specific
This question may not be exactly programming related, but this is about development. I
This is perhaps related to this question , but I have slightly more information.
I have an SQL question, related to this and this question (but different). Basically
Some of this question is related to JPA, but it's more about approaches than
This is somewhat related to the question posed in this question but I'm trying
Note: this question is related to this one , but two years is a
[This question is somewhat related to this question , but the answers are not...]
I've looked at other questions on Stack Overflow related to this question, but none

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.