Fork me on GitHub
C# Unity使用

1.引用對象

2.在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  configSource="unity.config" />
    
   
</configuration>
复制代码

這裡unity的配置是在unity.config中進行配置的。

代碼如下:

View Code

3.關於配置的說明

  <!--alias 定義別名-->
  <alias alias="IClass" type="ConsoleApplication1.IClass, ConsoleApplication1" />

  <!--引入下面的命名空間,可以省的輸入過多的內容-->
  <namespace name="ConsoleApplication1" />

2個操作是為了使輸入簡單。

4.實例說明

4.1 定義UnityContainer,并初始化用配置文件中的數據

            var container = new UnityContainer();

            UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
         
            configuration.Configure(container);

 

4.2 註冊簡單接口類

 <register type="IClass" mapTo="MyClass" />

代碼如下:

    interface IClass
    {
        void ShowInfo();
    }
复制代码
class MyClass:IClass
    {
        [Dependency]//實例化對象
        public virtual IPubContext Context { set; get; }
        #region IClass 成員

        public void ShowInfo()
        {
            Console.WriteLine("MyClass12");
        }

        #endregion
    }
复制代码

代碼中調用:

 IClass classInfo = container.Resolve<IClass>();
classInfo.ShowInfo();

4.3 如果對象中有屬性,需要初始化,可以在屬性上加[Dependency]//實例化對象,那麼在取得對象的時候,這個屬性頁初始化了。

如上例中的IPubContext 屬性。

    class IPubContext
    {
        public string Name { set; get; }
    }

4.4 帶構造函數的實例配置

    <register type="InstallerBase" mapTo="BlogInstaller">
      <constructor>
        <param name="pubContext" type="IPubContext" />
        <param name="sum" type="System.Int32" value="3" />
      </constructor>
    </register>

定義類型如下:

View Code

代碼調用如下:

 InstallerBase classInstallerBase = container.Resolve<InstallerBase>();
 classInstallerBase.ShowInfo();

其中,構造函數是:

        public BlogInstaller(IPubContext pubContext,int sum)
        {
            this.pubContext = pubContext;
        }

與配置文件中的參數是一一對應的。並且配置文件中對與sum這個屬性,有默認數值是3.

4.5 泛型類型配置

<register type="ISomeInterface[]" mapTo="MyTypeImplementingSomeInterface[]"/>

代碼如下定義:

View Code

代碼調用如下:

ISomeInterface<int> some = container.Resolve<ISomeInterface<int>>();

4.6 配置方法的執行

   <register type="ObjectWithOverloads" name="callFirstOverload">
      <method name="CallMe">
        <param name="param" type="int" value="17"/>
      </method>
    </register>

定義類callFirstOverload,並且定義方法CallMe

View Code

代碼調用:

    //執行方法
            var result = container.Resolve<ObjectWithOverloads>("callFirstOverload");
            Console.WriteLine(result.FirstOverloadCalls);

4.7 數組配置

复制代码
   <!--數組-->
    <register type="ILogger" name="main" mapTo="MockLogger" />
    <register type="ILogger" name="another" mapTo="MockLogger" />
    <register type="ILogger" name="special" mapTo="SpecialLogger" />
    <register type="ArrayDependencyObject" name="specificElements">
      <property name="Loggers">
        <array>
          <dependency name="main" />
          <dependency name="another" />
        </array>
      </property>
    </register>
复制代码

定義類:

View Code

代碼調用:

   var result2 = container.Resolve<ArrayDependencyObject>("specificElements");

可以看到result2執行完成后,會得到 result2.Loggers數組,這個數組包括2個對象,一個是main

另一個是another,都是ILogger對象。

4.8 LifetimeManager的使用。是定義一個生命週期。這裡就不說了。在我的MVC中,有使用。

 

 

 
分类: C#
posted on 2012-05-10 20:31  HackerVirus  阅读(270)  评论(0编辑  收藏  举报