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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:28:33+00:00 2026-05-23T12:28:33+00:00

I have global flags which enable/disable features. I’d like to inject some dependencies depending

  • 0

I have global flags which enable/disable features. I’d like to inject some dependencies depending on some flag. Some features require classes which are heavily constructed so I want to inject null if the value of the flag is false and the actual dependency otherwise. Ninject doesn’t allow injecting null. Are there any other options?

Update: constructor arguments can be decorated with OptionalAttribute attribute. In this case null is injected if there is no corresponding binding found. There is a problem here: I can’t verify if target class can be properly constructed. I have a test for each public dependency which verifies if it can be constructed successfully. In case if the value of the flag is true I will not be able to find the error when the dependency decorated with the OptionalAttribute attribute, cannot be constructed properly. I’d like to manage it on binding level only.

  • 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-23T12:28:34+00:00Added an answer on May 23, 2026 at 12:28 pm

    You can vary the injection behaviour by binding using a factory method (i.e. ToMethod), and it’s possible to allow injection of nulls by configuring the container’s AllowNullInjection setting.

    Another alternative would be to use a factory method and supply a lightweight dummy object instead of your heavy-weight class. If you are using interfaces this would be straightforward, just have implementations of the interface that do nothing. You could even use a mocking framework such as FakeItEasy to construct these dummies for you. The benefit here, is that the dummy makes the special behaviour transparent to clients i.e. clients do not need to check for null, etc.

    An example of using a factory method, plus AllowNullInjection and nulls:

    public void Configure()
    {
        bool create = true;
    
        IKernel kernel = new StandardKernel();
    
        kernel.Settings.AllowNullInjection = true;
    
        kernel.Bind<IFoo>().ToMethod(ctx => create ? ctx.Kernel.Get<Foo>() : null);
    
        DependendsOnIFoo depFoo = kernel.Get<DependendsOnIFoo>();
    }
    
    private interface IFoo {}
    
    private class Foo : IFoo {}
    
    private class DependendsOnIFoo
    {
        public DependendsOnIFoo(IFoo foo) {}
    }
    

    And an example where a lightweight object is substituted depending on the flag:

    public void Configure()
    {
        bool heavy = true;
    
        IKernel kernel = new StandardKernel();
    
        kernel.Bind<IFoo>()
         .ToMethod(ctx => heavy ? ctx.Kernel.Get<HeavyFoo>() : (IFoo)new DummyFoo());
    
        DependendsOnIFoo depFoo = kernel.Get<DependendsOnIFoo>();
    }
    
    private interface IFoo {}
    
    private class HeavyFoo : IFoo {}
    
    private class DummyFoo : IFoo { }
    
    private class DependendsOnIFoo
    {
        public DependendsOnIFoo(IFoo foo) {}
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have global static variables in a C library, which generate exceptions in a
In Actionscript you can have global variables like this: var number : Number =
I have some global variables in a Python script. Some functions in that script
I have a global pointer variable char* pointer = new char[500]; /* some operations...
If I have a global static variable x like in this code #include <stdio.h>
So i have this issue : i am declaring some extern global variables in
I have the following RegEx: ^(?!#)(?<=.+)$\r (with the global and multiline flags set) What
In my useful hotkeys program, i have a global hotkey which sets your current
I am trying to define a class in the global scope which contains some
Can Dynamic libraries have global, local and static variables in linux? If yes, then

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.