你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2015-02-27] 来源:Linux社区  作者:quwenzhe [字体:大 中 小]

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

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

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

我们在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功能很强大,我们这个架构只是用到了冰山一角,其他功能还需继续研究。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-02/114182.htm

linux
相关资讯       .NET开源框架  Castle框架 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款