I'm trying to get this working, and I can't find a good solution. I know that StructureMap can do this, but I'm stuck with Ninject.
What I have:
public interface IFormHandler<T>
{
void Handle(T form);
}
And I want to implement multiple classes like this one:
public class RegioDeleteModelFormHandler:IFormHandler<RegioDeleteModel>
{
public void Handle(RegioDeleteModel form)
{
//implement method here
}
}
or this one:
public class RegioUpdateModelFormHandler:IFormHandler<RegioUpdateModel>
{
public void Handle(RegioUpdateModelform)
{
//implement method here
}
}
Somewhere else in the code, I would like to be able to call something like this:
var handler = DependencyResolver.Current.GetService<IFormHandler<T>>();
to get the correct IFormhandler back.
Is there a way to do this? If Ninject cannot do this, is there a workaround? Currently, I only have the base nuGet Package for Ninject. Is there something I can use from one of the related packages?
For completeness sake, here is my DependencyResolverclass:
public class NinjectDependencyResolver : IDependencyResolver { private IKernel kernel;
public NinjectDependencyResolver() {
kernel = new StandardKernel();
AddBindings();
}
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>();
}
public IKernel Kernel {
get { return kernel; }
}
private void AddBindings() {
Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
thanks in advance!
EDIT my edit: Maybe I should add that I tried this in the AddBindings method above and that it works, but that's a lot of code, if I have to do it for all possible IFormHandlers:
Bind<IFormHandler<RegioDeleteModel>>().To<RegioDeleteModelFormHandler>();
Use conventions: https://github.com/ninject/ninject.extensions.conventions/wiki/What-is-configuration-by-convention