.net內建的IOC容器MEF使用

來源:互聯網
上載者:User

標籤:程式   物件導向編程   檔案   public   ram   code   程式集   指定   原則   

 

IOC能做什麼

IoC 不是一種技術,只是一種思想,一個重要的物件導向編程的法則,它能指導我們如何設計出松耦合、更優良的程式。

控制反轉:

將控制權移交給第三方容器  new 操作

依賴注入:

在程式的運行過程中 動態給某個對象 注入他所需要的 執行個體

 

例子:類比一個使用者購買了一個音樂播放器

使用者類

   public class User   {      //初始化一個音樂播放器       public MusicPlayer Playe = new MusicPlayer();       public void Play()       {           Playe.Play();       }    }

 /// <summary>   /// 音樂播放器   /// </summary>    public class MusicPlayer    {       public void Play()       {           Console.WriteLine("音樂播放器");       }    }

 

 

 

控制台

  class Program    {             static void Main(string[] args)        {                        var u =new User();            u.Play();            Console.ReadKey();        }    }

輸出

 

 

需求改變

  使用者需要將音樂播放器改為視頻播放器

 1.建立一個視頻播放器供應商程式集 以及視頻播放器類

 

    /// <summary>    /// 視頻播放器    /// </summary>    public class VideoPlayer    {        public void Play()        {            Console.WriteLine("視頻播放器");        }    }

 

 

 

2.刪除上層的音樂播放器的引用 並引用視頻播放器。將使用者的new 音樂播放器 改為new視頻播放器

   public class User   {       public VideoPlayer Playe = new VideoPlayer();       public void Play()       {           Playe.Play();       }    }

執行輸出

問題

  下層改變 影響上層

修改

  遵循設計模式 依賴導致原則

  即上層不直接依賴下層。他們都只依賴下層的抽象(抽象類別 或介面)

  這個時候我們為播放器抽象一個契約

 /// <summary>    /// 播放器契約類    /// </summary>    public interface IPlayer    {        void Play();    }

 並在音樂播放器 和視頻播放器 實現播放契約

 /// <summary>    /// 視頻播放器    /// </summary>    public class VideoPlayer : IPlayer    {        public void Play()        {            Console.WriteLine("視頻播放器");        }    }
/// <summary>   /// 音樂播放器   /// </summary>    public class MusicPlayer:IPlayer    {       public void Play()       {           Console.WriteLine("音樂播放器");       }    }

 

使用MEF IOC 實現上層與下層的完全解耦

  MEF的特點

   1. .net4.0新增 在System.ComponentModel.Composition 程式集 不需要引用第三方類庫

   2. 0配置:MEF是不需要使用設定檔或代碼對介面與實現進行一一配對的,只需要簡單的使用幾個Attribute特性,就能自動完成源與目標的配對工

   3. 自動化:系統初始化時自動遍曆程式目錄或指定檔案夾下的dll,根據程式集中介面與類的特定Attribute特性進行自動配對。

開始使用  

 1、ui層引用契約介面層 而不引用音樂播放器供應商 和 視頻播放器供應商

 2.引用MEF所需程式集System.ComponentModel.Composition

將託管於IOC容器的類打上Export 並標記他對應的介面

 /// <summary>    /// 視頻播放器    /// </summary>     [Export(typeof(IPlayer))]    public class VideoPlayer : IPlayer    {        public void Play()        {            Console.WriteLine("視頻播放器");        }    }
 /// <summary>   /// 音樂播放器   /// </summary>     [Export(typeof(IPlayer))]    public class MusicPlayer:IPlayer    {       public void Play()       {           Console.WriteLine("音樂播放器");       }    }

控制台使用

  class Program    {        public static CompositionContainer _container;        static void Main(string[] args)        {            AggregateCatalog catalog = new AggregateCatalog();            //將當前程式集 打了特性Export的類 匯入容器            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));            //將運行目錄下的MusicPlayerManufacturer 打了特性Export的類匯入容器            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.Load("MusicPlayerManufacturer")));            _container = new CompositionContainer(catalog);            //在容器中獲得User類的對象            var u = _container.GetExportedValue<User>();            u.Play();            Console.ReadKey();        }    }

當我們需要使用視頻播放器 只需要將視頻播放器的介面複製到bin目錄   MEF注入 改為 視頻播放器介面就行了

 

還沒寫完。。。有空再寫

.net內建的IOC容器MEF使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.