天天看點

Castle學習系列(一)---ActiveRecord基礎環境配置

       接下來準備學習下Castle架構,官網是http://www.castleproject.org/,本系列文章是建立在官網幫助文檔的基礎之上的。

       Castle最早在2003年誕生于Apache Avalon項目,目的是為了建立一個IOC(控制反轉)架構。發展到現在已經有4個元件了,分别是ActiveRecord(ORM元件)、Windsor(IOC元件)、DynamicProxy(動态代理元件)、MonoRail(Web MVC元件)。

       目前我隻做内部C/S系統,對于MonoRail元件還未找到使用場景,是以暫時不打算學習MonoRail元件。

       首先開始學習ActiveRecord 元件,ActiveRecord是建立在NHibernate基礎之上的,封裝了NHibernate的ORM特性,而且可以通過代碼來配置資料庫與實體之間的映射關系。目前ActiveRecord 是屬于Castle架構的已完成的元件,也就是說以後不會再有新的特性。

       ActiveRecord必須用到的DLL如下所示:

Castle學習系列(一)---ActiveRecord基礎環境配置

      其中NHibernate版本号是3.1.0.4000,無法更新到最新版本3.3.3.3001,因為修改ActiveRecord項目引用編譯不通過。

      其中Castle.Core版本号是2.5.1.0,可以更新到最新版本3.2.0.2259,隻需修改ActiveRecord項目引用,重新編譯取得Castle.ActiveRecord.dll即可(項目可從GIT下載下傳https://github.com/castleproject)

      對于ActiveRecord的學習,先從基本環境配置入手。建立WinForm項目,添加必須的dll引用。

      對于環境的配置可以通過XML,也可以通過代碼,考慮到我們機關的軟體部署模式,我是通過代碼來配置的。但首先來看看XML配置(寫入App.config):

<configuration>
    <activerecord 
        isWeb="true|false" 
        isDebug="true|false" 
        pluralizeTableNames="true|false"
        threadinfotype="custom thread info implementation"
        sessionfactoryholdertype="custom session holder implementation"
        namingstrategytype="custom namingstrategy implementation">
        <config
            database="MsSqlServer2000|MsSqlServer2005|MsSqlServer2008|SQLite|MySql|MySql5|Firebird|PostgreSQL|PostgreSQL81|PostgreSQL82|MsSqlCe|Oracle8i|Oracle9i|Oracle10g"
            connectionStringName="name of connection string in config">
            <!-- Any legal NHibernate parameter you want to specify or override its default value -->
        </config>
        <config type="Full Type name to Abstract Class that defines boundaries for different database">
            <add key="connection.driver_class"           value="NHibernate Driver" />
            <add key="dialect"                           value="NHibernate Dialect" />
            <add key="connection.provider"               value="NHibernate Connection Provider" />
            <add key="proxyfactory.factory_class"        value="NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle" />
      <!-- Use only one of the two attributes below -->
            <add key="connection.connection_string"      value="connection string" />
            <add key="connection.connection_string_name" value="name of connection string in config" />
        </config>
    </activerecord>
</configuration>
           
(摘自官方幫助文檔)
屬性 是否必須 說明
isWeb 是否ASP.NET項目,Web項目必須設定為True
isDebug 是否調試模式,若是會在debug個項目寫入相關調試資訊
pluralizeTableNames 是否表名和實體名一緻,預設false
threadinfotype IThreadScopeInfo自定義實作
sessionfactoryholdertype ISessionFactoryHolder自定義實作
namingstrategytype INamingStrategy自定義實作
type (on config node) 若存在多個資料庫,則需要配置來限定特定的實體通路特定的資料庫
database (on config node) 資料庫類别
connectionStringName (on config node) 資料庫連接配接字元串
一個實際的配置執行個體如下:
<configuration>
  <configSections>
      <section name="activerecord"
               type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
  </configSections>

  <connectionStrings>
      <add name="main" connectionString="Data Source=.;Initial Catalog=test;Integrated Security=SSPI"/>
  </connectionStrings>
  <activerecord>
      <config>
        <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
        <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
        <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
        <add key="connection.connection_string_name" value="main" />
      </config>
  </activerecord>
</configuration>
           

不同資料庫的不同驅動的配置請檢視http://docs.castleproject.org/Active%20Record.Xml%20Configuration%20Reference.ashx文章後面的介紹。

然後再看看代碼配置:代碼配置可以基于XML配置來寫,隻是将配置的Key/Value屬性用代碼的Dictionary來表示,代碼配置如下:

IDictionary<string, string> properties = new Dictionary<string, string>();

properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("dialect", "NHibernate.Dialect.MsSql2008Dialect");
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("connection.connection_string", "Data Source=WIN-QF5DBA70IVE\\SQL2008;Initial Catalog=CastleDemo;Integrated Security=SSPI");
properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle");

InPlaceConfigurationSource source = new InPlaceConfigurationSource();
source.Add(typeof(ActiveRecordBase), properties);

Assembly asm = Assembly.Load("CastleActiveRecord");
ActiveRecordStarter.Initialize(asm, source);
           

其中:

Assembly asm = Assembly.Load("CastleActiveRecord");

表示加載實體所在的程式集CastleActiveRecord,初始化之後調用ActiveRecordStarter.CreateSchema();可根據模型代碼生成資料庫表。

繼續閱讀