问题
Which IoC to consider while developing ASP.NET Web application project and what are the advantages of different IoC?
- ObjectBuilder
- Unity
- Spring.NET
回答1:
It is not in your list but consider looking at Castle Windsor
Number of facilities such as supporting Factories so that the factory product is injected not the factory itself or the Wcf facility for injecting a proxy or creating a host with dependencies.
Has a nice fluent interface for wiring up individual items but preferably you would use a convention to auto wire up so that you don't have to list each component.
Some other nice features:
- Requires a single dll since new revision.
- Supports injecting lists and arrays via SubDependancy resolvers
Here is a poll taken this year showing the favoured IoC. Unity seems to be top but that is probably because it is a Microsoft product and Microsoft workshops will pick it up by default.
回答2:
We use StructureMap on an ASP.NET MVC project and have found it incredibly useful. The only real downside is the limited documentation. The auto-wiring works really well and it requires very little configuration if you use strong conventions to name your classes and interfaces, e.g.:
public interface ISomething { ... }
public class Something: ISomething { ... }
Prior to that we used Spring.NET, although we ditched that because it does a lot of other (non-IoC) things that we didn't really need and required complex XML configuration to do what we needed. As an IoC container it worked fine though and was highly flexible.
The key thing I'd recommend whatever you choose is coding to allow use of IoC - we use interfaces all over the place. This really does help unit testing and all the benefits that brings too.
The Unity stuff in MVC3 does look interesting too but not had a chance to play with it yet.
回答3:
I doubt many people have used more than two or three of these, and in my quite limited experience with Unity and Ninject there isn't that much difference in the end result. They have slightly different options for configuring the mappings, and I'm sure some of the more advanced features differ but these are probably rarely used. At the end of the day the core requirements for an IoC container aren't that complicated - you could quite easily roll your own.
Far more important is that you are using an IoC pattern, which particular brand you choose probably has no significant impact on the quality of the resulting application.
For what it's worth I'd recommend Ninject if only because it is very lightweight.
来源:https://stackoverflow.com/questions/4509458/ioc-comparisions