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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:25:22+00:00 2026-05-28T05:25:22+00:00

I have a single Ninject 2 Kernel for my application which contains all bindings.

  • 0

I have a single Ninject 2 Kernel for my application which contains all bindings. One section of the application needs to have different settings on the Kernel than the rest of the application, but needs the same bindings (that portion is for NHibernate and needs InjectNonPublic = true and the InjectAttribute set). How can a make a Kernel that shares bindings with the current kernel but has different settings?

I believe that in other IOC containers this is would be achieved with a “nested container”, however I don’t see any support for nested containers in Ninject?

  • 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-28T05:25:23+00:00Added an answer on May 28, 2026 at 5:25 am

    I eventually solved my problem. As I said, what I was really doing was trying to customize injection for use by NHibernate. In order to solve this I ended up using Ninject’s internal IOC to replace certain strategies for its behavior.

    Internally, Ninject uses an instance of ISelector to determine which constructors it should consider invoking. I reimplemented this class by decorating the standard Selector (I couldn’t subclass the standard and override because none of the methods are virtual). I could then conditionally change the SelectConstructorsForInjection() method’s behavior.

    public class HydratingSelector : DisposableObject, ISelector
    {
        private readonly ISelector standardSelector;
        private const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
    
        public HydratingSelector(IConstructorScorer constructorScorer,
                                 IEnumerable<IInjectionHeuristic> injectionHeuristics)
        {
            standardSelector = new Selector(constructorScorer, injectionHeuristics);
        }
    
        public IEnumerable<ConstructorInfo> SelectConstructorsForInjection(Type type)
        {
            if(Settings.GetHydratedTypeSelector()(type))
            {
                var constructors = type.GetConstructors(Flags);
                return constructors.Length != 0 ? constructors : null;
            }
    
            return standardSelector.SelectConstructorsForInjection(type);
        }
    
        // Omitted implementations of other methods that just forward to standardSelector
    }
    

    The above took care of essentially doing InjectNonPublic = true for the hydrated types. But I still needed to do the equivalent of setting the InjectAttribute for those types. This I did by replacing the HydratingConstructorScorer like so:

    public class HydratingConstructorScorer: DisposableObject, IConstructorScorer
    {
        private readonly IConstructorScorer standardScorer = new StandardConstructorScorer();
    
        public int Score(IContext context, ConstructorInjectionDirective directive)
        {
            if(Settings.GetHydratedTypeSelector()(directive.Constructor.DeclaringType)
                && directive.Constructor.HasAttribute(Settings.GetHydrateAttribute()))
                return int.MaxValue;
    
            return standardScorer.Score(context, directive);
        }
    
        // Omitted implementations of other methods that just forward to standardSelector
    }
    

    I then incorporated these new strategies by creating a special Kernel like so:

    public class HydratingKernel : StandardKernel
    {
        public HydratingKernel(params INinjectModule[] modules)
            : base(modules)
        {
    
        }
    
        public HydratingKernel(INinjectSettings settings, params INinjectModule[] modules)
            : base(settings, modules)
        {
        }
    
        protected override void AddComponents()
        {
            base.AddComponents();
            SetupComponentsForHydratingKernel(Components);
        }
    
        internal static void SetupComponentsForHydratingKernel(IComponentContainer components)
        {
            components.RemoveAll<ISelector>();
            components.Add<ISelector, HydratingSelector>();
            components.RemoveAll<IConstructorScorer>();
            components.Add<IConstructorScorer, HydratingConstructorScorer>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single line CEikLabel in my application that needs to scroll text.
I have a single NSDictionary object which contains a large number of custom objects.
We have a Single Statement FUNCTION in SQL Server 2005 which uses CONTAINSTABLE(). All
I have single html file that contains 10000 list items that needs to be
I have an MVC application using Ninject to connect to a single database. Now
I have an single threaded, embedded application that allocates and deallocates lots and lots
I have a single entry output from a paradox table which is imported into
I have a problem which seems very similar to the one described in http://markmail.org/message/6rlrzkgyx3pspmnf
I have single-thread windows form application written with VB.NET and targeting Framework 1.1 .
I've been working on a project which uses SPNEGO to have Single Sign On

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.