【IT168技术文档】
Castle ActiveRecord在.Net2.0下支持泛型,这极大的方便了我们创建强类型集合以及对对象的强类型操作.本文引用了Castle站点上泛型的例子来详细介绍如何应用泛型. 另外你需要在这里找到NHibernate.Generics 来支持AR对泛型的扩展.这个例子描述了最简单的Blog与Post一对多的关系,为了让示例更清晰,做了少许修改.
NHibernate.Generics 中包含了若干供我们引用的泛型集合EntitySet,EntityList,EntityDictionary等,以及与这些集合相匹配的EntityRef.
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]
#region 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)); } }