C#対軽量レベル(IoC Container)依存注入Unityの使用

10822 ワード

C#対軽量レベル(IoC Container)依存注入Unityの使用


概要
Unityは軽量レベルの拡張可能な依存注入コンテナであり,構造関数,属性,メソッド呼び出し注入をサポートする.Unityは、コンポーネントベースのソフトウェアエンジニアリングに従事する開発者が直面している問題を処理することができます.成功したアプリケーションを構築する鍵は、非常に緩やかな結合設計を実現することです.ばらばらに結合されたアプリケーションは、より柔軟で、メンテナンスが容易です.このようなプログラムも開発中にテストしやすい.データベース接続、ネットワーク接続、ERP接続、豊富なユーザーインタフェースコンポーネントなど、オブジェクトをシミュレートすることができます.たとえば、顧客情報を処理するオブジェクトは、他のオブジェクトがアクセスするデータストレージに依存し、情報を検証し、ユーザーが更新を許可されているかどうかを確認します.注入テクノロジーに依存すると、特に依存が抽象的である可能性がある場合、顧客クラスがこれらのすべてのオブジェクトを正しくインスタンス化し、埋め込むことができます.
 
Unityプロファイル
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="UnityTest.ISqlHelper,UnityTest" mapTo="UnityTest.MysqlHelper,UnityTest">
        <lifetime type="singleton"/>
      register>
    container>
  unity>
configuration>

注意しなければならないのはtypeとmapToの値で、カンマで2つの部分を隔てています.1つはクラスのすべてで、ネーミングスペースを含めて、2つはネーミングスペースです.
では、他の方法もありますが、まずネーミングスペースを設定しておけば、そのままクラス名を書けばいいので、これは言いません.
ここでは簡単な構成で、詳細な構成は自分で検索します.
 
ダウンロードと参照
公式にダウンロード:http://unity.codeplex.com/
プロジェクトでdllを参照
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Configuration.dll
 
プログラム
データベースの操作クラスを交換すると仮定すると、まず操作クラスのインタフェースを確立し、派生したクラスが残っていることを具体的に実現します.
操作クラスインタフェース
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnityTest
{
    public interface ISqlHelper
    {
         string SqlConnection();
    }

    public interface IOtherHelper
    {
        string GetSqlConnection();
    }
}

 
派生クラス一:Ms SQL Server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnityTest
{
    public class MssqlHelper : ISqlHelper
    {
        public string SqlConnection()
        {
            return "this mssql.";
        }
    }
}

派生クラス2:MySQL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnityTest
{
    public class MysqlHelper : ISqlHelper
    {
        public string SqlConnection()
        {
            return "this mysql.";
        }
    }
}

その他のクラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnityTest
{
    public class MyOtherHelper : IOtherHelper
    {
        ISqlHelper sql;
        public MyOtherHelper(ISqlHelper sql)
        {
            this.sql = sql;
        }
        public string GetSqlConnection()
        {
            return this.sql.SqlConnection();
        }


    }
}

 
しゅプログラムよびだし
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;


namespace UnityTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer mycontainer = new UnityContainer();


            //             
            // MysqlHelper d = new MysqlHelper();
            //mycontainer.RegisterInstance(d);

            //         
            //mycontainer.RegisterType();

            //      
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Configure(mycontainer);
            //mycontainer.LoadConfiguration(); 

            //    
            ISqlHelper mysql = mycontainer.Resolve();
            Console.WriteLine(mysql.SqlConnection());

            //      
            mycontainer.RegisterType();
            IOtherHelper other = mycontainer.Resolve();
            Console.WriteLine(other.GetSqlConnection());

            Console.ReadKey();

        }
    }
}

 
ここまでで、終わりです.
自分で一度運転して、きっともっと深く理解できると信じています.
 
 
分類:
  .NET