Castle ActiveRecord框架學習(二):快速搭建簡單博客網站

1、數據庫

  1.數據表

  Category:類別標籤表(字段Type=1爲類別,Type=2爲標籤)html

  Category_Post:類別標籤與文章中間表數據庫

  Post:文章表app

  Comment:評論表ide

  2.數據庫關係圖

  

  3.簡單說明

  Category和Post表爲多對多關係post

  Post和Comment表 爲一對多關係ui

2、實體類

  1.Category類:

  // 指定數據表,Lazy爲延遲加載
    [ActiveRecord("Category",Lazy=true)]
    public class Category : ActiveRecordBase<Category>
    {
        // 指定數據表中的主鍵
        [PrimaryKey("Id")]
        public virtual int Id { get; set; }

        // 指定數據表中的列
        [Property("Title")]
        public virtual string Title { get; set; }

        [Property("Description")]
        public virtual string Description { get; set; }

        [Property("DateAdded")]
        public virtual DateTime DateAdded { get; set; }

        [Property("Type")]
        public virtual int Type { get; set; }

        // 多對多
        // typeof(Post):對方表的實體類,Table:關聯中間表,ColumnRef:關聯中間表中與對方實體相關的列,ColumnKey:關聯中間表中與本實體相關的列,Lazy:延遲加載,經過本實體獲取對方實體信息時,纔會去數據庫查詢
        [HasAndBelongsToMany(typeof(Post),Table = "Category_Post", ColumnRef = "Post_Id", ColumnKey = "Category_Id",Lazy=true)]
        public virtual IList<Post> Posts { get; set; }


        public static IList<Category> FindAllForTopCategory()
        {
            SimpleQuery<Category> query = new SimpleQuery<Category>(@" from Category c where c.Type=1");
            return query.Execute();
        }

        public static Category Find(int id)
        {
            return FindByPrimaryKey(id);
        }
    }
View Code

  2.Post類:

    // 指定數據表
    [ActiveRecord("Post",Lazy=true)]
    public class Post : ActiveRecordBase<Post>
    {
        // 指定數據表中的主鍵
        [PrimaryKey(PrimaryKeyType.Identity, "Id")]
        public virtual int Id { get; set; }

        // 指定數據表中的列
        [Property("Subject")]
        public virtual string Subject { get; set; }

        [Property("Text")]
        public virtual string Text { get; set; }

        [Property("DateAdded")]
        public virtual DateTime DateAdded { get; set; }

        // 一對多
        // typeof(Comment):對方的實體類,Table:對方表名,ColumnKey:對方表的外鍵,Lazy:延遲加載
        [HasMany(typeof(Comment), Table = "Comment", ColumnKey = "PostId",Lazy=true)]
        public virtual IList<Comment> Comments { get; set; }

        // 多對多
        [HasAndBelongsToMany(typeof(Category),Table="Category_Post",ColumnRef="Category_Id",ColumnKey="Post_Id",Lazy=true)]
        public virtual IList<Category> Categorys { get; set; }

        public static Post Find(int id)
        {
            return FindByPrimaryKey(id);
        }
    }
View Code

  3.Comment類:

// 指定數據表
    [ActiveRecord("Comment",Lazy=true)]
    public class Comment : ActiveRecordBase<Comment>
    {
        // 指定數據表中的主鍵
        [PrimaryKey("Id")]
        public virtual int Id { get; set; }

        // 指定數據表中的列
        [Property("Author")]
        public virtual string Author { get; set; }

        [Property("Text")]
        public virtual string Text { get; set; }

        [Property("DateAdded")]
        public virtual DateTime DateAdded { get; set; }

        // 多對一,對應Post的的Comments屬性
        [BelongsTo(Column = "PostId")]
        public virtual Post Post { get; set; }
    }
View Code

  說明:

  1.Category和Post在數據庫中是多對多關係,由一張中間表Category_Post互相關聯,但在C#實體類中則不須要爲Category_Post創建實體模型 。spa

  2.多對多在Castle ActiveRecord中經過HasAndBelongsToMany特性進行關聯,具體的使用在代碼註釋中以說明,見Category類和Post類中的代註釋。(官網中文3d

  3.一對多在Castle ActiveRecord中經過HasAndBelongsToMany特性進行關聯,具體的使用在代碼註釋中以說明,見Post類中的代註釋。(中文code

  4.多對一在Castle ActiveRecord中經過BelongsTo特性進行關聯,具體的使用在代碼註釋中以說明,見Comment類中的代註釋。(中文htm

  5.Lazy表明延遲加載,能夠爲實體類自己進行添加,也能夠爲多對多和一對多等關係字段進行添加,具體效果能夠自行查看。(官網中文

  6.啓動Lazy模式時,實例類中的上述必須爲virtual。

3、頁面展現

  首頁:

  主要代碼:

 private void LoadIndexPost()
        {
            StringBuilder sb = new StringBuilder();
            using (new SessionScope())
            {
                IList<Model.Post> PostListForIndex = Model.Post.FindAll();
                foreach (Model.Post post in PostListForIndex)
                {
                    sb.Append("<h3><a href=\"Post.aspx?id=" + post.Id + "\">" + post.Subject + "</a></h3>");
                    sb.Append("<figure><img src=\"images/ex03.png\"></figure>");
                    sb.Append("<ul><p><a href=\"Post.aspx?id=" + post.Id + "\">" + post.Text + "</a></p></ul>");
                    sb.Append("<div class=\"dateview\"><a title=\"/\" href=\"/\" target=\"_blank\" class=\"readmore\">閱讀全文>></a><span>" + post.DateAdded.ToString("yyy-MM-dd") + "</span>");
                    sb.Append("<span><a href=\"Post.aspx?id=" + post.Id + "#Comment\">評論數:" + post.Comments.Count() + "</a></span>");
                    sb.Append("<span>標籤:");
                    post.Categorys.ForEach(c => sb.Append("[<a href=\"Category.aspx?id=" + c.Id + "\">" + c.Title + "</a>]"));
                    sb.Append("</span></div>");
                }
                PostContent = sb.ToString();
            }
        }
View Code

  說明 :

  當在實體類中啓用Lazy模式時,經過關聯從一個實體獲取另外一個實體的信息時,必須使用new SessionScope(),開始時使用Model.Post.FindAll()只獲取須要的文章數據,因爲啓用了Lazy模式,沒有獲取與 Post表相關聯的全部數據,可是在後續代碼中發現須要評論表中的數據和類別標籤表中的數據,那就要保存當前的Session,以便後續的數據庫操做。

4、參考資料

  官方資料:http://docs.castleproject.org/

  中文資料:TerryLee

  模版提供:楊清

5、演示代碼

  下載

相關文章
相關標籤/搜索