BlogEngine.net學習(一)——實體類

來源:互聯網
上載者:User
    在BlogEngine.net中處理的和業務相關的實體有Post(文章),Page(頁面),Category(目錄)。其中的類別關係圖如下
所有需要發布的實體都需要實現IPublishiable介面。其中業務實體都可對應多個目錄,則在IPublishiable介面中有  StateList<Category> Categories { get;}欄位。StateList可以檢測是否已經修改過。
其中實現如下:

 1     /// <summary>
 2     /// A generic collection with the ability to 
 3     /// check if it has been changed.
 4     /// </summary>
 5     [System.Serializable]
 6     public class StateList<T> : System.Collections.Generic.List<T>
 7     {
 8 
 9         #region Base overrides
10         /// <summary>
11         /// Serves as a hash function for a particular type. <see cref="M:System.Object.GetHashCode"></see> is suitable for use in hashing algorithms and data structures like a hash table.
12         /// </summary>
13         /// <returns>
14         /// A hash code for the current <see cref="T:System.Object"></see>.
15         /// </returns>
16         public override int GetHashCode()
17         {
18             string hash = string.Empty;
19             foreach (T item in this)
20             {
21                 hash += item.GetHashCode().ToString();
22             }
23 
24             return hash.GetHashCode();
25         }
26 
27         /// <summary>
28         /// Determines whether the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>.
29         /// </summary>
30         /// <param name="obj">The <see cref="T:System.Object"></see> to compare with the current <see cref="T:System.Object"></see>.</param>
31         /// <returns>
32         /// true if the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>; otherwise, false.
33         /// </returns>
34         public override bool Equals(object obj)
35         {
36             if (obj == null)
37             {
38                 return false;
39             }
40 
41             if (obj.GetType() == this.GetType())
42             {
43                 return obj.GetHashCode() == this.GetHashCode();
44             }
45 
46             return false;
47         }
48 
49         #endregion
50 
51         private int _HasCode = 0;
52 
53         /// <summary>
54         /// Gets if this object's data has been changed.
55         /// </summary>
56         /// <returns>A value indicating if this object's data has been changed.</returns>
57         public virtual bool IsChanged
58         {
59             get
60             {
61                 return this.GetHashCode() != _HasCode;
62             }
63         }
64 
65         /// <summary>
66         /// Marks the object as being clean, 
67         /// which means not changed.
68         /// </summary>
69         public virtual void MarkOld()
70         {
71             _HasCode = this.GetHashCode();
72             base.TrimExcess();
73         }
74     }
75 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.