ASP.NET MVC - Using Ninject Bindings Outside of Controllers

I am using ASP.NET MVC3 and Ninject. I set the standard implementation of the code to "AppStart_NinjectMVC3.cs", which sets the bindings and adds the kernel to the DependencyResolver as follows:

public static void RegisterServices(IKernel kernel) { kernel.Bind<IUserRepository>().To<UserRepository>(); ... } public static void Start() { IKernel kernel = new StandardKernel(); RegisterServices(kernel); DependencyResolver.SetResolver(new NinjectServiceLocator(kernel)); } 

Everything works well in my controllers - dependencies are resolved normally.

I would like to be able to use Ninject and these bindings outside the controllers and outside the MVC stack. For example, I have a bunch of regular aspx pages in which I would like to use my ninject kernel, as well as some code dependent on global.asax.

Can I reuse my Ninject kernel in other places or do I need to register the kernel in my global.asax application as well?

+4
source share
2 answers

The current development version, found at http://teamcity.codebetter.com , provides third-party support for using regular aspx, mvc, and wcf pages. Perhaps you should take a look at this.

Remember that this is a development version and it is not very well tested. However, I think it should be fairly stable. But since this is a work in progress, the interface may change. Also, I will not provide much support before I write a Ninject 2.4 preview blog about this change.

You need

  • Ninject
  • Ninject.Web.Common
  • Ninject.Web
  • Ninject.Web.MVC3
+2
source

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:

 /// <summary> /// Configures how abstract service types are mapped to concrete implementations /// </summary> internal class ServiceModule : NinjectModule { public override void Load() { Bind<IConfiguration>().To<XXX.myNamespace.Configuration>(); // snip... all other bindings to interfaces } } 

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(); // snip additional logic... } catch(Exception ex) { // snip } } } 
0
source

Source: https://habr.com/ru/post/1345225/


All Articles