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