var kernel = new StandardKernel();
kernel.Bind<IContactRepository>().ToConstant(new ContactRepository());
config.ServiceResolver.SetResolver(
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.
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?
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).
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...
smolesen
Member
43 Points
37 Posts
ContactManager sample IoC
Feb 23, 2012 09:02 AM|LINK
Hi
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:
kernel.TryGet(t)
Only a single component is registered in the container:
kernel.Bind<IContactRepository>().ToConstant(new ContactRepository());
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??
TIA
Søren
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: ContactManager sample IoC
Feb 23, 2012 09:19 AM|LINK
It is no magic actually. See the below code:
var kernel = new StandardKernel(); kernel.Bind<IContactRepository>().ToConstant(new ContactRepository()); config.ServiceResolver.SetResolver( 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.
tweets as @tourismgeek
smolesen
Member
43 Points
37 Posts
Re: ContactManager sample IoC
Feb 23, 2012 10:10 AM|LINK
Hi tugberk_ugurlu_
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.
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: ContactManager sample IoC
Feb 23, 2012 10:44 AM|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.
tweets as @tourismgeek
smolesen
Member
43 Points
37 Posts
Re: ContactManager sample IoC
Feb 23, 2012 11:18 AM|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...
Thx,
Søren
Found a nice post about it here:
http://mikehadlow.blogspot.com/2011/02/mvc-30-idependencyresolver-interface-is.html
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: ContactManager sample IoC
Feb 23, 2012 11:45 AM|LINK
I understand.
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.
tweets as @tourismgeek