前段时间下载了castle的active record(AR)进行试用,通过几天的研究使用,有些心得,现进行一下整理,将所学,所得,所惑都理一下,以待在以后能更好的学习研究.
本文主要内容:Castle-AR的基本使用,通过一个小小的例子,做到能熟练的使用AR,对AR有一个感性的认识,在以后空余时间能对其进行深入的研究,学习.
话不多说,进入主题,ORM的概念博客园上已经介绍得很多,这里就不再提及.先介绍一下例子,这是我自己想的一个环境,包括如下对象:Blog(博客),Post(随笔),PostType(随笔类型),Community(社区),之所以虚构这几个对象,主要是想考察AR对各种关系的表现能力,和处理容易度,而这几个对象之间可以包括数据库中常见的一对多(Blog和Post的关系),多对一(Post和PostType的关系),多对多的关系(Blog和Community的关系),一对一的关系暂不考虑.库的结构如下图所示:
![%E6%9C%AA%E5%91%BD%E5%90%8D.jpg](https://i-blog.csdnimg.cn/blog_migrate/e95395e9de0ca41eab2eaff60071e195.jpeg)
生成数据库的脚本如下:
Sql
#region Sql
CREATE TABLE [dbo].[Blog] (
[BlogId] [int] IDENTITY (1, 1) NOT NULL ,
[BlogName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[BlogIntro] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[Email] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[RegistTime] [datetime] NULL
) ON [PRIMARY]
GO
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
CREATE TABLE [dbo].[Blog_Community] (
[BlogId] [int] NOT NULL ,
[CommunityId] [int] NOT NULL
) ON [PRIMARY]
GO
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
CREATE TABLE [dbo].[Community] (
[CommunityId] [int] IDENTITY (1, 1) NOT NULL ,
[CommunityName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[CommunityIntro] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
CREATE TABLE [dbo].[Post] (
[PostId] [int] IDENTITY (1, 1) NOT NULL ,
[PostTypeId] [int] NULL ,
[BlogId] [int] NULL ,
[PostName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[PostContext] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[PostTime] [datetime] NULL ,
[IsPost] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
CREATE TABLE [dbo].[PostType] (
[PostTypeId] [int] IDENTITY (1, 1) NOT NULL ,
[PostTypeName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
#endregion
OK,环境搭好了.现在进入我们最关心的部分
1.AR 之 一对多.
在该例子中Blog与Post之间的关系是一对多的.现在我们先来介绍一下由AR Generator自动生成的代码
[ActiveRecord(
"
Blog
"
)]
public
class
Blog : ActiveRecordBase
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
}
ActiveRecord特性表明该实体类与数据库中的哪张表对应.凡是要通过AR与库中的表进行映射的实体类必须继承ActiveRecordBase类.
[PrimaryKey(PrimaryKeyType.Native)]
public
int
BlogId
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.blogId;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.blogId = value;
}
}
这个地球人一看就明白了吧:)这里因为我属性的名称与库中的字段名称是一样的,所以,不用将该属性映射的字段写出来.非主键的属性可以通过[Property()]特性描述.(怎么样?是不是觉得比Nhibernate的映射文件要简单了许多啊?)
现在到了关键,我们的一对多关系:
[HasMany(
typeof
(Post), Table
=
"
Post
"
, ColumnKey
=
"
BlogId
"
)]
public
System.Collections.IList Posts
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.posts;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.posts = value;
}
}
AR采用HasMany来描述这种关系,后面参数的名称想必一看便知,Post是"多"的表名,BlogId是该表的主建.
好,我们再来看看Post类中的关键映射代码:
[BelongsTo(
"
BlogId
"
)]
public
Blog Blog
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.blog;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.blog = value;
}
}
是不是同样简单呢?
2.AR 之 多对一.
(这就不多讲了,只是HasMany和BelongsTo放的地方不同,关注的方向不同而已,用法都一样)
3.AR之 多对多.
在这个例子中,Blog和Community是多对多的关系.在我们一般的设计中,一般会把多对多的两个表拆成三个表,在这里,我加入了一个表Blog_Community.(该表不需要在AR中进行映射,AR会自动帮你完成相关操作).我们先看一下Blog里面的关键代码:
[HasAndBelongsToMany(
typeof
(Community), Table
=
"
Blog_Community
"
,ColumnRef
=
"
CommunityId
"
, ColumnKey
=
"
BlogId
"
)]
public
System.Collections.IList Community
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.community;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.community = value;
}
}
AR使用HasAndBelongsToMany来表示多对多的关系.其他参数我想各位一看便知道,不多费话.再看看Community里面的关键代码:
[HasAndBelongsToMany(
typeof
(Blog), Table
=
"
Blog_Community
"
,ColumnRef
=
"
BlogId
"
, ColumnKey
=
"
CommunityId
"
)]
public
System.Collections.IList Blog
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.blog;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.blog = value;
}
}
OK.这就完成了AR中的多对多的映射关系.(注意一点:我在生成各实体类的代码的时候,是使用的AR带的一个AR Generator,在将Blog表选进去的时候,他把Blog_Community和Community表都导进去了,从而生成的是一个一对多的关系和一个多对一个关系,所以后来我手动修改的代码添加了HasAndBelongsToMany的特性,不知道这是否是AR Generetor的一个BUG).
OK.这三种关系的映射就先介绍到这,以下是我写的一些测试代码.感兴趣的可以瞄两眼.
[Test]
public
void
TestBlogCRUD()
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
{
Blog b = new Blog();
b.BlogName = "DotNetFresh";
b.BlogIntro = "StudyDotNet";
b.Email = "hello@163.com";
b.RegistTime = DateTime.Now;
b.Create(); //C
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
b.BlogIntro = "Good good study,day day up";
b.Update(); //U
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Blog bb = Blog.Find(b.BlogId); //R
bb.Delete(); //D
}
[Test]
public
void
TestAddBlogWithPostAndPostType()
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
{
//新建一个Blog
Blog b = new Blog();
b.BlogName = "withpost";
b.BlogIntro = "i'm have post already";
b.Email = "hello@163.com";
b.RegistTime = DateTime.Now;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//新建一个PostType
PostType type = new PostType();
type.PostTypeName = "new type";
//新建一个Post
Post p = new Post();
p.Blog = b;
p.PostType = type;
p.PostName = "new post";
p.PostTime = DateTime.Now;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//执行事务,将上面三个对象持久化到数据库
using(TransactionScope transaction = new TransactionScope())
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
b.Create();
type.Create();
p.Create();
}
catch
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
transaction.VoteRollBack();
}
}
}
![None.gif](/Images/OutliningIndicators/None.gif)
[Test]
public
void
TestAddBlogWithCommunity()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//新建一个Blog
Blog b = new Blog();
b.BlogName = "withcommunity";
b.BlogIntro = "i'm in a community already";
b.Email = "hello@163.com";
b.RegistTime = DateTime.Now;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//该Blog属于的社区
ArrayList al = new ArrayList();
al.Add(Community.Find(3));
al.Add(Community.Find(4));
b.Community = al;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
b.Create(); //保存
}
好了.不贴了,太多了,从这些代码中同样体现出了幽雅的代码书写方式...好了.剩下的好处各位介绍ORM的说得太多了....;)
总结,心得:Castle的AR底层将NHibernate封装了起来,使用起来感觉比Nhibernate方便多了,不用再去生成一个个的映射文件,调试和维护起来都方便了很多,这应该是使用AR感觉最好的一个地方.另外配合着AR Generator,生成这些代码那叫一个快.不过AR采用反射的方式来处理映射,如果项目大了,不知道会对性能造成多大的影响?另外,经过Castle的这样封装,如果再配合着Castle的容器,一定会发挥更大的威力.继续研究学习吧.....:)