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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:41:46+00:00 2026-05-30T00:41:46+00:00

I’m trying out Ninject and I’m modifying to code I wrote with Structure Map

  • 0

I’m trying out Ninject and I’m modifying to code I wrote with Structure Map to see how easy it is. In this base code I have a graph of objects which have different configurations via the Structure Map registries and the one to use is chosen at runtime via a value in the database (in this case to pull back a wcf service body with some objects injected). So for example (using the Structure Map code):

Registry 1 sets up all defaults for the IBusinessContext, IRules, and ILogger types. This is just adding the types GenericContext/Logger/Rules along side the interfaces with no other specialisation.

public GenericRegistry()
    {
        // Set up some generic bindings here
        For<ILogger>().Use<Loggers.GenericLogger>();
        For<IBusinessRule>().Use<Rules.StandardRule>();
        For<IBusinessContext>().Use<Contexts.GenericBusinessContext>();
        For<ILoggerContext>().Use<Loggers.GenericLoggerContext>();
    }

Registry 2 sets up IBusinessContext to use the SpecialisedContext class and tells the ctor to use SpecializedLogger. The instance for IBusinessContext is named “SpecializedContext”.

public SpecializedRegistry()
    {
        // Old style syntax as it affects the default for IBusinessContext
        // Perhaps a hint at what I'm doing?
        InstanceOf<IBusinessContext>().Is.OfConcreteType<Contexts.SpecializedBusinessContext>().Named(SpecializedInstanceName).Ctor<ILogger>().Is<Loggers.SpecialisedLogger>();
    }

This all works as expected in Structure Map (depending on old or new syntax).

However, when I’ve been using Ninject I hit a problem with expecting the unnamed instance to be default (not how Ninject works, I get that). This led to some research which all suggested that using named instances is A Really Bad Idea. I understand that there are better ways to do this using auto registration or attributes to set a name or requesting a certain type, but in the system I’m describing there needs to be a way at run time to figure out what configuration to ask for at the top of the tree (and let the IoC framework figure out the rest based on registered types or rules).

So…am I just using the IoC concept wrong here by expecting to ask for my top level object by name or is there generally a better way to do what I’m trying to do? Should I be using something like MEF instead and treating this all like plug-ins?

I stress I’m not using this like a dumb factory and asking at each level of the code for a instance of type x from the container, it is the initiating action only.

Thanks in advance for your time and help 🙂

  • 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-30T00:41:48+00:00Added an answer on May 30, 2026 at 12:41 am

    There is nothing all that wrong with setting up ninject bindings by name, if that is the only way to achieve what it is you need IMO.

    So the basic syntax is:

    Bind<IBusinessContext>().To<ConcreteBusinessContext>().Named("XYZ");
    

    Or if you need a specific calling class to get a different binding then you can try:

    Bind<IIBusinessContext>().To<SomeOtherConcreteBusinessContext>().WhenInjectedInto<TypeOfCallingClass>();
    

    However, if the calling class (I’m talking about the class that has the IBusinessContext in its ctor) supplies a configuration value which determines which concrete type to load, then you will need to use a delegate:

    Bind<Func<string, IBusinessContext>>().ToMethod(ctx => str => DetermineWhichConcreteTypeToLoad(ctx, str));
    
    //messy sudo code
    static DetermineWhichConcreteTypeToLoad(IContext ctx, string str)
    {
        if(str == "somevalue"){
            return ctx.Kernel.Get<ConcreteType1>();
        else
            return ctx.Kernel.Get<ConcreteType2>();
    }
    

    and your calling class will look something like:

    class DoStuff
    {
        Func<string, IBusinessContext>> contextFunc;
    
        DoStuff(Func<string, IBusinessContext>> contextFunc)
        {
            this.contextFunc = contextFunc;
        }
    
        void SomeMethod()
        {
            var configuredValue = GetConfiguredValueSomehow();
            var context = contextFunc(configuredValue); //<-- this passes your config value back to ninject in the ToMethod() lambda
            //do something with context
        }
    }
    

    In that example there is no need for named instances, as you have a method which loads the specific concrete type, however you can still use named instances if you want to do something like this:

    Bind<IBusinessContext>().To<ConcreteBusinessContext>().Named("config1");
    Bind<IBusinessContext>().To<SomeOtherBusinessContext>().Named("config2");
    
    Bind<Func<string, IBusinessContext>>().ToMethod(ctx => str => ctx.Kernel.Get<IBusinessContext>().Named(str));
    
    class DoStuff
    {
        Func<string, IBusinessContext>> contextFunc;
    
        DoStuff(Func<string, IBusinessContext>> contextFunc)
        {
            this.contextFunc = contextFunc;
        }
    
        void SomeMethod()
        {
            var configuredValue = "config1";
            var context = contextFunc(configuredValue); //<-- this will passthrough "config1" to the above ToMethod() method and ask for a IBusinessContext named "config1"
    
        }
    }
    

    EDIT: I forgot to mention that if your config value doesnt have to come from the calling code, then this makes things much easier. Your code can instead look something like:

    // this method can just be a global method in you app somewhere
    static string GetConfigValue()
    {
        //something like
        return AppSetting.Get("config");
    }
    
    Bind<IBusinessContext>().To<ConcreteBusinessContext>().When(r => GetConfigValue() == "config1");
    Bind<IBusinessContext>().To<SomeOtherBusinessContext>().When(r => GetConfigValue() == "config2");
    
    class DoStuff
    {
        IBusinessContext context;
    
        DoStuff(BusinessContext context)
        {
            this.context = context;
        }
    
        void SomeMethod()
        {
            //use the context value as you normally would
        }
    }
    

    You can be creative and rather than use magic strings, your config method can load up an enum and your When() method can test for equality against an enum instead of a string, but you get the idea. This is known as contextual binding in ninject, and I can tell you as a once avid user of SM, this is much much more powerful than anything SM had. Checkout the rest of the When() methods and see what you can do.

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

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.