• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#控制反转(IOC:InverseOfControl)&依赖注入(DI:IndependenceInject) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

      举例:在每天的日常生活中,我们离不开水,电,气。在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭。而城市化之后,人们从这些琐事中解放了出来,城市中出现了水厂,发电厂,燃气公司。水,电,气我们自己打开开关用就可以而不用关心这些都是怎么来的,怎么实现的。控制反转指的就是由我们自己提供水电气发展为由政府提供水电气的整个过程。

      可是在现实的编程世界中,我们只能怎么才能实现控制反转呢?这时依赖注入(DI: Independence Inject)出场了。依赖注入主要分为构造函数, 属性注入和依赖方法注入,注入。下面通过Unity框架对三种方式分别进行了实现:

电厂接口:IElectricity

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTesting
{
    interface IElectricity
    {
        string ProvideElectricity();
    }
}
View Code

电厂类:ElectricityFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTesting
{
    class ElectricityFactory:IElectricity
    {
        public string ProvideElectricity()
        {
            return "The electricity is coming!";
        }
    }
}
View Code

水厂接口:IWater

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTesting
{
    interface IWater
    {
        string ProvideWater();
    }
}
View Code

水厂类:WaterFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTesting
{
    class WaterFactory:IWater
    {
        public string ProvideWater()
        {
            return "The water is coming!";
        }
    }
}
View Code

人接口(人会用水,用电):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTesting
{
    public interface People
    {
        string UsingWater();
        string UsingElectricity();
    }
}
View Code

居民类(用构造函数,属性注入,依赖方法注入,三种方式分别对居民用水,用电方法进行的具体实现):

using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTesting
{
    #region 构造器注入
    class ResidentsA:People
    {
        WaterFactory _wf;
        ElectricityFactory _ef;
        public ResidentsA(WaterFactory wf,ElectricityFactory ef)
        {
            this._ef = ef;
            this._wf = wf;
        }
        public string UsingWater()
        {
            Console.WriteLine("I want to drink!");
            return _wf.ProvideWater();
        }

        public string UsingElectricity()
        {
            Console.WriteLine("I want to cook some food!");
            return _ef.ProvideElectricity();
        }
    }
    #endregion

    #region 属性注入
    class ResidentsB : People
    {
        //属性注入
        [Dependency]
        public IWater _w { set; get; }
        [Dependency]
        public IElectricity _e { set; get; }

        public string UsingWater()
        {
            Console.WriteLine("I want to drink!");
            return _w.ProvideWater();
        }

        public string UsingElectricity()
        {
            Console.WriteLine("I want to cook some food!");
            return _e.ProvideElectricity();
        }
    }
    #endregion

    #region 依赖方法注入
    class Residents : People
    {
        public IWater _iw;
        public IElectricity _ie;

        public string UsingWater()
        {
            Console.WriteLine("I want to drink!");
            return _iw.ProvideWater();
        }

        public string UsingElectricity()
        {
            Console.WriteLine("I want to cook some food!");
            return _ie.ProvideElectricity();
        }

        [InjectionMethod]
        public void UsingWaterTest(IWater iw)
        {
            Console.WriteLine("I want to drink!");
            this._iw = iw;
        }

        [InjectionMethod]
        public void UsingElectricityTest(IElectricity ie)
        {
            Console.WriteLine("I want to cook some food!");
            this._ie = ie;
        }
    }
    #endregion
}
View Code

App.config配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <container>
      <!--register type="full class name,namespace"-->
      
      <register type="IOCTesting.IWater,IOCTesting" mapTo="IOCTesting.WaterFactory,IOCTesting">
        <lifetime type="singleton"/>
      </register>

      <register type="IOCTesting.IElectricity,IOCTesting" mapTo="IOCTesting.ElectricityFactory,IOCTesting">
        <lifetime type="singleton"/>
      </register>

      <!--<register type="IOCTesting.People,IOCTesting" mapTo="IOCTesting.ResidentsA,IOCTesting">
        <lifetime type="singleton"/>
      </register>-->
      <!--<register type="IOCTesting.People,IOCTesting" mapTo="IOCTesting.ResidentsB,IOCTesting">
        <lifetime type="singleton"/>
      </register>-->
      <register type="IOCTesting.People,IOCTesting" mapTo="IOCTesting.Residents,IOCTesting">
        <lifetime type="singleton"/>
      </register>
    </container>
  </unity>
</configuration>
View Code

具体的调用类Program.cs:

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            //配置文件注册
            IUnityContainer mycontanier = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)System.Configuration.ConfigurationManager.GetSection("unity");
            section.Configure(mycontanier);

            //创建调用者
            //Console.WriteLine("-------------------Constructor Injection-------------------");
            //ResidentsA pa = mycontanier.Resolve<People>() as ResidentsA;
            //Console.WriteLine(pa.UsingWater());
            //Console.WriteLine(pa.UsingElectricity());

            //Console.WriteLine("-------------------Property Injection-------------------");
            //ResidentsB pb = mycontanier.Resolve<People>() as ResidentsB;
            //Console.WriteLine(pb.UsingWater());
            //Console.WriteLine(pb.UsingElectricity());

            Console.WriteLine("-------------------Method Injection-------------------");
            Residents p = mycontanier.Resolve<People>() as Residents;
            Console.WriteLine(p._iw.ProvideWater());
            Console.WriteLine(p._ie.ProvideElectricity());

            Console.ReadLine();
        }
    }
}
View Code

(备注:Unity框架需要提前自己安装,其实就是引用了两个包而已,在测试的过程中,如需测试构造函数注入,则需要在App.config和Program.cs中注释掉属性注入,依赖方法注入部分,否则会报错。)

ps. 1. 依赖方法注入会在被依赖对象创建时自动调用;

     2. 由于被依赖对象是在程序运行时创建的所以这里面又用到了反射的技术,对于反射,我会继续创建反射的例子进行说明。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
转:不用AJAX实现前台JS调用后台C#方法(小技巧)发布时间:2022-07-13
下一篇:
C#设计的一个向导程序(Wizard)框架发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap