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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:17:00+00:00 2026-06-11T16:17:00+00:00

With StructureMap, can one inject instances based on their name by convention, like so:

  • 0

With StructureMap, can one inject instances based on their name by convention, like so:

Ctor:

public HomeController(IQuery getItemByProductNumberQuery, IQuery getCustomerById)

StructureMap config:

x.For<IQuery>().Add<GetItemByProductNumberQuery>().Named("getItemByProductNumberQuery");
x.For<IQuery>().Add<GetCustomerById>().Named("getCustomerById");

I’m working with legacy code, so changing the DI container is a huge undertaking, but it would be interesting to know about other containers’ solution to the problem at hand as well.

  • 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-11T16:17:01+00:00Added an answer on June 11, 2026 at 4:17 pm

    It seems to me that you are missing an abstraction in your code base. Under normal circumstances, a service (IQuery in your case) should be unambiguous, which isn’t the case in your situation. Identifying them by the argument name is often error prone and leads to a DI configuration that is hard to maintain. This doesn’t always have to be this way (as can be seen in this example), but it probably is in your case.

    To solve this, make your IQuery interface generic:

    public interface IQuery<TParameters, TResult>
    {
        TResult Handle(TParameters parameters);
    }
    

    This allows you to register all implementations of this interface by their closed generic representation, and allow your control to depend on this closed generic representation:

    public HomeController(
        IQuery<GetItemByProductNumberParameters, Item> getItemQuery, 
        IQuery<GetCustomerByIdParameters, Customer> getCustomerById)
    

    As you can see, each query defines a ‘XXXParameters’ object. This is a DTO that contains all parameters needed to run the query. The code in your HomeController might look like this:

    public View Item(int productNumber)
    {
        var parameters = new GetItemByProductNumberParameters
        {
            ProductNumber = productNumber,
            // other parameters here
        };
    
        Item item = this.getItemQuery.Handle(parameters);
    
        return this.View(item);
    }
    

    It might be a bit difficult to batch register all IQuery<TParameter, TResult> implementations in one go with StructureMap, but this SO question might help. And if not, using another DI container might yield better results.

    You can find more information about why you want to model your queries this way in this article: Meanwhile… on the query side of my architecture.

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

Sidebar

Related Questions

I have a convention for StructureMap that looks like this: public class FakeRepositoriesConvention :
StructureMap newbie question. public class SomeClass: IInterface1, IInterface2 { } I would like the
I'm currently using StructureMap to inject instances of NHibernate ISessions using the following code:
How can I figure StructureMap to use more than one concrete Class per Interface.
With StructureMap one can do a resolution and force the container to use specific
Im using StructureMap for IoC, im getting Configuration from App.Config, like this: public class
I'm using StructureMap at the moment, generally with convention-based ( Scan() ) auto-configuration, and
I have a problem.... Lest say I have a class like this one: public
High Level With StructureMap, Can I define a assembly scan rule that for an
In Using StructureMap 2.5 to scan all assemblies in a folder , we can

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.