摘要:本文将会介绍使用ActiveRecord中的一些技巧。
主要内容
1.由实体类生成数据表
2.运行存在的SQL脚本
3.使用空属类型
4.使用枚举类型的属性
5.使用NHibernate中的日志记录
一.由实体类生成数据表
在前面所用到的例子中我们都是先有数据表结构,然后才有实体类,然而这会让很多朋友认为ORM怎么变成了ROM了,其实这只是我们平时的一个开发时的习惯问题,ActiveRecord是支持先有实体类,再由实体类生成数据库表。只不过我们可以在开发中根据项目的实际情况在这两种之间选择。看下面的代码,要生成数据库表结构,在实体类中需要多提供一些信息,是否为空,字段长度等。
[ActiveRecord(
"
Blogs
"
)]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
Blog : ActiveRecordBase
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _id;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _name;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _author;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[PrimaryKey(PrimaryKeyType.Native, "blog_id")]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Id
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _id; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _id = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property("blog_name", NotNull=true, Length=25)]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Name
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _name = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property("blog_author", NotNull=true, Length=50)]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Author
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _author; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _author = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
要生成数据库表需要调用ActiveRecordStarter.CreateSchema()方法就可以了。
public
void
Initli()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
XmlConfigurationSource source = new XmlConfigurationSource("MyConfig.xml");
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ActiveRecordStarter.Initialize( source, typeof(Blog),typeof(Post),typeof(Custom));
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ActiveRecordStarter.CreateSchema();
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
这里需要注意两点:
1.生成数据库表时只有当该表不存在时ActiveRecord才会生成,否则表如果存在ActiveRecord不会做任何事情,也不会报任何错误。
2.如果在实体类中没有指定字段的长度和是否为空,则默认生成的字段是允许为空的,且字符类生成后的字段类型为Nvarchar,长度为255。
二.运行存在的SQL脚本
有时候我们会想在ActiveRecord框架启动时运行一个已经存在的SQL脚本来生成数据库表结构,ActiveRecord同样也提供了这样的功能,通过调用ActiveRecordStarter.CreateSchemaFromFile()来实现。示例代码如下:
public
void
Initli()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
XmlConfigurationSource source = new XmlConfigurationSource("MyConfig.xml");
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ActiveRecordStarter.Initialize( source, typeof(Blog));
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ActiveRecordStarter.CreateSchemaFromFile("MySqlScript.sql");
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
三.使用空属类型
在进行数据库操作时,有时候需要进行空值的处理,在ActiveRecord中给我们提供了一组空属类型,可以方便的进行处理,比如可以这样写属性:
[Property]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
NullableDateTime CreatedDate
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _createdDate; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _createdDate = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
[Property]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
public
NullableInt32 Count
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _count; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _count = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
看一下ActiveRecord提供的空属类型与实际类型对照表
CLR Basic Type
| Nullable Type
|
System.Boolean
| Nullables.NullableBoolean
|
System.Byte
| Nullables.NullableByte
|
System.Char
| Nullables.NullableChar
|
System.DateTime
| Nullables.NullableDateTime
|
System.Decimal
| Nullables.NullableDecimal
|
System.Double
| Nullables.NullableDouble
|
System.Guid
| Nullables.NullableGuid
|
System.Int16
| Nullables.NullableInt16
|
System.Int32
| Nullables.NullableInt32
|
System.Int64
| Nullables.NullableInt64
|
System.SByte
| Nullables.NullableSByte
|
System.Single
| Nullables.NullableSingle
|
注意在使用空属类型时需要添加以下引用
Nullables.dll
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
Nullables.NHibernate.dll
四.使用枚举类型属性
在ActiveRecord中我们可以定义一个属性的类型为枚举类型,示例代码:
public
class
Post : ActiveRecordBase
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//![](http://terrylee.cnblogs.com/Images/dot.gif)
![](http://terrylee.cnblogs.com/Images/dot.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private StatusType _status_type_id;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public enum StatusType
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Editing = 0,
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Published = 1,
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Archived = 2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property("status_id")]
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public StatusType Status
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _status_type_id; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _status_type_id = value; }
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
在使用该实体类的StatusType属性时,可以这样赋值:
Post.Status
=
Post.StatusType.Archived;
五.使用NHibernate的日志记录
用过NHibernate的朋友都知道,NHibernate是用Log4net来记录日志的,在ActiveRecord中也可以通过简单的配置来使用Log4net记录。配置示例:
<?
xml version="1.0" encoding="utf-8"
?>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
configuration
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
configSections
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
section
name
="nhibernate"
type
="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
section
name
="activerecord"
type
="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
section
name
="log4net"
type
="log4net.Config.Log4NetConfigurationSectionHandler, log4net"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
configSections
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
activerecord
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
activerecord
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
log4net
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<!--
Define some output appenders
-->
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
appener
name
="trace"
type
="log4net.Appender.TraceAppender, log4net"
>
Õ
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
layout
type
="log4net.Layout.PatternLayout,log4net"
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
param
name
="ConversionPattern"
value
="%d [%t] %-5p %c [%x] <%P{user}> - %m%n"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
layout
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
appener
>
Õ
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
appener
name
="console"
type
="log4net.Appender.ConsoleAppender, log4net"
>
Õ
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
layout
type
="log4net.Layout.PatternLayout,log4net"
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
param
name
="ConversionPattern"
value
="%d [%t] %-5p %c [%x] <%P{user}> - %m%n"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
layout
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
appener
>
Õ
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
appener
name
="rollingFile"
type
="log4net.Appender.RollingFileAppender,log4net"
>
Õ
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
param
name
="File"
value
="log.txt"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
param
name
="AppendToFile"
value
="true"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
param
name
="RollingStyle"
value
="Date"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
param
name
="DatePattern"
value
="yyyy.MM.dd"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
param
name
="StaticLogFileName"
value
="true"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
layout
type
="log4net.Layout.PatternLayout,log4net"
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
param
name
="ConversionPattern"
value
="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
layout
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
appener
>
Õ
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
root
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<!--
priority value can be set to ALL|INFO|WARN|ERROR
-->
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
priority
value
="ALL"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
appener-ref
ref
="rollingFile"
/>
Õ
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
root
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
log4net
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
nhibernate
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<!--
with this set to true, SQL statements will output to the console window if it's a console app
-->
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
<
add
key
="hibernate.show_sql"
value
="true"
/>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
nhibernate
>
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
</
configuration
>
初始化配置在调用ActiveRecordStarter.Initialize()方法之前
public
void
Initli()
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
XmlConfigurationSource source = new XmlConfigurationSource("MyConfig.xml");
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
log4net.Config.XmlConfigurator.Configure();
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ActiveRecordStarter.Initialize( source, typeof(Blog));
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
六.
Hooks
有时候我们会在保存,加载,删除等操作时做一些必需的处理,这时可以通过重载以下三个方法来实现:
BeforeSave(IDictionary state)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
BeforeLoad(IDictionary state)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
BeforeDelete(IDictionary state)
比如说我们想在保存的时候设置Post的创建时间为当前时间,可以这样去写
protected
override
bool
BeforeSave(IDictionary state)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
state["Created"] = DateTime.Now;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return true;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
本文就到这里了,同时本篇也是Castle ActiveRecord学习实践系列的最后一篇,过几天我会提供一个完整的ActiveRecord的例子程序。下面有时间我会继续写Castle中的其他部分,包括IOC及Facility,Aspect#,DynamicProxy等。
参考资料
文中很多内容来自于Castle的官方网站http://www.castleproject.org
作者:
TerryLee
出处:
http://terrylee.cnblogs.com