Castle ActiveRecord在.Net2.0下支持泛型,这极大的方便了我们创建强类型集合以及对对象的强类型操作.本文引用了Castle站点上泛型的例子来详细介绍如何应用泛型.
Castle ActiveRecord在.Net2.0下支持泛型,这极大的方便了我们创建强类型集合以及对对象的强类型操作.本文引用了Castle站点上泛型的例子来详细介绍如何应用泛型. 另外你需要在这里找到NHibernate.Generics 来支持AR对泛型的扩展.这个例子描述了最简单的Blog与Post一对多的关系,为了让示例更清晰,做了少许修改.
NHibernate.Generics 中包含了若干供我们引用的泛型集合EntitySet,EntityList,EntityDictionary等,以及与这些集合相匹配的EntityRef.
DB结构
CREATE TABLE [dbo].[Blogs] (
[blog_id] [int] IDENTITY (1, 1) NOT NULL ,
[blog_name] [varchar] (50) NULL ,
) ON [PRIMARY]
CREATE TABLE [dbo].[Posts] (
[post_id] [int] IDENTITY (1, 1) NOT NULL ,
[post_title] [varchar] (50) NULL ,
[post_contents] [text] NULL ,
[post_blogid] [int] NULL ,
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Bolg Class
[ActiveRecord]
public class Blog : ActiveRecordBase
{
EntitySet<Post> _posts;
int _id;
string _name;
private Blog()
{
_posts = new EntitySet<Post>(
delegate(Post p) { p.Blog = this; },
delegate(Post p) { p.Blog = null; }
);
}
[PrimaryKey(PrimaryKeyType.Identity, "blog_id", Access = PropertyAccess.NoSetterCamelCaseUnderscore)]
public int Id
{
get { return _id; }
}
[Property("blog_name")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[HasMany(typeof(Post), "post_blogId", "Posts", Inverse = true,
CustomAccess = Generics.Access, RelationType = RelationType.Set)]
public ICollection<Post> Posts
{
get { return _posts; }
}
public static Blog FindByName(string value)
{
return ActiveRecordBase<Blog>.FindOne(new EqExpression("Name", value));
}
}
在BlogClass中看到了这样的定义EntitySet<Post> _posts.
首先看看构造函数中对它的初始化2个匿名delegate的作用是当向Bolg.Posts集合中添加或移除一个Post时,自动添加或移除Post.Bolg属性设置.省却了每次都需要做双向设置.
再来看一下Bolg.Posts的公开属性返回一个ICollection的接口是很重要的而不是具体类型,这样屏蔽了直接使用NHibernate.Generics中的集合所带来的依赖.
最后看看顶上的attribute,对应关系不多讲,CustomAccess = Generics.Access是一定要标明的描述了泛型访问方式.RelationType = RelationType.Set同样要标识明确RelationType.Set对应EntitySet, RelationType.Bag则对应EntityList.Inverse属性有可无了,它指明了即使Bolg.Posts中的Post没有被存入数据库Bolg一样可以被先保存,通常我认为使用Cascade = ManyRelationCascadeEnum.All会更好.
还有一点需要注意的是应用了泛型的私有变量命名如 _posts,按照AR目前最新的DailyBulid你必须按照下划线这样的格式命名,Access属性的设置似乎是无效的.如果使用其他方式会出错.
泛型集合之后来关注下其他问题.
你看到了Id的attribute,由于id是自动生成的所以无需设置set访问器,Access = PropertyAccess.NoSetterCamelCaseUnderscore会帮你赋值NoSetterCamelCaseUnderscore指出Id属性的私有变量命名方式是无set访问器的以下划线开头的Camel命名方式.
对对象的数据库操作同样可以使用泛型,可以看到FindByName中省去了麻烦的转型.
Post Class
[ActiveRecord]
public class Post : ActiveRecordBase
{
int _id;
string _title;
string _content;
EntityRef<Blog> _blog;
[PrimaryKey(PrimaryKeyType.Identity, "post_id",
Access = PropertyAccess.NoSetterCamelCaseUnderscore)]
public int Id
{
get { return _id; }
}
[Property("post_title")]
public string Title
{
get { return _title; }
set { _title = value; }
}
[Property("post_content")]
public string Content
{
get { return _content; }
set { _content = value; }
}
[BelongsTo("post_blog_id",
CustomAccess = Generics.Access)]
public Blog Blog
{
get { return _blog.Value; }
set { _blog.Value = value; }
}
public Post()
{
_blog = new EntityRef<Blog>(
delegate(Tests.Blog b) { b.Posts.Add(this); },
delegate(Tests.Blog b) { b.Posts.Remove(this); }
);
}
}
在PostClass中EntityRef是一个对对象的包装,使得它能够添加delegate与Entityxx集合对应来处理添加或移除的同步操作.在Post.Blog属性中可以通过_blog.Value来访问与设置EntityRef包装的值.attribute并没有特别支处,只是CustomAccess = Generics.Access也是不可缺少的属性.
现在你可以使用ActiveRecord轻松编程了.