首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Castle Windsor 容器配置基礎(一):組件與設施註冊、多種配置來源、Include 模組化與條件編譯

     編輯:關於.NET

配置核心元素

  • Components:所有元件註冊,每個 <component> 需 id,參數置於 <parameters>。
  • Facilities:擴充設施 (如 NHibernate、Transaction),需 id 與 type。

三種配置來源

  1. 獨立 XML:new WindsorContainer(new XmlInterpreter("config.xml"))
  2. App/Web.config:需註冊 castle section,new WindsorContainer(new XmlInterpreter()) 自動讀取。
  3. 自定義 IConfigurationStore:實作介面完全掌控配置來源。

Include:模組化配置

將 properties.config、facilities.config、services.config 分檔,主配置引用:

<castle>
  <include uri="properties.config" />
  <include uri="facilities.config" />
  <include uri="services.config" />
</castle>

Properties:變數複用

<properties>
  <connectionString>server=local;uid=sa;pwd=sa;database=test</connectionString>
</properties>
<components>
  <component id="connectionfactory" ...>
    <properties>
      <connString>#{connectionString}</connString>
    </properties>
  </component>
</components>

條件編譯指令

支援 <define>、<undef>、<if>、<choose>/<when>/<otherwise>:

<define flag="DEBUG" />
<components>
  <component id="BasicUsageDemo">
    <if defined="DEBUG">
      <interceptors><interceptor>${logging.interceptor}</interceptor></interceptors>
    </if>
  </component>
  <undef flag="DEBUG" />
  <choose>
    <when defined="DEBUG"><component id="BasicUsageDebug" /></when>
    <when defined="Complex"><component id="ComplexDemo" /></when>
    <when defined="Prod"><component id="ProdDemo" /></when>
    <otherwise><component id="Default" /></otherwise>
  </choose>
</components>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved