码迷,mamicode.com
首页 > Web开发 > 详细

谈谈对.NET开源框架Castle的认识

时间:2015-01-29 00:09:21      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:castle

   最新项目优化,由架构师温工手把手辅导我搭架构,能每天接受一位久经沙场架构师的亲自指导,对我是莫大的荣幸。

   架构分为三个模块:AlarmEngineCoreLib模块为接口模块,AlarmEngineKernalLib模块为具体实现类模块,AlarmEngineWebApp为对外发布的WCF模块。核心模块AlarmEngineWebApp目录如下:

   技术分享

   我们在AlarmEngineWebApp模块通过Castle管理AlarmEngineCoreLib接口和AlarmEngineKernalLib实现类的关系。

   配置过程如下

   1.在配置文件配置接口和实现类的关系:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor"/>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <castle>
    <components>
      <component id="log_service"   service="AlarmEngineCoreLib.IService"  type="AlarmEngineKernalLib.LogService,AlarmEngineKernalLib"/>
      <component id="data_source_service"   service="AlarmEngineCoreLib.IService"  type="AlarmEngineKernalLib.DataSourceService,AlarmEngineKernalLib"/>
      <component id="write_to_db_service"   service="AlarmEngineCoreLib.IService"  type="AlarmEngineKernalLib.WriteToDBService,AlarmEngineKernalLib"/>
      <component id="rule_engine_service"   service="AlarmEngineCoreLib.IService"  type="AlarmEngineKernalLib.RuleEngine,AlarmEngineKernalLib"/>
      <component id="memory_pool_service"   service="AlarmEngineCoreLib.IService"  type="AlarmEngineKernalLib.MemeryPool,AlarmEngineKernalLib"/>
    </components>
  </castle>
</configuration>
    2.在代理类中声明目标接口,通过一个对外的方法CreateMemoryPool控制接口的实现过程。

using System;
using AlarmEngineCoreLib;

namespace AlarmEngineWebApp
{
    public class MemoryPoolProxyService : IMemoryPoolProxyService
    {
        private static IMemoryPool memoryPool = null;

        /// <summary>
        /// 提供对外创建具体类的方法
        /// </summary>
        public static void CreateMemoryPool(IServiceContainer _Container)
        {
            MemoryPoolProxyService.memoryPool = _Container.GetService("AlarmEngineKernalLib.MemeryPool") as IMemoryPool;
        }
    }
}
        3.在Global启动时,调用代理类的对外方法,完成接口的实现过程。

         /// <summary>
        /// 报警引擎服务容器
        /// </summary>
        public static Global _AlarmEngineApp = null;

        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            _AlarmEngineApp = new Global();
             
            MemoryPoolProxyService.CreateMemoryPool(_AlarmEngineApp);
        }
    这样,我们通过Castle提供的容器管理接口和实现类之间的关系,典型的依赖注入容器。

   Castle是针对.NET平台下的一个非常优秀的开源项目,从数据访问框架 ORM到依赖注入容器,再到WEB层的MVC框架、AOP,基本包括了整个开发过程中的所有东西,为我们快速的构建企业级的应用程序提供了很好的服务。

   Castle功能很强大,我们这个架构只是用到了冰山一角,其他功能还需继续研究。

谈谈对.NET开源框架Castle的认识

标签:castle

原文地址:http://blog.csdn.net/quwenzhe/article/details/43246583

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!