Unity Study 1

本文深入探讨了使用Unity容器进行依赖注入的技术,具体展示了如何注入数组类型和自定义类,并通过配置文件实现。通过示例代码演示了在容器中调用和使用这些注入对象的方法。

上次写了一个Unity的helloworld,就是将一个字符串注入到实例中。继续学习,这次注入数组类型和自定义的类。看代码……

    public class TestClass
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
    public class ArrayTest
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string[] values;

        public string[] Values
        {
            get { return values; }
            set { values = value; }
        }

        private TestClass testClassSingle;

        public TestClass TestClassSingle
        {
            get { return testClassSingle; }
            set { testClassSingle = value; }
        }
    }

配置文件:

<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 
Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity>
    <typeAliases>
      <typeAlias alias="string" type="System.String,mscorlib"/>
      <typeAlias alias="stringArray" type="System.String[],mscorlib"/>
      <typeAlias alias="arrayTest" type="UnityGameIOC.ArrayTest,UnityGameIOC"/>
      <typeAlias alias="testClassSingle" type="UnityGameIOC.TestClass,UnityGameIOC"/>
    </typeAliases>
    <containers>
      <container>
        <types>

          <type type="testClassSingle" name="testClassSingle">
            <typeConfig>
              <property name="Name" propertyType="string">
                <value type="string" value="单个类测试"/>
              </property>
            </typeConfig>
          </type>


          <type type="arrayTest">
            <typeConfig>
              <!--注入一个字符串-->
              <property name="Name" propertyType="string">
                <value type="string" value="dddddd" />
              </property>
              <!--注入数组类型-->
              <property name="Values" propertyType="stringArray">
                <array>
                  <value type="string" value="1"/>
                  <value type="string" value="2"/>
                  <value type="string" value="3"/>
                  <value type="string" value="4"/>
                </array>
              </property>
              <!--注入单个自定义的类-->
              <property name="TestClassSingle" propertyType="testClassSingle">
                <dependency name="testClassSingle" type="testClassSingle"/>
              </property>
            </typeConfig>
          </type>
        </types>
      </container>
    </containers>
  </unity>
</configuration>

 

在容器中调用:

 

  class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(container);
            ArrayTest test = container.Resolve(typeof(ArrayTest)) as ArrayTest;
            foreach (string i in test.Values)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine(test.Name);
            Console.WriteLine(test.TestClassSingle.Name);
        }
    }
运行截图:

image

总而言之,Unity和Castle IOC用着还挺像的。关于这些奇怪的标签没有弄太清楚,只搞清楚了一部分,等全弄清了再做笔记吧。

转载于:https://www.cnblogs.com/fei-time/archive/2010/02/08/1665664.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值