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

  • Home
  • SEARCH
  • 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 6802245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:11:59+00:00 2026-05-26T19:11:59+00:00

We are using Ninject in our MVC3 project to do dependency injection. I used

  • 0

We are using Ninject in our MVC3 project to do dependency injection.
I used NuGet to add package references to Ninject and Ninject.MVC3 packages. When I did this it created a NinjectMVC3 class in my App_Start folder:

public static class NinjectMVC3
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    public static void Start()
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
        DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
        bootstrapper.Initialize(CreateKernel);
    }

    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        RegisterServices(kernel);
        return kernel;
    }

    private static void RegisterServices(IKernel kernel)
    {           
        kernel.Bind<IPrincipal>().ToMethod(c => HttpContext.Current.User);
    }
}

So far this has worked great to resolve dependencies in my controllers:

public class HomeController : Controller {
    protected IPrincipal principal { get; set; }

    public HomeController(IPrincipal principal) {
        this.principal = principal;
    }
}

This controller has a dependency on IPrincipal which I have set up in my bootstrapper class to resolve to HttpContext.Current.User. I have another class which has a dependency on IPrincipal that is not a controller:

public class NonControllerClass
{
    protected IPrincipal Principal { get; set; }

    public NonControllerClass(IPrincipal principal) {
    }
}

How would i go about resolving this dependency? How would I do it if it wasn’t a parameter of the constructor?
}

  • 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-26T19:12:00+00:00Added an answer on May 26, 2026 at 7:12 pm

    Well, ideally this should never be a problem. All dependencies should be inected into your controller, and any dependencies those dependencies depend on should automatically get injected as well.

    In MVC, (almost) everything starts at the controller. So you might have:

    public class HomeController : Controller { 
        protected IMyService myService { get; set; } 
    
        public HomeController(IMyService myService) { 
            this.myService = myService; 
        } 
    } 
    
    public class MyService {
        protected IPrincipal principal;
    
        public MyService(IPrincipal principal) { this.principal = principal)
    }
    

    Notice how you don’t have to do anything, your service automatically gets injected with the right dependencies because your service was injected into your controller.

    However, there might be times you need to dynamically create an object. In that case, you can use the MVC DependencyResolver.

    var principal = DependencyResolver.Current.GetService<IPrincipal>();
    

    You should avoid doing this unless absolutely necessary, however, because this is considered an anti-pattern (known as Service Location). Sometimes you don’t have much choice though.

    If you don’t want to use constructor injection, you can use property injection.

    public class MyService {
        [Inject]
        public IPrincipal principal {get; set;}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On our current WPF project, we've been performing dependency injection using the Ninject IOC
I am using Dependency Injection in my code (with Ninject ) and thought I
I'm using Ninject to do some dependancy injection. (Mainly for the DAL), my project
I'm just getting started with Dependency Injection (DI) using Ninject and am working through
I am thinking about using Microsoft Unity for my Dependency Injection tool in our
I'm using Ninject in an MVC project and I've used the autoregistration features in
I'm using Ninject to do dependency injection. I have a userService in which I
I'm using Ninject for dependency injection in my application. Here's an example of one
I want to map DbConnection to an un-opened SqlConnection using Ninject . This is
I have set up a NInject (using version 1.5) binding like this: Bind<ISessionFactory>().ToMethod<ISessionFactory>(ctx =>

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.