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

I have an existing ASP .NET MVC application and I would like to ignore
I have a site written in ASP.NET MVC. I have a page where the
I'm converting an existing ASP.NET app to MVC2, and I have an existing method
I'm implementing the MVP pattern within an existing asp.net webforms application and I was
Coding Platform: ASP.NET 4.0 WebForms with C# We have a website with the existing
I have a fairly standard ASP.Net web application which is used via mobile safari
I am currently investigating the possibility to incorporate MVC into our existing WebForms framework.
I am building in some custom javascript functionality in an existing ASP.NET usercontrol. The
I have an ASP.NET web app where I use an ado sql connection to
I use asp.net 4 and c#. I have a TreeView and I would like

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.