I used Ninject MVC Extension in my ASP.NET MVC application.
This is how I achieved what I think you are trying to accomplish.
Global.asax.cs:
public class MvcApplication : NinjectHttpApplication { /// <summary> /// Overridden Ninject method that is called once the application has started and is initialized /// </summary> protected override void OnApplicationStarted() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); // Tell the MVC Framework to use our implementation of metadataprovider. ModelMetadataProviders.Current = new XXX.myNamespace.MetadataProvider(); // Tell the MVC Framework to use our CartModelBinder class ModelBinders.Binders.Add(typeof(Cart), new CartModelBinder()); } /// <summary> /// Establish a reference to our DIFactory object /// <remarks> /// This application currently uses Ninject for dependency injection. /// </remarks> /// </summary> /// <returns></returns> protected override IKernel CreateKernel() { return DIFactory.GetNinjectFactory(); } // snip... additional global.asax.cs methods }
DIFactory.cs:
/// <summary> /// This class is used as a container for dependency injection throughout the entire application /// </summary> public class DIFactory { public static IKernel _kernel = null; /// <summary> /// Method used to create a single instance of Ninject IKernel /// </summary> /// <returns>IKernel</returns> public static IKernel GetNinjectFactory() { if (_kernel == null) { var modules = new INinjectModule[] { new ServiceModule() }; _kernel = new StandardKernel(modules); } return _kernel; } /// <summary> /// Method used as a service locator for the IConfiguration interface /// </summary> /// <returns></returns> public static IConfiguration CreateConfigurationType() { return _kernel.Get<IConfiguration>(); } // snip....additional public static methods for all other Interafaces necessary }
ServiceModule.cs:
Use in other classes besides controllers:
UserInteraction.cs:
public class UserInteraction : IUserInteraction { private IConfiguration configuration; public bool SubmitFeedback(Feedback feedback) { try { this.configuration = DIFactory.CreateConfigurationType();
source share