Inversion of Control with .net

It's rare that I hear someone using Inversion of Control (Ioc) principle with .Net. I have some friends that work with Java that use a lot more Ioc with Spring and PicoContainer.

I understand the principle of removing dependencies from your code... but I have a doubt that it's so much better.

Why do .Net programmers not use (or use less) those types of frameworks? If you do, do you really find a positive effect in the long term?


Lots of people use IOC in .NET, and there are several frameworks available to assist with using IoC. You may see it less in the WinForms side of things, because it's harder to just let the container wire everything together when you are designing forms in Visual Studio, but I can say that for server-side .NET applications, where I work at least, IoC is used very successfully.

Why use it in .NET? For the same reason you use it everywhere else. The 2 biggest things I like are:

  • Designing for IoC tends to enforce good coding practice - designing to interfaces, low coupling, high cohesion. This also leads to classes that are very easy to unit-test.
  • System configuration can often be changed without recompiling.
  • Some other posts discussing the different IoC/DI frameworks available for .NET:

  • Which C#/.net Dependency Injection frameworks are worth looking into?
  • Which Dependency Injection Tool Should I Use?

  • I use StructureMap for dependency injection and have only recently started using it with iBATIS.NET to inject our domain object mappers at runtime (and not through an XML config file, no thanks!).

    I've seen immediate benefits. Creating interfaces for all our mappers (such as IPersonMapper ) and then adding Moq allows me to write some pretty great database-free unit tests quickly and easily.

    Previously (.NET 1.0) I wrote my own plugin system mainly to learn about reflection. Since that time I've implemented some sort of IoC in my projects. Its only recently I started using IoC for making unit tests so much less painful to write. I couldn't imagine doing it any other way at this point.


    IoC is not really that commonplace in .Net up until now. And it has everything to do with Microsoft and there promotion campaigns they did. up until now they were more emphasizing the RAD capabilities of VS and in the meanwhile the forgot to promote things like IoC and Di but now they have their own framework called Unity and with the work they did on ASP.Net MVC.

    So I guess the majority of people will start to use things like that. Because know they have a MS alternative to use.

    And I use StructureMap.

    链接地址: http://www.djcxy.com/p/82124.html

    上一篇: 良好的国际奥委会框架与asp.net mvc一起使用?

    下一篇: .net的控制反转