[导入]Castle ActiveRecord学习记录 - 处理继承关系(续)

原文链接:http://www.cnblogs.com/debug1/archive/2006/05/19/413756.html

  在上一篇处理继承关系中,我们介绍了使用Subclass和Discriminator来实现简单的继承关系。这一篇,将介绍如何使用Discriminator来实现稍微复杂一些的继承关系。


  在此之前,要先说明一下:本次设计中的实体类都未继承自ActiveRecordBase。所以这里还要引入ActiveRecordMediator类,它可以让没有继承自ActiveRecordBase的实体使用ActiveRecord的功能。
ActiveRecordMediator
  Namespace: Castle.ActiveRecord
  Assembly:    Castle.ActiveRecord (in Castle.ActiveRecord.dll)


来看实体类设计:

[基类]

public abstract class EntityBase
{
private int _id;
public EntityBase()
{
}
/// 
/// 虽然类没有打上"ActiveRecord"属性标签,
/// 但该属性在子类中将被做为主键持久化到数据库
/// 
        [PrimaryKey(PrimaryKeyType.Native, "id")]
public int ID
{
get { return _id; }
set { _id = value; }
}
public virtual void Save()
{
ActiveRecordMediator.Save(this);
}
public virtual void Create()
{
ActiveRecordMediator.Create(this);
}
public virtual void Update()
{
ActiveRecordMediator.Update(this);
}
public virtual void Delete()
{
ActiveRecordMediator.Delete(this);
}
public virtual object Execute(NHibernateDelegate call)
{
return ActiveRecordMediator.Execute(this.GetType(), call, this);
}
}

请注意,实体类中ID属性被上打了"PrimaryKey"标签,另外EntityBase里还包含了基本的实例方法 Save(),Create(),Update(),Delete(),Execute()。


[论坛类]

[ActiveRecord("Forum")]
public class ForumEntity : EntityBase
{
private string _name;
private IList _posts = new ArrayList();
public ForumEntity()
{
}
public ForumEntity(string name)
{
this._name = name;
}
[Property("name")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[HasMany(typeof(Posts), Table = "Thread", ColumnKey = "forum_id", Cascade = ManyRelationCascadeEnum.All)]
public IList Posts
{
get { return _posts; }
set { _posts = value; }
}
public static void DeleteAll()
{
ActiveRecordMediator<ForumEntity>.DeleteAll();
}
public static ForumEntity[] FindAll()
{
return ActiveRecordMediator<ForumEntity>.FindAll();
}
public static ForumEntity Find(int id)
{
return ActiveRecordMediator<ForumEntity>.Find(id);
}
public static ForumEntity TryFind(int id)
{
return ActiveRecordMediator<ForumEntity>.TryFind(id);
}
}

由于在EntityBase中已经指定了PrimaryKey,所以ForumEntity类中没有再给他指定主键字段(下面的ThreadEntity类也是如此)。

[主题跟回帖类]

[ActiveRecord(DiscriminatorValue = "post")]
public class Posts : ThreadEntity
{
private ForumEntity _forum;
public Posts()
{
}
public Posts(string author, string title)
: base(author, title)
{
}
[BelongsTo("forum_id")]
public ForumEntity Forum
{
get { return _forum; }
set { _forum = value; }
}
public static void DeleteAll()
{
ActiveRecordMediator<Posts>.DeleteAll();
}
public static Posts[] FindAll()
{
return ActiveRecordMediator<Posts>.FindAll();
}
public static Posts Find(int id)
{
return ActiveRecordMediator<Posts>.Find(id);
}
public static Posts TryFind(int id)
{
return ActiveRecordMediator<Posts>.TryFind(id);
}
}
[ActiveRecord(DiscriminatorValue = "reply")]
public class Replies : ThreadEntity
{
private Posts _post;
public Replies()
{
}
public Replies(string author, string title)
: base(author, title)
{
}
[BelongsTo("post_id", Insert = false, Update = false, Cascade = CascadeEnum.None)]
public Posts Post
{
get { return _post; }
set { _post = value; }
}
public static void DeleteAll()
{
ActiveRecordMediator<Replies>.DeleteAll();
}
public static Replies[] FindAll()
{
return ActiveRecordMediator<Replies>.FindAll();
}
public static Replies Find(int id)
{
return ActiveRecordMediator<Replies>.Find(id);
}
public static Replies TryFind(int id)
{
return ActiveRecordMediator<Replies>.TryFind(id);
}
}

这里的Thread可以理解为主题(Post)和回帖(Reply)的集合。


可以运行下面的代码做个简单测试:

private void Test()
{
ForumEntity forum = new ForumEntity("Castle ActiveRecord");
forum.Create();
ForumEntity f1 = ForumEntity.TryFind(1);
MessageBox.Show(f1 == null ? "forum not found..." : "Forum:" + f1.Name);
if (f1 != null)
{
Posts post = new Posts("debug_fan", "post-1");
post.Forum = f1;
post.Create();
Posts p1 = Posts.TryFind(1);
MessageBox.Show(p1 == null ? "post not found..." : "Post:\"" + p1.Title + "\" belongs to [Forum:" + (p1.Forum == null ? "" : p1.Forum.Name) + "]");
if (p1 != null)
{
Replies reply = new Replies("fan", "re: " + p1.Title);
reply.Post = p1;
reply.Create();
Replies r1 = Replies.FindAll()[0];
MessageBox.Show(r1 == null ? "reply not found..." : "Reply:\"" + r1.Title + "\" belongs to [Post:" + (r1.Post == null ? "" : r1.Post.Title) + "]");
}
}
}




 

[导入]Castle ActiveRecord学习记录 - 处理继承关系(续)
文章来源:http://www.agilelabs.cn/blogs/%e8%8c%83%e4%bf%8a/archive/2006/05/19/1227.aspx

转载于:https://www.cnblogs.com/debug1/archive/2006/05/19/413756.html

上一篇:node.js Next框架的三种渲染方式:客户端渲染、SSG、SSR


下一篇:精讲响应式WebClient第3篇-POST、DELETE、PUT方法使用