zoukankan      html  css  js  c++  java
  • Unity依赖注入使用

    构造器注入(Constructor Injection):IoC容器会智能地选择选择和调用适合的构造函数以创建依赖的对象。如果被选择的构造函数具有相应的参数,IoC容器在调用构造函数之前会自定义创建相应参数对象;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1.创建一个UnityContainer对象
                //2.通过UnityContainer对象的RegisterType方法来注册对象与对象之间的关系
                //3.通过UnityContainer对象的Resolve方法来获取指定对象关联的对象
                UnityContainer container = new UnityContainer();//创建容器
                container.RegisterType<IWaterTool, PressWater>();//注册依赖对象 默认注册(无命名),如果后面还有默认注册会覆盖前面的
                container.RegisterType<IWaterTool, PressWater>("PressWater1");  //命名注册
                IWaterTool water = container.Resolve<IWaterTool>();
                IWaterTool water1 = container.Resolve<IWaterTool>("PressWater1");
                Console.WriteLine(water.returnWater());
                Console.WriteLine(water1.returnWater());   
                Console.Read();
            }
        }
    
        /// <summary>
        /// 压水井
        /// </summary>
        public class PressWater : IWaterTool
        {
            public string returnWater()
            {
                return "地下水好甜啊!!!";
            }
        }
    
        /// <summary>
        /// 获取水方式接口
        /// </summary>
        public interface IWaterTool
        {
            string returnWater();
        }
    }
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1.在配置文件<configSections> 配置节下注册名为unity的section
                //2.在<configuration> 配置节下添加Unity配置信息
                //3.在代码中读取配置信息,并将配置载入到UnityContainer中
                IUnityContainer container = new UnityContainer();
                container.LoadConfiguration("MyContainer");
                UnityConfigurationSection section
                  = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");//获取指定名称的配置节   
                section.Configure(container, "MyContainer");//获取特定配置节下已命名的配置节<container name='MyContainer'>下的配置信息
    
                IWaterTool water = container.Resolve<IWaterTool>("PressWater");
                Console.WriteLine(water.returnWater());
                Console.Read();
            }
        }
    
        /// <summary>
        /// 压水井
        /// </summary>
        public class PressWater : IWaterTool
        {
            public string returnWater()
            {
                return "地下水好甜啊!!!";
            }
        }
    
        /// <summary>
        /// 获取水方式接口
        /// </summary>
        public interface IWaterTool
        {
            string returnWater();
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
      </configSections>
    
      <unity>
        <!--定义类型别名-->
        <aliases>
          <add alias="IWaterTool" type="ConsoleApplication1.IWaterTool,ConsoleApplication1" />
          <add alias="PressWater" type="ConsoleApplication1.PressWater,ConsoleApplication1" />
        </aliases>
        <!--容器-->
        <container name="MyContainer">
          <!--映射关系-->
          <register type="IWaterTool"  mapTo="PressWater" name="PressWater"></register>
        </container>
      </unity>
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
      </startup>
    </configuration>


    属性注入(Property Injection):如果需要使用到被依赖对象的某个属性,在被依赖对象被创建之后,IoC容器会自动初始化该属性;

        class Program
        {
            static void Main(string[] args)
            {
                UnityContainer container = new UnityContainer();
                container.RegisterType<IPeople, VillagePeople>();
                container.RegisterType<IWaterTool, PressWater>();
                IPeople people = container.Resolve<IPeople>();
                Console.WriteLine(people._pw.returnWater());
                people.DrinkWater();
                Console.Read();
            }
        }
    
        /// <summary>
        /// 压水井
        /// </summary>
        public class PressWater : IWaterTool
        {
            public string returnWater()
            {
                return "地下水好甜啊!!!";
            }
        }
    
        /// <summary>
        /// 获取水方式接口
        /// </summary>
        public interface IWaterTool
        {
            string returnWater();
        }
    
        /// <summary>
        /// 人接口
        /// </summary>
        public interface IPeople
        {
            IWaterTool _pw { get; set; }
            void DrinkWater();
        }
    
        /// <summary>
        /// 村民
        /// </summary>
        public class VillagePeople : IPeople
        {
            [Dependency]
            public IWaterTool _pw { get; set; }
            public VillagePeople(IWaterTool pw)
            {
                _pw = pw;
            }
            public void DrinkWater()
            {
                Console.WriteLine(_pw.returnWater());
            }
        }

    方法注入(Method Injection):如果被依赖对象需要调用某个方法进行相应的初始化,在该对象创建之后,IoC容器会自动调用该方法。

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                UnityContainer container = new UnityContainer();
                container.RegisterType<IPeople, VillagePeople>();
                container.RegisterType<IWaterTool, PressWater>();
                IPeople people = container.Resolve<IPeople>();
                Console.WriteLine(people._pw.returnWater());
                people.DrinkWater();
                Console.Read();
            }
        }
    
        /// <summary>
        /// 压水井
        /// </summary>
        public class PressWater : IWaterTool
        {
            public string returnWater()
            {
                return "地下水好甜啊!!!";
            }
        }
    
        /// <summary>
        /// 获取水方式接口
        /// </summary>
        public interface IWaterTool
        {
            string returnWater();
        }
    
        /// <summary>
        /// 人接口
        /// </summary>
        public interface IPeople
        {
            IWaterTool _pw { get; set; }
            void DrinkWater();
            void ObjectInit();
        }
    
        /// <summary>
        /// 村民
        /// </summary>
        public class VillagePeople : IPeople
        {
            [Dependency]
            public IWaterTool _pw { get; set; }
            public VillagePeople(IWaterTool pw)
            {
                _pw = pw;
            }
            public void DrinkWater()
            {
                Console.WriteLine(_pw.returnWater());
            }
            [InjectionMethod]
            public void ObjectInit()
            {
                Console.WriteLine("方法注入");
            }
        }
    }
  • 相关阅读:
    Shell之sed用法 转滴
    再议mysql 主从配置
    CentOS 如何将.deb 文件 转换.rpm
    scp命令[转]
    安装samba服务器
    xdebug影响php运行速度
    PHP中VC6、VC9、TS、NTS版本的区别与用法详解
    将Centos的yum源更换为国内的阿里云源
    centos网卡错误Device eth0 does not seem to be present
    虚拟机VirtualBox中centos6.5网络设置
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/5403747.html
Copyright © 2011-2022 走看看