惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
爱范儿
爱范儿
WordPress大学
WordPress大学
V
V2EX
宝玉的分享
宝玉的分享
小众软件
小众软件
B
Blog
博客园 - 叶小钗
U
Unit 42
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
H
Help Net Security
P
Proofpoint News Feed
D
Docker
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
月光博客
月光博客

博客园 - 芭蕉

Model-View-ViewModel(MVVM) with MEF 使用EnvDTE自动格式整个工程 Duck Typing in C# - 芭蕉 Dynamic Proxy C# 4.0 Dynamic --通过DLR直接调用IronPython 介绍一个Python进行函数式编程时有用的module - 芭蕉 - 博客园 lambda in F# - 芭蕉 UML类关系图标识速记 Python functions VisualStudio quick tips -- 快速在多个打开的代码文件间切换 VisualStudio quick tips --快速调整字体大小 结合实例实习F#(三)--理解函数式语言中的函数 VisualStudio quick tips --快速打开Properties Page 结合实例学习F#(二) --基本数据类型Discriminated Unions 结合实例学习F#(一) --快速入门 Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance Factory Method 顺时钟方向螺旋状打印矩阵元素 玩转Visual Studio ---Debug篇
Inversion of Control
芭蕉 · 2010-01-07 · via 博客园 - 芭蕉

 The following article is mainly from Prism documentation, it's just a review of IoC.

Having class depends on services or components whose concrete type is specified at the design time introduced several problems:

1.       Update the dependencies need change the class who depends on them

2.       Concrete dependencies must be available at compile time in order to build your type.

3.       Hard to test, couldn’t use Mock or Stubs.

4.       Manage the dependencies is a nightmare.

Solution:  Your class does not create other objects on which they rely to do their work.  Instead, they get these objects that they need from outside source.

Implementation Details:

The IoC pattern can be implemented in several ways. The Dependency Injection (DI) and the Service Locator patter (also known as IoC container) are two common ways.

1.       Dependency Injection:  a specialization of the IoC, uses a builder object to initialize object and provide the required dependencies to the object.  There are two main form of dependency injection:

·         Constructor injection: use parameters of the objects constructor method to express dependencies and to have the builder inject it with its dependencies.

·         Setter injection: the dependencies are expressed through setter properties.

2.       Service Locator: create a service locator that contains the dependencies and the service locator encapsulates the logic to locate them. In your class, use the service locator to obtain service instances.                                                                                                                                    

 

Note: the service locator does not instantiate the services. It provides to way to register services and it holds references to the services. Once the service is registered, the locator can find the service.  The service locator should provide a way to locate a service without specifying the concrete type.

Sample Code:

代码

 interface IIoCContainer
    {
        T Resolve
<T>();
    }
public class SimpleContainer : IIoCContainer
    {
        
readonly Dictionary<Type, object> _container = new Dictionary<Type, object>();public T Resolve<T>()
        {
            
return (T)_container[typeof(T)];
        }
public void Register<T>(object obj)
        {
            
if (obj is T == false)
            {
                
throw new InvalidOperationException("Invalid operation");
            }
            _container.Add(
typeof(T), obj);
        }
    }

 For the more complex container, refer to:

  1. Unity Application Block: http://www.codeplex.com/unity/

  2. Windsor Container: http://www.castleproject.org/container/index.html