问题描述:

I've been playing around with a couple of DI containers and I like TinyIoC. I was first introduced to it through Nancy/fx.

I'm trying to use it on a current MVC project but cannot seem to get it to instantiate controllers that contain interface parameters in the constructor.

From what I understand it should be straight forward. It even comes with an AutoRegister method takes care of basic registration. I've implemented the IControllerFactory that allows me to override the DefaultControllerFactory for MVC, which simply instantiates controllers, BTW, it does not perform and DI.

I've also implemented IDependancyResolver to leverage TinyIoC. But instantiating controllers doesn't work. Do you have sample code I can see that illustrates how to use it for instantiating controllers using TinyIoC?

Here's what I have so far.

ControllerFactory Code

public class TinyIoCControllerFactory : IControllerFactory

{

private readonly TinyIoCContainer _container;

public TinyIoCControllerFactory(TinyIoCContainer container)

{

_container = container;

}

public IController CreateController(RequestContext requestContext, string controllerName)

{

return (_container.CanResolve<IController>(controllerName))

? _container.Resolve<IController>(controllerName)

: null;

}

public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)

{

return SessionStateBehavior.Default;

}

public void ReleaseController(IController controller)

{

var disposeable = (IDisposable) controller;

if (disposeable == null) return;

disposeable.Dispose();

}

}

DependencyResolver Code

public class TinyIoCResolver : IDependencyResolver

{

private readonly TinyIoCContainer _container;

public TinyIoCResolver(TinyIoCContainer container)

{

_container = container;

}

public object GetService(Type serviceType)

{

return _container.Resolve(serviceType);

}

public IEnumerable<object> GetServices(Type serviceType)

{

return _container.ResolveAll(serviceType, true);

}

}

Registration code

public static void RegisterContainer(TinyIoCContainer container)

{

ControllerInstaller.Install(container);

RepositoryInstaller.Install(container);

container.Register<IControllerFactory>(new TinyIoCControllerFactory(container));

DependencyResolver.SetResolver(new TinyIoCResolver(container));

}

public static void Install(TinyIoCContainer container)

{

container.Register<ISomeDBContext, SomeDbContext>();

container.Register<ISomeRepository, SomeRepository>();

}

public static void Install(TinyIoCContainer container)

{

container.Register<IController, HomeController>();

container.Register<IController, TestController>();

}

Do I need to use the UsingConstructor() method to force instantiation? If so, how do I use it?

Here's a snapshot of the error just for Home controller which has no constructor parameters.

The error message is the same for controllers with arguments as well. The end result is that no controllers are being instantiated.

Thanks

Tim

网友答案:

With a decent dependency injection container you don't have to create your own IControllerBuilder. You only need to register your TinyIoCResolver (IDependencyResolver) and the ASP.NET MVC will do the rest.

相关阅读:
Top