在学习castle过程中发现安装了1.0 Release Candidate 3版本的castle,系统自带了一个ActiveRecord-Samples例子,决定好好研究下:
从 App.cs 开始
App.cs 代码
namespace BlogSample
{
using System.Windows.Forms;
using BlogSample.UI;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
public class App
{
public static void Main()
{
// 1. Step Configure and Initialize ActiveRecord:
// If you want to use the InPlaceConfigurationSource:
// Hashtable properties = new Hashtable();
// properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
// properties.Add("hibernate.dialect", "NHibernate.Dialect.MsSql2000Dialect");
// properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
// properties.Add("hibernate.connection.connection_string", "Data Source=.;Initial Catalog=test;Integrated Security=SSPI");
// InPlaceConfigurationSource source = new InPlaceConfigurationSource();
// source.Add(typeof(ActiveRecordBase), properties);
// We are using XmlConfigurationSource:
XmlConfigurationSource source = new XmlConfigurationSource("../appconfig.xml");
ActiveRecordStarter.Initialize( source, typeof(Blog), typeof(Post), typeof(User) );
// 2. Create the schema
// If you want to let AR to create the schema
if (MessageBox.Show("Do you want to let ActiveRecord create the database tables?", "Schema", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
ActiveRecordStarter.CreateSchema();
}
// 3. Create the first user (so you can log in)
if (User.GetUsersCount() == 0)
{
User user = new User("admin", "123");
user.Create();
}
// 4. Bring the Login Form
using(LoginForm login = new LoginForm())
{
if (login.ShowDialog() != DialogResult.OK)
{
return;
}
}
// 5. Show the main form
using(BlogManagement mainForm = new BlogManagement())
{
Application.Run(mainForm);
}
}
}
}
关于配置信息:
可以看到在main()函数第一句就是找配置信息
XmlConfigurationSource source = new XmlConfigurationSource("../appconfig.xml");
appconfig.xml如下:
appconfig.xml
<?xml version="1.0" encoding="utf-8" ?>
<activerecord>
<config>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver" />
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect" />
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider" />
<add
key="hibernate.connection.connection_string"
value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" />
</config>
</activerecord>
其实我们应该配置的属性有:
属性名 |
说明 |
hibernate.connection.provider_class |
定制IConnectionProvider的类型. 例如:full.classname.of.ConnectionProvider (如果提供者创建在NHibernate中), 或者 full.classname.of.ConnectionProvider, assembly (如果使用一个自定义的IConnectionProvider接口的实现,它不属于NHibernate)。 |
hibernate.connection.driver_class |
定制IDriver的类型. full.classname.of.Driver (如果驱动类创建在NHibernate中), 或者 full.classname.of.Driver, assembly (如果使用一个自定义IDriver接口的实现,它不属于NHibernate)。 |
hibernate.connection.connection_string |
用来获得连接的连接字符串。 |
hibernate.connection.isolation |
设置事务隔离级别. 请检查 System.Data.IsolationLevel 来得到取值的具体意义并且查看数据库文档以确保级别是被支持的。 例如: Chaos, ReadCommitted, ReadUncommitted, RepeatableRead, Serializable, Unspecified |
除了上面的ADO.NET属性之外,我们还有如下的可选属性
属性名 |
说明 |
hibernate.dialect |
NHibernate方言(Dialect)的类名 - 可以让NHibernate使用某些特定的数据库平台的特性 例如: full.classname.of.Dialect(如果方言创建在NHibernate中), 或者full.classname.of.Dialect, assembly (如果使用一个自定义的方言的实现,它不属于NHibernate)。 |
hibernate.default_schema |
在生成的SQL中,scheml/tablespace的全限定名. 例如: SCHEMA_NAME |
hibernate.prepare_sql |
是否准备sql语句 例如: true | false |
hibernate.session_factory_name |
SessionFactory被创建后将自动绑定这个名称. 例如: some.name |
hibernate.use_outer_join |
允许使用外连接抓取。 例如:true | false |
hibernate.cache.provider_class |
指定一个自定义的CacheProvider缓存提供者的类名 例如: full.classname.of.CacheProvider(如果ICacheProvider创建在NHibernate中), 或full.classname.of.CacheProvider, assembly(如果使用一个自定义的ICacheProvider,它不属于NHibernate)。 |
hibernate.query.substitutions |
把NHibernate查询中的一些短语替换为SQL短语(比如说短语可能是函数或者字符)。 例如: hqlLiteral=SQL_LITERAL, hqlFunction=SQLFUNC |
你会发现上面的配置项有的在实例的配置文件中找不到,为什么呢?那是因为配置信息一旦设置了一个hibernate.dialect方言(它是NHibernate.Dialect.Dialect 的一个子类)Hibernate会为上面列出的一些属性使用特殊默认值,省得我们手工指定。
NHibernate SQL 方言对照表:
数据库系统 |
SQL方言 |
DB2 |
NHibernate.Dialect.DB2Dialect |
PostgreSQL |
NHibernate.Dialect.PostgreSQLDialect |
MySQL |
NHibernate.Dialect.MySQLDialect |
Oracle (any version) |
NHibernate.Dialect.OracleDialect |
Oracle 9/10g |
NHibernate.Dialect.Oracle9Dialect |
Sybase |
NHibernate.Dialect.SybaseDialect |
Microsoft SQL Server 2000 |
NHibernate.Dialect.MsSql2000Dialect |
Microsoft SQL Server 7 |
NHibernate.Dialect.MsSql7Dialect |
Access (Jet) |
NHibernate.JetDriver.JetDialect, NHibernate.JetDriver |
Firebird |
NHibernate.Dialect.FirebirdDialect |
配置信息有了,配置方法有几种:
四种:
-
XmlConfigurationSource配置(就像上面例子中的配置一样)
-
InPlaceConfigurationSource配置(这是一种硬编码,当配置信息是动态获取的,就用这种方式)
代码如下:
Code
InPlaceConfigurationSource source = new InPlaceConfigurationSource();
Hashtable properties = new Hashtable();
properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("hibernate.dialect", "NHibernate.Dialect.MsSql2000Dialect");
properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("hibernate.connection.connection_string", "Initial Catalog=test;Data Source=.");
source.Add( typeof(ActiveRecordBase), properties );
ActiveRecordStarter.Initialize( source, typeof(ActiveRecordBase) );
3.使用应用程序的配置文件(Web.config 或者App.config)
Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
</configSections>
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Initial Catalog=test;Data Source=." />
</config>
</activerecord>
</configuration>
4.在Web应用程序中的配置(在Web应用程序中使用ActiveRecord,需要指定isWeb="true")
例子:
Code
<activerecord isWeb="true">
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Initial Catalog=test;Data Source=." />
</config>
</activerecord>
castle官网上的一些例子:
Code
MS SQLServer
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" />
</config>
</activerecord>
Oracle
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.OracleDialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Data Source=dm;User ID=dm;Password=dm;" />
</config>
</activerecord>
MySQL
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Database=test;Data Source=someip;User Id=blah;Password=blah" />
</config>
</activerecord>
Firebird
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.FirebirdDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.FirebirdDialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Server=localhost;Database=d:\db.fdb;User=SYSDBA;password=masterkey;ServerType=1;Pooling=false" />
<add key="hibernate.query.substitutions" value="true 1, false 0" />
</config>
</activerecord>
PostgreSQL
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.NpgsqlDriver" />
<add key="hibernate.dialect" value="NHibernate.Dialect.PostgreSQLDialect" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Server=localhost;initial catalog=nhibernate;User ID=nhibernate;Password=nhibernate;" />
</config>
</activerecord>
Access (Jet)
Connectivity to an Microsoft Access database via Jet requires a reference to NHibernate.JetDriver.
<activerecord>
<config>
<add key="hibernate.connection.driver_class" value="NHibernate.JetDriver.JetDriver, NHibernate.JetDriver" />
<add key="hibernate.dialect" value="NHibernate.JetDriver.JetDialect, NHibernate.JetDriver" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.connection_string" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=nhibernate.mdb" />
</config>
</activerecord>
关于初始化:
初始化语句:
XmlConfigurationSource source = new XmlConfigurationSource("../appconfig.xml");
ActiveRecordStarter.Initialize( source, typeof(Blog), typeof(Post), typeof(User) );
这时就产生了两个疑问:
1. 在ActiveRecordStarter.Initialize()我有多少个实例我都要typeof()吗?有没有其他办法?
2.这个是Form程序我可以将初始化语句写在main函数的开头,如果是web程序我将初始化写在哪里?
答案:
1.你可以把实体类的程序集都一起初始化了
例子:ActiveRecordStarter.Initialize(Assembly.Load("BlogSample"), source)
(注意:在程序前using System.Reflection; )
2.如果是web程序初始化你可以把它放在Global.asax的Application_ Start方法里面。(注意在前面添加
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="Castle.ActiveRecord" %>
<%@ Import Namespace="Castle.ActiveRecord.Framework" %>,三个引用)