.NET - What IoC Container framework would you reco

2019-03-14 03:27发布

问题:

What Inversion of Control Container framework would you recommend to a beginner in this area?

回答1:

James Kovacs did a dnrtv screencast where he shows how to create your own IOC container. I recommend this to a beginner not because it's necessarily the one you want to use but because it shows the basics of why you need an IOC container and what the basic functionality is. After this, try moving on to Castle Windsor or Structure Map.



回答2:

Autofac.

Lightweight, simple, fast, C# 3.0-oriented and works (for me) better in complex projects than any other IoC Container (even Windsor).

Primary features:

  • Deterministic disposal of components
  • Expression-based configuration
  • Proper support for multi-scoped component resolution
  • High performance
  • Lean codebase


回答3:

I recall there are some really simple tutorials from BitterCoder on Castle's Windsor which I found helpful and easy to follow.

Even if you decide to use Windsor or not, those walkthroughs should still give you a good idea of the types of features / functionality to expect from other containers.

  • Part 1 - Simple Configuration
  • Part 2 - More Configuration (Arrays)
  • Part 3 - Still More Configuration (Dictionaries)
  • Part 4 - Switching Configurations
  • Part 5 - Configuration Parameters
  • Part 6 - Switching Between Lifestyles
  • Part 7 - Switching Implementations
  • Part 8 - Referencing Implementations By Key
  • Part 9 - Constructor Injection
  • Part 10 - Setter Injection
  • Part 11 - Factories
  • Part 12 - Decorators
  • Part 13 - Injecting Service Arrays
  • Part 14 - Startable Facility


回答4:

Ninject seems to get a lot of support in these questions

https://stackoverflow.com/questions/71041/which-single-iocdi-container-would-you-recommend-using-and-why

Which .NET Dependency Injection frameworks are worth looking into?



回答5:

There is a great book out by Mark Seemann called Dependency Injection In .Net. The latter half of the book covers 6 popular frameworks in enough detail to get a feel for them. It's worth a look to help a new person decide. He also covers poor man's dependency injection for those not ready to try a container, but want to at least write their application in a manner that can easily be converted to use an IoC container.

The containers he covers are:

  • Castle Windsor
  • StructureMap
  • Spring.Net
  • Autofac
  • Unity
  • MEF (though I believe he says that MEF isn't exactly an IoC container)


回答6:

Danial Cazzulino has a bunch (scrunch?) of screencasts on creating the Funq DI Container. Funq is lightweight and fast but lacks some of the bells and whistles of the larger, more established frameworks.

I use the Funq-derived Munq which is also lightweight and fast and is a simple way to add a DI Container to MVC3 projects via Nuget. Up to now I haven't found any need to move to a different framework - Munq has been very adequate.



回答7:

I have done some evaluation of the Spring.NET port. It offers AOP, config-file setup of the container, and other cool stuff. I think their site is well organized and the docs are well written.



回答8:

I would say StructureMap. It is one of the more mature framework and really easy to use. Moreover it can integrate well with NMock to create mock objects which are invaluable for unit testing. The Microsoft supported framework is Unity so you may look at that as well.



回答9:

I agree with dp. The author of that screencast even says that you should roll your. This is so you will know how they work. Then once you have that down you can move on to the more full featured one's.

It has also been mentioned that there really isn't that a 'one IOC container to rule them all'. This is because they pretty much all have the same features. It comes down to which implementation you favor. For instance, if you are using the Enterprise Library Framework then heavily then you might favor the Unity container because you get free tie-ins to the rest of EntLib. Or maybe you are allergic to XML then you can use Ninject which has a very nice fluent interface. Each framework has its strengths and weaknesses but they will pretty much be the same from the beginners stand point.