Skip to content

Commit e257d09

Browse files
committed
Established Topic(string, string, Topic, int) constructor
Previously, `Topic` had a default constructor, which the `TopicFactory` used to create new instances. Updated the constructor to require, at minimum, `key` and `contentType` and, optionally, `parent`, and `id`. This ensures there isn't a means of creating an "empty" (invalid) `Topic`. Updated derived classes and `TopicFactory` to honor this (which greatly simplifies the latter).
1 parent 8ffddca commit e257d09

7 files changed

Lines changed: 155 additions & 57 deletions

File tree

Ignia.Topics.Web/Migrations/TopicsSetup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,15 @@ public static Topic SetTopic(Topic parentTopic, string key, string contentType)
254254
| Create a strongly-typed ContentType object if the contentType key is set to "ContentType"
255255
\---------------------------------------------------------------------------------------------------------------------*/
256256
if (contentType.Equals("ContentType")) {
257-
topic = new ContentTypeDescriptor();
257+
topic = TopicFactory.Create(key, "ContentTypeDescriptor");
258258
}
259259

260260
/*----------------------------------------------------------------------------------------------------------------------
261261
| Create a strongly-typed AttributeDescriptor object if the contentType key is set to either "Attribute" or
262262
| "AttributeDescriptor".
263263
\---------------------------------------------------------------------------------------------------------------------*/
264264
else if (contentType.Equals("Attribute") || contentType.Equals("AttributeDescriptor")) {
265-
topic = new AttributeDescriptor();
265+
topic = TopicFactory.Create(key, "AttributeDescriptor");
266266
}
267267

268268
/*----------------------------------------------------------------------------------------------------------------------

Ignia.Topics/Attribute.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,26 @@ public class Attribute : AttributeDescriptor {
2929
| CONSTRUCTOR
3030
\-------------------------------------------------------------------------------------------------------------------------*/
3131
/// <summary>
32-
/// Initializes a new instance of the <see cref="Attribute"/> class.
32+
/// Initializes a new instance of a <see cref="Attribute"/> class with a <see cref="Topic.Key"/>, <see
33+
/// cref="Topic.ContentType"/>, and, optionally, <see cref="Topic.Parent"/>, <see cref="Topic.Id"/>.
3334
/// </summary>
34-
public Attribute() : base() { }
35+
/// <remarks>
36+
/// By default, when creating new attributes, the <see cref="AttributeValue"/>s for both <see cref="Topic.Key"/> and <see
37+
/// cref="Topic.ContentType"/> will be set to <see cref="AttributeValue.IsDirty"/>, which is required in order to
38+
/// correctly save new topics to the database. When the <paramref name="id"/> parameter is set, however, the <see
39+
/// cref="AttributeValue.IsDirty"/> property is set to <c>false</c> on <see cref="Topic.Key"/> as well as on <see
40+
/// cref="Topic.ContentType"/>, since it is assumed these are being set to the same values currently used in the
41+
/// persistance store.
42+
/// </remarks>
43+
/// <param name="key">A string representing the key for the new topic instance.</param>
44+
/// <param name="contentType">A string representing the key of the target content type.</param>
45+
/// <param name="id">The unique identifier assigned by the data store for an existing topic.</param>
46+
/// <param name="parent">Optional topic to set as the new topic's parent.</param>
47+
/// <exception cref="ArgumentException">
48+
/// Thrown when the class representing the content type is found, but doesn't derive from <see cref="Topic"/>.
49+
/// </exception>
50+
/// <returns>A strongly-typed instance of the <see cref="Topic"/> class based on the target content type.</returns>
51+
public Attribute(string key, string contentType, Topic parent, int id = -1) : base(key, contentType, parent, id) { }
3552

3653
} //Class
3754

Ignia.Topics/AttributeDescriptor.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,36 @@ public class AttributeDescriptor : Topic {
5050
| CONSTRUCTOR
5151
\-------------------------------------------------------------------------------------------------------------------------*/
5252
/// <summary>
53-
/// Initializes a new instance of the <see cref="AttributeDescriptor"/> class.
53+
/// Initializes a new instance of a <see cref="AttributeDescriptor"/> class with a <see cref="Topic.Key"/>, <see
54+
/// cref="Topic.ContentType"/>, and, optionally, <see cref="Topic.Parent"/>, <see cref="Topic.Id"/>.
5455
/// </summary>
55-
public AttributeDescriptor() : base() { }
56+
/// <remarks>
57+
/// By default, when creating new attributes, the <see cref="AttributeValue"/>s for both <see cref="Topic.Key"/> and <see
58+
/// cref="Topic.ContentType"/> will be set to <see cref="AttributeValue.IsDirty"/>, which is required in order to
59+
/// correctly save new topics to the database. When the <paramref name="id"/> parameter is set, however, the <see
60+
/// cref="AttributeValue.IsDirty"/> property is set to <c>false</c> on <see cref="Topic.Key"/> as well as on <see
61+
/// cref="Topic.ContentType"/>, since it is assumed these are being set to the same values currently used in the
62+
/// persistance store.
63+
/// </remarks>
64+
/// <param name="key">A string representing the key for the new topic instance.</param>
65+
/// <param name="contentType">A string representing the key of the target content type.</param>
66+
/// <param name="id">The unique identifier assigned by the data store for an existing topic.</param>
67+
/// <param name="parent">Optional topic to set as the new topic's parent.</param>
68+
/// <exception cref="ArgumentException">
69+
/// Thrown when the class representing the content type is found, but doesn't derive from <see cref="Topic"/>.
70+
/// </exception>
71+
/// <returns>A strongly-typed instance of the <see cref="Topic"/> class based on the target content type.</returns>
72+
public AttributeDescriptor(
73+
string key,
74+
string contentType,
75+
Topic parent,
76+
int id = -1
77+
) : base(
78+
key,
79+
contentType,
80+
parent,
81+
id
82+
) { }
5683

5784
/*==========================================================================================================================
5885
| PROPERTY: TYPE

Ignia.Topics/ContentType.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,26 @@ public class ContentType : ContentTypeDescriptor {
2828
| CONSTRUCTOR
2929
\-------------------------------------------------------------------------------------------------------------------------*/
3030
/// <summary>
31-
/// Initializes a new instance of the <see cref="ContentType"/> class.
31+
/// Initializes a new instance of a <see cref="ContentType"/> class with a <see cref="Topic.Key"/>, <see
32+
/// cref="Topic.ContentType"/>, and, optionally, <see cref="Topic.Parent"/>, <see cref="Topic.Id"/>.
3233
/// </summary>
33-
public ContentType() : base() { }
34+
/// <remarks>
35+
/// By default, when creating new attributes, the <see cref="AttributeValue"/>s for both <see cref="Topic.Key"/> and <see
36+
/// cref="Topic.ContentType"/> will be set to <see cref="AttributeValue.IsDirty"/>, which is required in order to
37+
/// correctly save new topics to the database. When the <paramref name="id"/> parameter is set, however, the <see
38+
/// cref="AttributeValue.IsDirty"/> property is set to <c>false</c> on <see cref="Topic.Key"/> as well as on <see
39+
/// cref="Topic.ContentType"/>, since it is assumed these are being set to the same values currently used in the
40+
/// persistance store.
41+
/// </remarks>
42+
/// <param name="key">A string representing the key for the new topic instance.</param>
43+
/// <param name="contentType">A string representing the key of the target content type.</param>
44+
/// <param name="id">The unique identifier assigned by the data store for an existing topic.</param>
45+
/// <param name="parent">Optional topic to set as the new topic's parent.</param>
46+
/// <exception cref="ArgumentException">
47+
/// Thrown when the class representing the content type is found, but doesn't derive from <see cref="Topic"/>.
48+
/// </exception>
49+
/// <returns>A strongly-typed instance of the <see cref="Topic"/> class based on the target content type.</returns>
50+
public ContentType(string key, string contentType, Topic parent, int id = -1) : base(key, contentType, parent, id) { }
3451

3552
} //Class
3653

Ignia.Topics/ContentTypeDescriptor.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,36 @@ public class ContentTypeDescriptor : Topic {
4444
| CONSTRUCTOR
4545
\-------------------------------------------------------------------------------------------------------------------------*/
4646
/// <summary>
47-
/// Initializes a new instance of the <see cref="ContentTypeDescriptor"/> class.
47+
/// Initializes a new instance of a <see cref="ContentTypeDescriptor"/> class with a <see cref="Topic.Key"/>, <see
48+
/// cref="Topic.ContentType"/>, and, optionally, <see cref="Topic.Parent"/>, <see cref="Topic.Id"/>.
4849
/// </summary>
49-
public ContentTypeDescriptor() : base() { }
50+
/// <remarks>
51+
/// By default, when creating new attributes, the <see cref="AttributeValue"/>s for both <see cref="Topic.Key"/> and <see
52+
/// cref="Topic.ContentType"/> will be set to <see cref="AttributeValue.IsDirty"/>, which is required in order to
53+
/// correctly save new topics to the database. When the <paramref name="id"/> parameter is set, however, the <see
54+
/// cref="AttributeValue.IsDirty"/> property is set to <c>false</c> on <see cref="Topic.Key"/> as well as on <see
55+
/// cref="Topic.ContentType"/>, since it is assumed these are being set to the same values currently used in the
56+
/// persistance store.
57+
/// </remarks>
58+
/// <param name="key">A string representing the key for the new topic instance.</param>
59+
/// <param name="contentType">A string representing the key of the target content type.</param>
60+
/// <param name="id">The unique identifier assigned by the data store for an existing topic.</param>
61+
/// <param name="parent">Optional topic to set as the new topic's parent.</param>
62+
/// <exception cref="ArgumentException">
63+
/// Thrown when the class representing the content type is found, but doesn't derive from <see cref="Topic"/>.
64+
/// </exception>
65+
/// <returns>A strongly-typed instance of the <see cref="Topic"/> class based on the target content type.</returns>
66+
public ContentTypeDescriptor(
67+
string key,
68+
string contentType,
69+
Topic parent,
70+
int id = -1
71+
) : base(
72+
key,
73+
contentType,
74+
parent,
75+
id
76+
) { }
5077

5178
/*==========================================================================================================================
5279
| PROPERTY: DISABLE CHILD TOPICS

Ignia.Topics/Topic.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,50 @@ public class Topic {
3333

3434
/*==========================================================================================================================
3535
| CONSTRUCTOR
36-
>-----------=---------------------------------------------------------------------------------------------------------------
37-
| ### NOTE JJC082715: The empty constructor is a prerequisite of the factory method, which relies on Activator to create a
38-
| new instance of the object.
39-
\-----------=-------------------------------------------------------------------------------------------------------------*/
36+
\-------------------------------------------------------------------------------------------------------------------------*/
4037
/// <summary>
41-
/// Initializes a new instance of the <see cref="Topic"/> class.
38+
/// Initializes a new instance of a <see cref="Topic"/> class with a <see cref="Key"/>, <see cref="ContentType"/>, and,
39+
/// optionally, <see cref="Parent"/>, <see cref="Id"/>.
4240
/// </summary>
43-
public Topic() {
41+
/// <remarks>
42+
/// By default, when creating new attributes, the <see cref="AttributeValue"/>s for both <see cref="Key"/> and <see
43+
/// cref="ContentType"/> will be set to <see cref="AttributeValue.IsDirty"/>, which is required in order to correctly save
44+
/// new topics to the database. When the <paramref name="id"/> parameter is set, however, the <see
45+
/// cref="AttributeValue.IsDirty"/> property is set to <c>false</c>on <see cref="Key"/> and <see cref="ContentType"/>, as
46+
/// it is assumed these are being set to the same values currently used in the persistance store.
47+
/// </remarks>
48+
/// <param name="key">A string representing the key for the new topic instance.</param>
49+
/// <param name="contentType">A string representing the key of the target content type.</param>
50+
/// <param name="id">The unique identifier assigned by the data store for an existing topic.</param>
51+
/// <param name="parent">Optional topic to set as the new topic's parent.</param>
52+
/// <exception cref="ArgumentException">
53+
/// Thrown when the class representing the content type is found, but doesn't derive from <see cref="Topic"/>.
54+
/// </exception>
55+
/// <returns>A strongly-typed instance of the <see cref="Topic"/> class based on the target content type.</returns>
56+
public Topic(string key, string contentType, Topic parent, int id = -1) {
57+
58+
/*----------------------------------------------------------------------------------------------------------------------
59+
| Set core properties
60+
\---------------------------------------------------------------------------------------------------------------------*/
61+
Key = key;
62+
ContentType = contentType;
63+
Parent = parent;
64+
65+
/*----------------------------------------------------------------------------------------------------------------------
66+
| If ID is set, ensure attributes are not marked as IsDirty
67+
\---------------------------------------------------------------------------------------------------------------------*/
68+
if (id >= 0) {
69+
Id = id;
70+
Attributes.SetValue("Key", key, false, false);
71+
Attributes.SetValue("ContentType", contentType, false, false);
72+
if (parent != null) {
73+
Attributes.SetValue("ParentId", parent.Id.ToString(), false, false);
74+
}
75+
}
76+
77+
/*----------------------------------------------------------------------------------------------------------------------
78+
| Set relationships
79+
\---------------------------------------------------------------------------------------------------------------------*/
4480
Children = new TopicCollection(this);
4581
Attributes = new AttributeValueCollection(this);
4682
IncomingRelationships = new RelatedTopicCollection(this, true);

0 commit comments

Comments
 (0)