ASP.NET MVC - Using Ninject bindings outside of controllers
I'm using ASP.NET MVC3, and Ninject. I've set up the standard code implementation in "AppStart_NinjectMVC3.cs" that sets up the bindings and adds a kernel to the DependencyResolver like this:
public static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUserRepository>().To<UserRepository>();
...
}
开发者_JS百科 public static void Start() {
IKernel kernel = new StandardKernel();
RegisterServices(kernel);
DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
}
All is working well in my controllers - dependencies are being resolved fine.
I'd like to be able to use Ninject and these bindings outside of controllers, and outside of the MVC stack. For example, I have a bunch of regular aspx pages in which I'd like to use my ninject kernel, and some code hanging off global.asax too.
Can I re-use my Ninject kernel in these other places, or do I need to also register a kernel in my Global.asax appstart?
The current development release found on http://teamcity.codebetter.com provides support for side a side usage of ordinary aspx pages, mvc and wcf. You might want to have a look at this.
Be aware this is a development version and it is not tested very well. Nevertheless, I think it should be pretty much stable. But as it is work in progress it the interface can change. Also I won't give a lot of support before I have written the Ninject 2.4 preview blog about this change.
You need
- Ninject
- Ninject.Web.Common
- Ninject.Web
- Ninject.Web.MVC3
I've used the Ninject MVC Extension within my ASP.NET MVC application.
Here is the manner in which I've achieved what I think you're 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's 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
}
}
}
精彩评论