Inversion of Control

本文探讨了类依赖问题带来的挑战,如更新依赖项时需要修改依赖该类的代码,具体的依赖项必须在编译时可用等。为解决这些问题,文章介绍了IoC(Inversion of Control)的概念,并详细讨论了两种实现方式:依赖注入(DI)和服务定位器模式。通过这两种方式,可以有效地分离组件间的耦合,提高代码的可维护性和可测试性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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:

 

ExpandedBlockStart.gif 代码
  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

转载于:https://www.cnblogs.com/hiber/archive/2010/01/07/1641148.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值