Using Ninject as my IDependencyResolver has serious issues? Please assist
-
29-10-2019 - |
题
I created an NinjectResolver which implements IDependencyResolver. In the global.asax, I'm setting this guy as my DependencyResolver.
However, in doing so, I lose all of my data-dash attributes on validaiton. And it also messes up the entire client-side paging, sorting on my grid (using Telerik's MVC extensions).
Here's the class:
public class NinjectResolver : IDependencyResolver
{
private readonly IKernel _kernel;
public IKernel Kernel
{
get
{
return _kernel;
}
}
public NinjectResolver(params Ninject.Modules.INinjectModule[] modules)
{
_kernel = new StandardKernel(modules);
}
public object GetService(Type serviceType)
{
return _kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _kernel.GetAll(serviceType);
}
public IBindingToSyntax<T> Bind<T>()
{
return _kernel.Bind<T>();
}
}
And here's my app start in global.asax ...
protected void Application_Start()
{
.... omitted
var modules = new Ninject.Modules.INinjectModule[]
{
new PersistenceModule()
};
var dependencyResolver = new NinjectResolver(modules);
DependencyResolver.SetResolver(dependencyResolver);
.... omitted
}
If I comment out the SetResolver line, all my client-side validation works as well as my client-side grid sorting, paging, filtering, etc. Unfortunately, I lose all my constructor injection.
Any help is appreciated please. Not sure if I have to add more modules for client side operations to work?
Please advise.
解决方案
Why don't you use https://github.com/ninject/ninject.web.mvc/wiki/MVC3 instead of writing your own implementation? It is widely used and proven to work in many projects.