Spring.NET學習筆記3——實現一個簡易的IoC架構(練習篇)

來源:互聯網
上載者:User

講了這麼多理論,我們來手動實現一個簡易的IoC{
tagshow(event)
}">架構的,這樣可以加深IoC的理論知識。

  一、思路

在我們使用Spring.NET架構的時候,首先需要執行個體化Spring.NET容器, 然後調用IoC容器IObjectFactory{
tagshow(event)
}">介面中GetObject方法擷取容器中的對象。通過這一點就可以告訴我們製作IoC容器需要寫一個擷取 XML檔案內容的方法和申明一個Dictionary<string, object>來存放IoC容器中的對象,還需要寫一個能從Dictionary<string, object>中擷取對象的方法。

  二、分析

要擷取XML檔案的內容,在3.5的文法中,我自然而然想到了Linq To XML。需要執行個體XML中{
tagshow(event)
}">配置的對象,我們想到了使用反射技術來擷取對象的執行個體。
  

  三、代碼實現

1.xml工廠

MyXmlFactory

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.Reflection;
  7. namespace MyselfIoC
  8. {
  9.     public class MyXmlFactory
  10.     {
  11.         private IDictionary<string, object> objectDefine = new Dictionary<string, object>();
  12.         public MyXmlFactory(string fileName)
  13.         {
  14.             InstanceObjects(fileName);  // 執行個體IoC容器
  15.         }
  16.         /// <summary>
  17.         /// 執行個體IoC容器
  18.         /// </summary>
  19.         /// <param name="fileName"></param>
  20.         private void InstanceObjects(string fileName)
  21.         {
  22.             XElement root = XElement.Load(fileName);
  23.             var objects = from obj in root.Elements("object") select obj;
  24.             objectDefine = objects.ToDictionary(
  25.                     k => k.Attribute("id").Value,
  26.                     v =>
  27.                     {
  28.                         string typeName = v.Attribute("type").Value; 
  29.                         Type type = Type.GetType(typeName); 
  30.                         return Activator.CreateInstance(type);
  31.                     }
  32.                 );
  33.         }
  34.         /// <summary>
  35.         /// 擷取對象
  36.         /// </summary>
  37.         /// <param name="id"></param>
  38.         /// <returns></returns>
  39.         public object GetObject(string id)
  40.         {
  41.             object result = null;
  42.             if (objectDefine.ContainsKey(id))
  43.             {
  44.                 result = objectDefine[id];
  45.             }
  46.             return result;
  47.         }
  48.     }
  49. }

複製代碼

2.調用

Program

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MyselfIoC
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             AppRegistry();
  12.             Console.ReadLine();
  13.         }
  14.         static void AppRegistry()
  15.         {
  16.             MyXmlFactory ctx = new MyXmlFactory(@"D:\Objects.xml");
  17.             Console.WriteLine(ctx.GetObject("PersonDao").ToString());
  18.         }
  19.     }
  20. }

複製代碼

好了,一個簡易的IoC架構就基本實現了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.