Castle学习系列(一)---ActiveRecord基础环境配置

来源:互联网 发布:loco加速器软件下载 编辑:程序博客网 时间:2024/06/11 18:29

       接下来准备学习下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如下所示:


       


      其中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项目必须设置为TrueisDebug是否调试模式,若是会在debug个项目写入相关调试信息pluralizeTableNames是否表名和实体名一致,默认falsethreadinfotypeIThreadScopeInfo自定义实现sessionfactoryholdertypeISessionFactoryHolder自定义实现namingstrategytypeINamingStrategy自定义实现
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();可根据模型代码生成数据库表。




0 0
原创粉丝点击