1
Using#region Using
2
3
using System;
4
using System.Collections.Generic;
5
using LiveBlog.Core.Providers;
6
7
#endregion
8
9
namespace LiveBlog.Core
10
...{
11
/**//// <summary>
12
/// Categories are a way to organize posts.
13
/// A post can be in multiple categories.
14
/// </summary>
15
[Serializable]
16
public class Category : BusinessBase<Category, Guid>, IComparable<Category>
17
...{
18
19
internal static string _Folder = System.Web.HttpContext.Current.Server.MapPath(BlogSettings.Instance.StorageLocation);
20
21
Constructor#region Constructor
22
23
/**//// <summary>
24
/// Initializes a new instance of the <see cref="Category"/> class.
25
/// </summary>
26
public Category()
27
...{
28
Id = Guid.NewGuid();
29
}
30
31
/**//// <summary>
32
///
33
/// </summary>
34
/// <param name="title"></param>
35
/// <param name="description"></param>
36
public Category(string title, string description)
37
...{
38
this.Id = Guid.NewGuid();
39
this._Title = title;
40
this._Description = description;
41
}
42
43
#endregion
44
45
Properties#region Properties
46
47
private string _Title;
48
/**//// <summary>
49
/// Gets or sets the Title or the object.
50
/// </summary>
51
public string Title
52
...{
53
get ...{ return _Title; }
54
set
55
...{
56
if (_Title != value) MarkChanged("Title");
57
_Title = value;
58
}
59
}
60
61
private string _Description;
62
/**//// <summary>
63
/// Gets or sets the Description or the object.
64
/// </summary>
65
public string Description
66
...{
67
get ...{ return _Description; }
68
set
69
...{
70
if (_Description != value) MarkChanged("Description");
71
_Description = value;
72
}
73
}
74
75
/**//// <summary>
76
/// Returns a category based on the specified id.
77
/// </summary>
78
public static Category GetCategory(Guid id)
79
...{
80
foreach (Category category in Categories)
81
...{
82
if (category.Id == id)
83
return category;
84
}
85
86
return null;
87
}
88
89
private static object _SyncRoot = new object();
90
private static List<Category> _Categories;
91
/**//// <summary>
92
/// Gets an unsorted list of all Categories.
93
/// </summary>
94
public static List<Category> Categories
95
...{
96
get
97
...{
98
if (_Categories == null)
99
...{
100
lock (_SyncRoot)
101
...{
102
if (_Categories == null)
103
...{
104
_Categories = BlogService.FillCategories();
105
_Categories.Sort();
106
}
107
}
108
}
109
110
return _Categories;
111
}
112
}
113
114
#endregion
115
116
Base overrides#region Base overrides
117
118
/**//// <summary>
119
/// Reinforces the business rules by adding additional rules to the
120
/// broken rules collection.
121
/// </summary>
122
protected override void ValidationRules()
123
...{
124
AddRule("Title", "Title must be set", string.IsNullOrEmpty(Title));
125
}
126
127
/**//// <summary>
128
/// Retrieves the object from the data store and populates it.
129
/// </summary>
130
/// <param name="id">The unique identifier of the object.</param>
131
/// <returns>
132
/// True if the object exists and is being populated successfully
133
/// </returns>
134
protected override Category DataSelect(Guid id)
135
...{
136
return BlogService.SelectCategory(id);
137
}
138
139
/**//// <summary>
140
/// Updates the object in its data store.
141
/// </summary>
142
protected override void DataUpdate()
143
...{
144
if (IsChanged)
145
BlogService.UpdateCategory(this);
146
}
147
148
/**//// <summary>
149
/// Inserts a new object to the data store.
150
/// </summary>
151
protected override void DataInsert()
152
...{
153
if (IsNew)
154
BlogService.InsertCategory(this);
155
}
156
157
/**//// <summary>
158
/// Deletes the object from the data store.
159
/// </summary>
160
protected override void DataDelete()
161
...{
162
if (IsDeleted)
163
BlogService.DeleteCategory(this);
164
if (Categories.Contains(this))
165
Categories.Remove(this);
166
}
167
168
/**//// <summary>
169
/// Saves the object to the database.
170
/// </summary>
171
//public override void Save()
172
// {
173
// if (this.IsDeleted)
174
// {
175
// BusinessBase<Category, Guid>.OnSaving(this, SaveAction.Delete);
176
// BlogService.DeleteCategory(this);
177
// BusinessBase<Category, Guid>.OnSaved(this, SaveAction.Delete);
178
// }
179
180
// if (this.IsDirty && !this.IsDeleted && !this.IsNew)
181
// {
182
// BusinessBase<Category, Guid>.OnSaving(this, SaveAction.Update);
183
// BlogService.UpdateCategory(this);
184
// BusinessBase<Category, Guid>.OnSaved(this, SaveAction.Update);
185
// }
186
187
// if (this.IsNew)
188
// {
189
// BusinessBase<Category, Guid>.OnSaving(this, SaveAction.Insert);
190
// BlogService.InsertCategory(this);
191
// BusinessBase<Category, Guid>.OnSaved(this, SaveAction.Insert);
192
// }
193
// }
194
195
/**//// <summary>
196
/// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
197
/// </summary>
198
/// <returns>
199
/// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
200
/// </returns>
201
public override string ToString()
202
...{
203
return Title;
204
}
205
206
#endregion
207
208
IComparable Members#region IComparable<Category> Members
209
210
/**//// <summary>
211
/// Compares the current object with another object of the same type.
212
/// </summary>
213
/// <param name="other">An object to compare with this object.</param>
214
/// <returns>
215
/// A 32-bit signed integer that indicates the relative order of the objects being compared.
216
/// The return value has the following meanings: Value Meaning Less than zero This object is
217
/// less than the other parameter.Zero This object is equal to other. Greater than zero This object is greater than other.
218
/// </returns>
219
public int CompareTo(Category other)
220
...{
221
return this.Title.CompareTo(other.Title);
222
}
223
224
#endregion
225
}
226
}
227