Skip to content

Commit 1678523

Browse files
committed
Introduced new Topic.IsSaved convenience property
If the `Id >= 0` then the `Topic` has been saved to the underlying `ITopicRepository`. By having a simple convenience property to evaluate this, the semantics are more intuitive, and it may reduce the need to centralized variables in some places.
1 parent b778718 commit 1678523

5 files changed

Lines changed: 23 additions & 12 deletions

File tree

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ SqlDateTime version
337337
/*------------------------------------------------------------------------------------------------------------------------
338338
| Define variables
339339
\-----------------------------------------------------------------------------------------------------------------------*/
340-
var isNew = topic.Id == -1;
341340
var areReferencesResolved = true;
342341
var areRelationshipsDirty = topic.Relationships.IsDirty();
343342
var areAttributesDirty = topic.Attributes.IsDirty(excludeLastModified: !areRelationshipsDirty);
@@ -418,7 +417,7 @@ SqlDateTime version
418417
/*------------------------------------------------------------------------------------------------------------------------
419418
| Establish database connection
420419
\-----------------------------------------------------------------------------------------------------------------------*/
421-
var procedureName = isNew? "CreateTopic" : "UpdateTopic";
420+
var procedureName = !topic.IsSaved? "CreateTopic" : "UpdateTopic";
422421
var command = new SqlCommand(procedureName, connection) {
423422
CommandType = CommandType.StoredProcedure
424423
};
@@ -438,7 +437,7 @@ SqlDateTime version
438437
/*------------------------------------------------------------------------------------------------------------------------
439438
| Establish query parameters
440439
\-----------------------------------------------------------------------------------------------------------------------*/
441-
if (!isNew) {
440+
if (topic.IsSaved) {
442441
command.AddParameter("TopicID", topic.Id);
443442
command.AddParameter("DeleteRelationships", areReferencesResolved && areRelationshipsDirty);
444443
}
@@ -460,7 +459,7 @@ SqlDateTime version
460459
topic.Id = command.GetReturnCode();
461460

462461
Contract.Assume<InvalidOperationException>(
463-
topic.Id > 0,
462+
topic.IsSaved,
464463
"The call to the CreateTopic stored procedure did not return the expected 'Id' parameter."
465464
);
466465

@@ -657,7 +656,7 @@ private static void PersistRelations(Topic topic, SqlConnection connection) {
657656
var targetIds = new TopicListDataTable();
658657
var relatedTopics = topic.Relationships.GetTopics(key);
659658
var topicId = topic.Id.ToString(CultureInfo.InvariantCulture);
660-
var savedTopics = relatedTopics.Where(t => t.Id > 0).Select<Topic, int>(m => m.Id);
659+
var savedTopics = relatedTopics.Where(t => t.IsSaved).Select<Topic, int>(m => m.Id);
661660

662661
command = new SqlCommand("UpdateRelationships", connection) {
663662
CommandType = CommandType.StoredProcedure

OnTopic.TestDoubles/StubTopicRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public override int Save(Topic topic, bool isRecursive = false, bool isDraft = f
115115
/*------------------------------------------------------------------------------------------------------------------------
116116
| Recurse through children
117117
\-----------------------------------------------------------------------------------------------------------------------*/
118-
if (topic.Id < 0) {
118+
if (!topic.IsSaved) {
119119
topic.Id = _identity++;
120120
}
121121

OnTopic/Mapping/TopicMappingService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private async Task<object> MapAsync(
193193
if (cache.TryGetValue(topic.Id, out var dto)) {
194194
return dto;
195195
}
196-
else if (topic.Id > 0) {
196+
else if (topic.IsSaved) {
197197
cache.GetOrAdd(topic.Id, target);
198198
}
199199

OnTopic/Repositories/TopicRepositoryBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public virtual int Save([ValidatedNotNull, NotNull]Topic topic, bool isRecursive
333333
/*----------------------------------------------------------------------------------------------------------------------
334334
| Perform reordering and/or move
335335
\---------------------------------------------------------------------------------------------------------------------*/
336-
if (topic.Parent != null && topic.Attributes.IsDirty("ParentId") && topic.Id >= 0) {
336+
if (topic.Parent != null && topic.Attributes.IsDirty("ParentId") && topic.IsSaved) {
337337
var topicIndex = topic.Parent.Children.IndexOf(topic);
338338
if (topicIndex > 0) {
339339
Move(topic, topic.Parent, topic.Parent.Children[topicIndex - 1]);
@@ -344,10 +344,10 @@ public virtual int Save([ValidatedNotNull, NotNull]Topic topic, bool isRecursive
344344
}
345345

346346
/*------------------------------------------------------------------------------------------------------------------------
347-
| If new content type, remove from cache
347+
| If new content type, add to cache
348348
\-----------------------------------------------------------------------------------------------------------------------*/
349349
if (
350-
topic.Id < 0 &&
350+
!topic.IsSaved &&
351351
topic is ContentTypeDescriptor &&
352352
_contentTypeDescriptors != null &&
353353
!_contentTypeDescriptors.Contains(topic.Key)
@@ -358,7 +358,7 @@ topic is ContentTypeDescriptor &&
358358
/*------------------------------------------------------------------------------------------------------------------------
359359
| If new attribute, refresh cache
360360
\-----------------------------------------------------------------------------------------------------------------------*/
361-
if (topic.Id < 0 && IsAttributeDescriptor(topic)) {
361+
if (!topic.IsSaved && IsAttributeDescriptor(topic)) {
362362
ResetAttributeDescriptors(topic);
363363
}
364364

@@ -606,7 +606,7 @@ protected IEnumerable<AttributeDescriptor> GetUnmatchedAttributes(Topic topic) {
606606
foreach (var attribute in contentType.AttributeDescriptors) {
607607

608608
// Ignore unsaved topics
609-
if (topic.Id == -1) {
609+
if (!topic.IsSaved) {
610610
continue;
611611
}
612612

OnTopic/Topic.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ public string? View {
284284
}
285285
}
286286

287+
/*==========================================================================================================================
288+
| PROPERTY: IS SAVED
289+
\-------------------------------------------------------------------------------------------------------------------------*/
290+
/// <summary>
291+
/// Determines whether or not the current <see cref="Topic"/> has been saved to an underlying <see
292+
/// cref="Repositories.ITopicRepository"/>.
293+
/// </summary>
294+
/// <value>
295+
/// <c>true</c> if it has been saved; otherwise, <c>false</c>.
296+
/// </value>
297+
public bool IsSaved => Id >= 0;
298+
287299
/*==========================================================================================================================
288300
| PROPERTY: IS HIDDEN
289301
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)