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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:28:59+00:00 2026-05-12T22:28:59+00:00

I am fairly new to StructureMap, but my understanding is that there are two

  • 0

I am fairly new to StructureMap, but my understanding is that there are two ways of getting an instance from the ObjectFactory:

  1. By type (i.e. ObjectFactory.GetInstance<IFoo>())
  2. By type and name (i.e. ObjectFactory.GetNamedInstance<IFoo>("foo.bar"))

I have seen a multitude of examples out there demonstrating how to create an MVC Controller Factory that will provide controller instances using StructureMap, and in most cases I have seen, they are using the method from option #1 above.

Let’s say we have some controller that accepts a repository/DAO interface as a constructor arg…

public class FooController : Controller
{
  public FooController (IFooRepository fooRepository)
  {
     // constructor code goes here
  }
}

Using the interface class gives me the ability to “inject” any implementation of that interface in to my controller. What if for two different actions, I want to inject different implementations? Perhaps in one case, I need an implementation of my repository that will query a SQL Server database, and in another case, I need an implementation that will get data from an XML file or through a Web Service call.

It seems that if you use the ObjectFactory.GetInstance() method, you would be restricted to only providing your controller with a single implementation of your repository interface.

However, if you go with named instances, then you will end up having to create the instances based on the name of the controller that the MVC framework provides. In most cases, this will typically be the controller name coming from the URL, but it’s really up to the route configuration. This seems like it could be very confusing, and would only get more confusing as the number of routes increased.

Is there any alternative method that would allow for different controller instantiation strategies without using named instances? Would it be a better idea to just create separate controllers, and make sure that all the actions in any given controller will be valid for the same concrete instance of a repository/DAO class?

  • 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-12T22:28:59+00:00Added an answer on May 12, 2026 at 10:28 pm

    For what it’s worth, I ended up going with named instances, where the names of each instance are based on the name of the controller that the MVC framework pulls from the URL.

    For instance, the URL /foo/DoSomething might get an IController instance from the ObjectFactory that is instantiated with a Repository object that uses the SqlClient API for data access. A URL such as /foo.webservice/DoSomething would then create an instance of the same concrete controller class, but pass that instance’s constructor a repository object that uses a web service for data access.

    There are several ways to override StructureMap’s default auto-wiring behavior for constructor arguments. In my case, I used the CtorDependency and Is methods, and had mappings that looked something like this…

    // create a named instance for the "foo" controller that uses the SqlClient based repository
    InstanceOf<IController>()
        .Is.OfConcreteType<FooController>()
        .CtorDependency<IFooRepository>()
        .Is(new FooRepositorySql(Constants.FooConnectionString))
        .WithName("foo");
    
    // create a named instance for the "foo.webservice" controller that uses the Web Service based repository
    InstanceOf<IController>()
        .Is.OfConcreteType<FooController>()
        .CtorDependency<IFooRepository>()
        .Is(new FooRepositoryWs(Constants.FooServiceUrl))
        .WithName("foo.webservice");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fairly new to wpf, using the galasoft mvvm templates. I have two relaycommands that
Being fairly new to programming, I am having trouble understanding exactly what Homebrew does...
I fairly new to JQuery and perhaps trying to achieve something that might be
I'm fairly new to ruby and I've got a hash that looks like so:
I am fairly new to php and mysql but I have created a small
Fairly new to Google Maps API. I've got an array of data that I
Fairly new to socket programming, but I've been assigned with a whopper of project.
Am fairly new to using MySQL and a total novice at Perl but am
I'm fairly new to SQL and I'm currently reworking a java program that another
So I'm fairly new to jQueryMobile but I've managed to create a search bar

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.