Inversion of Control

 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

posted @   芭蕉  阅读(238)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示