談談對.NET開源架構Castle的認識

來源:互聯網
上載者:User

標籤: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的認識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.