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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:21:12+00:00 2026-06-04T04:21:12+00:00

I have an IConfig object that contains settings used throughout my application. At the

  • 0

I have an IConfig object that contains settings used throughout my application. At the moment, I inject the entire object into the constructor of each object that needs it, as follows:

public interface IConfig 
{
    string Username { get; }
    string Password { get; }
    //... other settings
}

public class Foo : IFoo
{
    private readonly string username;
    private readonly string password;

    public Foo(IConfig config)
    {
        this.username = config.Username;
        this.password = config.Password;
    }
}

The downside is that IConfig contains a large number of settings because it’s deserialised from an overall config file, so injecting the entire object is unnecessary. What I’d like to do is change the constructor to Foo(string username, string password) so that it only receives the settings it needs. This also makes creating Foo objects for testing much easier (not having to set up IConfig just to create Foo). I’d like to bind the constructor arguments directly in my NinjectModule, something like the following:

public class MyModule : NinjectModule
{
    public override void Load()
    {
        Bind<IConfig>().To<JsonConfig>()
            .InSingletonScope();

        Bind<IFoo>().To<Foo>()
            .WithConstructorArgument("username", IConfig.Username)
            .WithConstructorArgument("password", IConfig.Password);
    }
}

Obviously this code doesn’t work, but how would I go about doing what I wanted?

My original idea was to use the NinjectModule.Kernel to get the IKernel then get an instance of my IConfig object and inject the properties as required, but the object returned by NinjectModule.Kernel has no Get<T>() method.

  • 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-06-04T04:21:13+00:00Added an answer on June 4, 2026 at 4:21 am

    You are on the right track:

    The Kernel.Get<T>() method is an extension method defined on the ResolutionExtensions in the Ninject namepsace so with adding the using Ninject; it is available in your module as well.

    But instead of the Module.Kernel you should use the IContext provided in the second overload of WithConstructorArgument to get the Kernel:

    Bind<IFoo>().To<Foo>()
        .WithConstructorArgument("username", 
                                 context => context.Kernel.Get<IConfig>().Username)
        .WithConstructorArgument("password", 
                                 context => context.Kernel.Get<IConfig>().Password);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that allows administrators to specify valid IP addresses from which
Have a network location that shows paths in the 8.3 short format. I need
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
I have published an asp.net application to a sub directory of my IIS application.
I have a simple question. Is there a command that allows you to pull
I have a shell script that periodically checks the ADSL external IP address and
Running ipconfig /all shows a Teredo Tunneling Pseudo-Interface. What is that? Does this have
I have just setup VisualSVN on my local machine and on our server that
I have a network C++ program in Windows that I'd like to test for

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.