Last post Feb 23, 2012 11:45 AM by tugberk_ugurlu_
Feb 23, 2012 09:02 AM|smolesen|LINK
I've been looking on the ContactManager sample, trying to figure out how that works, to do something similar with Windsor...
I'm not very familiar with Ninject, but it seems like some magic is going on in the:
Only a single component is registered in the container:
but for some reason, Ninject is still able to resolve the ContactController and inject the repository....
Are the ContactController somehow added to the container automatically??
Feb 23, 2012 09:19 AM|tugberk_ugurlu_|LINK
It is no magic actually. See the below code:
var kernel = new StandardKernel();
t => kernel.TryGet(t),
t => kernel.GetAll(t));
t is the type which is requested and the IoC container (ninject, autofac or anything else) can supply the value there. ServiceResolver will resolve the controller with paramaters.
In fact, you can Write a Custom Dependency Resolver if you want. See this article: http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver
This provides a cleaner way. You can hook up Windsor there easiliy.
Feb 23, 2012 10:10 AM|smolesen|LINK
If I look at the 'Write a Custom Dependency Resolver', they explicit returns an instance of 'ContactsController' which make sence...
But how is Ninject able to resolve the ContactsController, when it's not added anywhere?? Is Ninject just able to resolve a concrete type without haveing it added somehow?
I would have expected something like:
In Windsor it would not work, unless I add the ContactController to the container... but maybe that's a difference between Windsor and Ninject.
Feb 23, 2012 10:44 AM|tugberk_ugurlu_|LINK
Please have a look at "Dependency Injection with IoC Containers" section there.
Honestly, I really do not like how windsor works with ASP.NET MVC and since the web api DependencyResolver implementation is nearly the same with ASP.NET MVC, you will have the same issue If I may guess. AFAIK, Windsor cannot be integrated with IDependencyResolver
in ASP.NET MVC (I tried to enable that once by writing a class which implements IDependencyResolver and uses windsor. It fails due to couple of reasons which made no sense to me).
There are other great libraries out there which are better in terms of simplicity and performance (StructureMap and Autofac).
I hope this helps.
Feb 23, 2012 11:18 AM|smolesen|LINK
Well, the ting is that I have a rather large project using WCF Web Api and Windsor, that I'd like to migrate to ASP.NET Web Api and changing the IoC from Windsor to something else is not really an option....
Seems like it's possible to resolve controllers, by inplementing a IHttpControllerFactory, since there's a ReleaseController method, but that'll only work for controllers... doesn't seem like there a similar factory for Formatters and "DelegatingHandlers"
So maybe I'll have to stick with Web Web Api for now, and hope that the final release of ASP.NET Web Api will make it possible...
Found a nice post about it here:
Feb 23, 2012 11:45 AM|tugberk_ugurlu_|LINK
Please follow the Brad Wilson's ASP.NET MVC 3 Service Location series of blog posts: http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html
I am not completely sure but definately positive on that WCF Web API adopts this approach as well.