面向对象设计一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC、DI以及Ioc容器等概念
依赖倒置DIP
依赖倒置原则,它转换了依赖,高层模块不依赖于低层模块的实现,而低层模块依赖于高层模块定义的接口
控制反转(IoC)
控制反转(IoC),它为相互依赖的组件提供抽象,将依赖(低层模块)对象的获得交给第三方(系统)来控制,即依赖对象不在被依赖模块的类中直接通过new来获取
依赖注入(DI)
就是将依赖对象的创建和绑定转移到被依赖对象类的外部来实现
IoC容器
IoC容器实际上是一个DI框架,
它能简化我们的工作量。它包含以下几个功能:
- 动态创建、注入依赖对象。
- 管理对象生命周期。
- 映射依赖关系。
比较流行的Ioc容器有以下几种:
1. Ninject: http://www.ninject.org/
2. Castle Windsor: http://www.castleproject.org/container/index.html
3. Autofac: http://code.google.com/p/autofac/
4. StructureMap: http://docs.structuremap.net/
5. Unity: http://unity.codeplex.com/
内容尽在不断完善中,谢谢了解