Castle.Windsor依赖注入的高级应用_Castle.Windsor.3.1.0

前端之家收集整理的这篇文章主要介绍了Castle.Windsor依赖注入的高级应用_Castle.Windsor.3.1.0前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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",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;
        }
    }
}


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

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,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. 使用配置文件进行组件注册【不需要依赖】

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";//服务名
            vServiceName = "CastleDemo.Lib.sql.sqlDatabase";

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

            }

        }



    }
}


4. Demo下载

点此下载

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


5.1. Singleton : 容器中只有一个实例将被创建

5.2. Transient : 每次请求创建一个新实例

5.3. PerThread: 每线程中只存在一个实例

5.4. PerWebRequest : 每次web请求创建一个新实例

5.5. Pooled :使用"池化"方式管理组件,可使用PooledWithSize方法设置池的相关属性

原文链接:https://www.f2er.com/javaschema/286510.html

猜你在找的设计模式相关文章