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

The Archive Base Latest Questions

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

I have an existing ASP.NET MVC application and am using StructureMap as my IOC

  • 0

I have an existing ASP.NET MVC application and am using StructureMap as my IOC container of choice. Currently when a controller needs an IMembershipProvider I use StructureMap to inject a concrete instance in the controller’s constructor based on the BuyerMembershipProvider configuration from my web.config file as in the below solution code:

ObjectFactory.Initialize(x =>
{
        x.ForRequestedType<IMembershipService>().TheDefaultIsConcreteType<AccountMembershipService>();
    x.ForRequestedType<IFormsAuthentication>().TheDefaultIsConcreteType<FormsAuthenticationService>();
    x.ForRequestedType<MembershipProvider>().TheDefault.IsThis(SecurityProvider.Providers["BuyerMembershipProvider"]);
});

This works fine. However, it is bound to the “buyer” user type. Seller information is stored in a different database and will have different tables to store membership information. Essentially, I would like to be able to inject either a BuyerMembershipProvider or a SellerMembership Provider based on context and have the following in my objectfactory initialization:

x.ForRequestedType<MembershipProvider>().TheDefault.IsThis(SecurityProvider.Providers["BuyerMembershipProvider"]);
x.ForRequestedType<MembershipProvider>().TheDefault.IsThis(SecurityProvider.Providers["SellerMembershipProvider"]);

Is it possible to decide between two concrete implementations when initializing the objectfactory?

Any help is greatly appreciated,
Thanks in advance!
JP

  • 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-13T17:41:23+00:00Added an answer on May 13, 2026 at 5:41 pm

    The major mechanisms in choosing between different implementations would be via

    • named instance
    • choosing a certain ctor dependency
    • a conditional expression in StructureMap.

    You can’t state 2 defaults for a given type (which should be obvious), but you can name an instance and say under which circumstances you want to use it.

    A named instance is defined e.g. like this:

    InstanceOf<IApplicationModule>()
      .Is.OfConcreteType<BusinessProcessSupport>()
      .WithName("BusinessProcessSupport");
    

    Here is an example where a named instance is used in fulfilling a certain ctor dependency:

    ForRequestedType<MenuStripModule>()
      .CacheBy(InstanceScope.Singleton)
      .TheDefault.Is.OfConcreteType<MenuStripModule>()
      .CtorDependency<ICommandInfoProvider>()
        .Is(i => i.TheInstanceNamed("Functions"));
    

    And finally you can have a look here at a (somewhat nasty) StructureMap expression that uses the conditional capabilities: http://realfiction.net/Content/Entry/142
    Please note that you are using the StructureMap 2.5.* syntax. 2.6.* is out which has modified the configuration grammar to be more concise and to the point.

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

Sidebar

Related Questions

First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I have an existing ASP.NET MVC application with some sample data in the SQL
I'm new to DDD. I have an existing ASP.NET application (not MVC) and I
I have an ASP.NET MVC application where I am editing an existing database to
I have an existing ASP.NET web application that I'm converting to MVC 1.0. The
Whats the best method to localize an existing ASP.Net Application (MVC 3). What needs
Currently we are migrating an existing asp.net web application to asp.net mvc 2 platform.
I have a ASP.NET MVC 3 application, using Entity Framework 4 to handle Data
I have an existing ASP.NET MVC 2 application that I've been asked to extend.
I have an existing ASP .NET MVC application and I would like to ignore

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.