所有需要发布的实体都需要实现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
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