1. 程式人生 > >使用Spring.NET的IoC容器

使用Spring.NET的IoC容器

frame 管理 sdn detail lns string creat ack mod

使用Spring.NET的IoC容器

0. 輔助類庫

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpringDemo.Pub
{
    public static class Pub
    {


        public static string ServiceNameA = "SpringDemo.Lib.Oracle.OracleDatabase";
        public static string ServiceNameB = "SpringDemo.Lib.Sql.SqlDatabase";

        public static string FullNameA = "SpringDemo.Lib.Oracle.OracleDatabase, SpringDemo.Lib.Oracle";
        public static string FullNameB = "SpringDemo.Lib.Sql.SqlDatabase, SpringDemo.Lib.Sql";

        public static Type TypeA = Type.GetType(FullNameA);
        public static Type TypeB = Type.GetType(FullNameB);


    }
}


1. 使用代碼方式進行組件註冊【依賴服務類】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SpringDemo.Lib;



using Spring.Core;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory.Support;



namespace SpringDemo.Lib.Mgr
{
    /// <summary>
    /// 管理類
    /// </summary>
    public partial class Mgr
    {
        private static IApplicationContext container = null;

        /// <summary>
        /// 自定義容器和組件註冊
        /// </summary>
        /// <returns></returns>
        public static IApplicationContext GetContainer()
        {

            if (container == null)
            {


                IObjectDefinitionFactory factory = new DefaultObjectDefinitionFactory();
                AbstractObjectDefinition defiA = factory.CreateObjectDefinition(SpringDemo.Pub.Pub.FullNameA, null, AppDomain.CurrentDomain);
                AbstractObjectDefinition defiB = factory.CreateObjectDefinition(SpringDemo.Pub.Pub.FullNameB, null, AppDomain.CurrentDomain);


                //建立容器
                GenericApplicationContext tmpContainer = new GenericApplicationContext();
                tmpContainer.RegisterObjectDefinition(SpringDemo.Pub.Pub.ServiceNameA, defiA);
                tmpContainer.RegisterObjectDefinition(SpringDemo.Pub.Pub.ServiceNameB, defiB);
    

                container = tmpContainer;

            }
            return container;
        }
    }
}


2. 使用代碼方式進行組件註冊【不需要依賴】【類似反射的全字符串形式】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Spring.Core;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory.Support;

using SpringDemo.Lib;
namespace SpringDemo.Lib.Container
{
    /// <summary>
    /// 管理類
    /// </summary>
    public partial class Container
    {
        private static IApplicationContext container = null;

        /// <summary>
        /// 自定義容器和組件註冊
        /// </summary>
        /// <returns></returns>
        public static IApplicationContext GetContainer()
        {

            if (container == null)
            {
                IObjectDefinitionFactory factory = new DefaultObjectDefinitionFactory();
                AbstractObjectDefinition defiA = factory.CreateObjectDefinition(SpringDemo.Pub.Pub.FullNameA, null, AppDomain.CurrentDomain);
                AbstractObjectDefinition defiB = factory.CreateObjectDefinition(SpringDemo.Pub.Pub.FullNameB, null, AppDomain.CurrentDomain);


                //建立容器
                GenericApplicationContext tmpContainer = new GenericApplicationContext();
                tmpContainer.RegisterObjectDefinition(SpringDemo.Pub.Pub.ServiceNameA, defiA);
                tmpContainer.RegisterObjectDefinition(SpringDemo.Pub.Pub.ServiceNameB, defiB);


                container = tmpContainer;

            }
            return container;
        }
    }
}


3. 使用配置文件進行組件註冊【不需要依賴】

3.1. 定義配置文件

<?xml version="1.0"?>
<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="assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"/>-->
      <resource uri="config://spring/objects" />
    </context>
    <objects xmlns="http://www.springframework.net">
      <object id="SpringDemo.Lib.Oracle.OracleDatabase" type="SpringDemo.Lib.Oracle.OracleDatabase, SpringDemo.Lib.Oracle" singleton="false" />
      <object id="SpringDemo.Lib.Sql.SqlDatabase" type="SpringDemo.Lib.Sql.SqlDatabase, SpringDemo.Lib.Sql" singleton="false" />
    </objects>
  </spring>
  
  <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>


3.2. 讀取config配置文件進行組件註冊

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SpringDemo.Lib;


using Spring.Core;
using Spring.Context;
using Spring.Context.Support;


namespace SpringDemo.Run
{
    public partial class Helper
    {

        /// <summary>
        /// 根據配置文件裏的服務名生成對象
        /// </summary>
        public static void GetFrom_Config(string vServiceName)
        {

            IApplicationContext container = ContextRegistry.GetContext();


            
            if (container != null)
            {
                IDatabase db = null;
                //db = container.GetObject(vServiceName) as IDatabase;
                db = container.GetObject<IDatabase>(vServiceName); ;
                if (db != null)
                {
                    db.Select("..........");
                }

            }

        }



    }
}


使用Spring.NET的IoC容器