反射 + 配置文件 實現IOC容器

IOC實現:


IOC容器咱們只停留在知道上是不行的,咱們要動手作印象對更深入,那麼我給你們看一個代碼。看看代碼中IOC容器的實現。spring


代碼實現:


建立一個類庫:設計模式


解決方式的類庫創建:post




建立一個實體類:User:學習


<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Text;

namespace Spring.Demo.Model
{
    /// <summary>
    /// 用戶類
    /// </summary>
    public class Users
    {
        /// <summary>
        /// 編號
        /// </summary>
        private int _oid;
        public int Oid
        {
            get { return _oid; }
            set { _oid = value; }
        }

        /// <summary>
        /// 姓名
        /// </summary>
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// 性別
        /// </summary>
        private string _sex;
        public string Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }

        /// <summary>
        /// 年齡
        /// </summary>
        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
    }
}</span>

建立IUsers的接口:


<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Text;
namespace Spring.Demo.Service
{
    public interface IUsers
    {
        /// <summary>
        /// 返回用戶的具體信息的方法
        /// </summary>
        /// <returns></returns>
        string GetUserInfo();
    }
}
</span>

建立一個實現IUsers接口的實現類:


<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Text;
using Spring.Demo.Service;
using Spring.Demo.Model;

namespace Spring.Demo.Compontext
{
    public class UsersCompontents : IUsers
    {
        public UsersCompontents()
        { }

        #region 獲取用戶信息
        public string GetUserInfo()
        {
            Users user = new Users();
            user.Oid = 1;
            user.Name = "Beniao";
            user.Sex = "Boy";
            user.Age = 25;

            return string.Format("編號:{0}--->姓名:{1}--->性別:{2}--->年齡:{3}",
                user.Oid,
                user.Name,
                user.Sex,
                user.Age);
        }
        #endregion
    }
}</span>

建立測試類:


<span style="font-size:18px;">using ITOO.Library.Core.AOP;
using Spring.Context;
using Spring.Demo.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Sping.Demo.SimpleTest
{
    class Program
    {
        static void Main(string[] args)
        {

            IUsers studentChangeBll = SpringHelper.GetObject<IUsers>("Users");

            Console.WriteLine(studentChangeBll.GetUserInfo());
            Console.Read();
        }
    }
}</span>

在控制檯程序中建立一個配置文件:


<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8" ?

> <configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections> <spring> <context> <resource uri="config://spring/objects"/> </context> <objects xmlns="http://www.springframework.net"> <!--這的配置依據實際的程序來的。UsersCompontents是程序集Spring.Demo.Compontext下的一個類--> <object name="Users" type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontext" singleton="false" > </object> </objects> </spring> </configuration></span>spa


執行後,發現SpringHelper卻小引用。

咱們通常寫代碼中咱們是這樣寫的: .net


<span style="font-size:18px;">//從config文件中取得程序集信息
IApplicationContext context = ConfigurationManager.GetSection("spring/context")
                               as IApplicationContext;
//調用方法
//Users爲config文件中的配置節
//<object name="Users"       
//        type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontent">
//</object>
IUsers user = context.GetObject("Users") as IUsers;</span>

這樣咱們就可以從配置文件裏將對象取出來,但是咱們都不想在代碼中有多餘的代碼。不能每一次new對象的時候,咱們都要寫一遍這句話:IApplicationContext context = ConfigurationManager.GetSection("spring/context") as IApplicationContext;這樣就添加了咱們維護代碼的成本,所以。咱們將這句話封裝起來,封裝的代碼是這種:


建立一個類:SpringHelper:設計


<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Spring.Context.Support;

namespace ITOO.Library.Core.AOP
{
    public class SpringHelper
    {
        /// <summary>
        /// Spring容器上下文
        /// </summary>
        private static IApplicationContext SpringContext
        {
            get
            {
                return ContextRegistry.GetContext();
            }
        }


        /// <summary>
        /// 獲取配置文件 配置的 對象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objName"></param>
        /// <returns></returns>
        public static T GetObject<T>(string objName) where T : class
        {
            return (T)SpringContext.GetObject(objName);
        }
    }
}


</span>

以上的代碼咱們就可以將每次讀取配置文件裏的那句話去掉了,咱們直接就可以寫這樣一句話就可以了:IUsers studentChangeBll = SpringHelper.GetObject<IUsers>("Users");


這裏體現了封裝的重要性。先前我在作AOP的時候。個人師傅看到了相似這種代碼的時候,他就跟我討論過這個問題,我當時懵懵懂懂,沒有進行下一步的行動,現在想一想,問題出現在我根本沒有動手去作,或者知識沒有深刻到那個層次,認識這個知識的方面沒有那麼深。code

所有問題,都要動手去作才行。
orm


總結:


咱們從上面的實踐到分析以後,咱們發現事實上咱們看似是新的東西,事實上咱們已經學習過了,就像IOC容器同樣,咱們學習過了反射和配置文件。咱們發現事實上IOC容器不就是反射和配置文件來實現的嗎,反射和配置文件是咱們在大話設計模式中就已經學習到了的東西,這都不是新的東西。一個看似複雜的東西,都是有簡單的東西來組裝成的。咱們知道這個。就不會對新的東西有畏懼感了。xml

相關文章
相關標籤/搜索