依赖注入 Castle.Windsor高级应用

原文:https://blog.csdn.net/liuweitoo/article/details/8281213


_Castle.Windsor.3.1.0

一. 使用代码方式进行组件注册【依赖服务类】

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

using CastleDemo.Lib;


using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.MicroKernel.Registration;


namespace CastleDemo.Lib.Mgr
{
    /// <summary>
    /// 管理类
    /// </summary>
    public partial class Mgr
    {
        private static IWindsorContainer container = null;

        /// <summary>
        /// 自定义容器和组件注册
        /// </summary>
        /// <returns></returns>
        public static IWindsorContainer GetContainer()
        {

            if (container == null)
            {
                Type objTypeA = Type.GetType("CastleDemo.Lib.Oracle.OracleDatabase, CastleDemo.Lib.Oracle");
                Type objTypeB = Type.GetType("CastleDemo.Lib.Sql.SqlDatabase, CastleDemo.Lib.Sql");


                //建立容器
                IWindsorContainer tmpContainer = new WindsorContainer();


                //加入组件:旧版
                //tmpContainer.AddComponent("CastleDemo.Lib.Oracle.OracleDatabase", typeof(IDatabase), objTypeA);
                //tmpContainer.AddComponent("CastleDemo.Lib.Sql.SqlDatabase", typeof(IDatabase), objTypeB);

                //加入组件:新版
                tmpContainer.Register(Component.For(typeof(IDatabase)).ImplementedBy(objTypeA).Named("CastleDemo.Lib.Oracle.OracleDatabase"));
                tmpContainer.Register(Component.For(typeof(IDatabase)).ImplementedBy(objTypeB).Named("CastleDemo.Lib.Sql.SqlDatabase"));
           

                container = tmpContainer;

            }
            return container;
        }
    }
}

二. 使用代码方式进行组件注册【不需要依赖】【类似反射的全字符串形式】

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

using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.MicroKernel.Registration;

namespace CastleDemo.Lib.Container
{
    /// <summary>
    /// 管理类
    /// </summary>
    public partial class Container
    {
        private static IWindsorContainer container = null;

        /// <summary>
        /// 自定义容器和组件注册
        /// </summary>
        /// <returns></returns>
        public static IWindsorContainer GetContainer()
        {

            if (container == null)
            {
                Type objType = Type.GetType("CastleDemo.Lib.IDatabase, CastleDemo.Lib");

                Type objTypeA = Type.GetType("CastleDemo.Lib.Oracle.OracleDatabase, CastleDemo.Lib.Oracle");
                Type objTypeB = Type.GetType("CastleDemo.Lib.Sql.SqlDatabase, CastleDemo.Lib.Sql");


                //建立容器
                IWindsorContainer tmpContainer = new WindsorContainer();


                //加入组件:旧版
                //tmpContainer.AddComponent("CastleDemo.Lib.Oracle.OracleDatabase", objType, objTypeA);
                //tmpContainer.AddComponent("CastleDemo.Lib.Sql.SqlDatabase", objType, objTypeB);

                //加入组件:新版
                tmpContainer.Register(Component.For(objType).ImplementedBy(objTypeA).Named("CastleDemo.Lib.Oracle.OracleDatabase"));
                tmpContainer.Register(Component.For(objType).ImplementedBy(objTypeB).Named("CastleDemo.Lib.Sql.SqlDatabase"));
                   
                container = tmpContainer;

            }
            return container;
        }
    }
}

三. 使用配置文件进行组件注册【不需要依赖】

3.1. 定义配置文件

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  </configSections>
  <castle>
    <components>
      <component name="CastleDemo.Lib.Oracle.OracleDatabase" type="CastleDemo.Lib.Oracle.OracleDatabase, CastleDemo.Lib.Oracle" service="CastleDemo.Lib.IDatabase, CastleDemo.Lib"/>
      <component name="CastleDemo.Lib.Sql.SqlDatabase" type="CastleDemo.Lib.Sql.SqlDatabase, CastleDemo.Lib.Sql" service="CastleDemo.Lib.IDatabase, CastleDemo.Lib"/>
    </components>
  </castle>
<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 CastleDemo.Lib;


using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.MicroKernel.Registration;

namespace CastleDemo.Run
{
    public partial class Helper
    {

        /// <summary>
        /// 根据配置文件里的服务名生成对象
        /// </summary>
        public static void GetFrom_Config()
        {

            IWindsorContainer container = new WindsorContainer(new XmlInterpreter());

            string vServiceName = "CastleDemo.Lib.Oracle.OracleDatabase";//服务名
            if (container != null)
            {
                IDatabase db = container.Resolve<IDatabase>(vServiceName);
                if (db != null)
                {
                    db.Select("..........");
                }
            }
        }
    }
}

四. Demo下载

点此下载

五. Castle容器的组件生存周期,主要有如下几种

  1. Singleton : 容器中只有一个实例将被创建
  2. Transient : (短暂的)每次请求创建一个新实例
  3. PerThread: 每线程中只存在一个实例
  4. PerWebRequest : 每次web请求创建一个新实例
  5. Pooled :使用"池化"方式管理组件,可使用PooledWithSize方法设置池的相关属性

posted on 2020-10-21 10:14  springsnow  阅读(257)  评论(0编辑  收藏  举报

导航