Skip to content

Commit 4c4134b

Browse files
committed
Merge branch 'improvement/TopicRepository-IsNew-checks' into develop
Improved the `TopicRepository` base class to be more aware of `IsNew` conditions. In the case of loading a version of a new topic (8aacc5a) or saving a topic whose parent is new (4da0d43), an exception is thrown, as this is a completely unexpected scenario with no option for succeeding. The latter is validated via a unit test (854299a). In the case of `Move()` and `Delete()`, we instead just bypass their underlying `MoveTopic()` (bf1c584) and `DeleteTopic()` (7b46514) methods, which concrete implementations are responsible for implementing, thus allowing the basic validation and in-memory logic to be performed. This allows e.g. an `IsNew` topic to be deleted in memory, even though it obviously can't be deleted from the database. This satisfies the conditions of Issue #77.
2 parents 646eb92 + 7b46514 commit 4c4134b

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

OnTopic.Tests/TopicRepositoryBaseTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,9 @@ public void Move_ContentTypeDescriptor_UpdatesContentTypeCache() {
910910
[Fact]
911911
public void Save_AttributeDescriptor_UpdatesContentType() {
912912

913-
var contentType = new ContentTypeDescriptor("Parent", "ContentTypeDescriptor");
914-
var attributeList = new Topic("Attributes", "List", contentType);
915-
var childContentType = new ContentTypeDescriptor("Child", "ContentTypeDescriptor", contentType);
913+
var contentType = new ContentTypeDescriptor("Parent", "ContentTypeDescriptor", null, 1);
914+
var attributeList = new Topic("Attributes", "List", contentType, 2);
915+
var childContentType = new ContentTypeDescriptor("Child", "ContentTypeDescriptor", contentType, 3);
916916

917917
Contract.Assume(childContentType);
918918

@@ -924,7 +924,7 @@ public void Save_AttributeDescriptor_UpdatesContentType() {
924924

925925
_topicRepository.Save(newAttribute);
926926

927-
Assert.True(childContentType.AttributeDescriptors.Count > attributeCount);
927+
Assert.Equal<int>(attributeCount+1, childContentType.AttributeDescriptors.Count);
928928

929929
}
930930

OnTopic/Repositories/TopicRepository.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ protected ContentTypeDescriptorCollection SetContentTypeDescriptors(ContentTypeD
223223
/// <inheritdoc />
224224
public override Topic? Load(Topic topic, DateTime version) {
225225
Contract.Requires(topic, nameof(topic));
226+
Contract.Requires<ArgumentException>(
227+
!topic.IsNew,
228+
$"The version '{version}' of '{topic.GetUniqueKey()}' cannot be loaded. Topics must be saved in order to load " +
229+
$"previous versions."
230+
);
226231
return Load(topic.Id, version, topic);
227232
}
228233

@@ -260,6 +265,15 @@ public override void Rollback([ValidatedNotNull]Topic topic, DateTime version) {
260265
/// <inheritdoc />
261266
public override sealed void Save([ValidatedNotNull] Topic topic, bool isRecursive = false) {
262267

268+
/*------------------------------------------------------------------------------------------------------------------------
269+
| Establish parameters
270+
\-----------------------------------------------------------------------------------------------------------------------*/
271+
Contract.Requires(topic, nameof(topic));
272+
Contract.Requires<ArgumentException>(
273+
topic.Parent is null || !topic.Parent.IsNew,
274+
$"The parent of '{topic.GetUniqueKey()}' is not saved. Topics can only be saved once their parent is saved."
275+
);
276+
263277
/*------------------------------------------------------------------------------------------------------------------------
264278
| Establish dependencies
265279
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -501,7 +515,9 @@ topic.Parent is not null &&
501515
/*------------------------------------------------------------------------------------------------------------------------
502516
| Execute core implementation
503517
\-----------------------------------------------------------------------------------------------------------------------*/
504-
MoveTopic(topic, target, sibling);
518+
if (!topic.IsNew && !target.IsNew && !sibling.IsNew) {
519+
MoveTopic(topic, target, sibling);
520+
}
505521

506522
/*------------------------------------------------------------------------------------------------------------------------
507523
| Perform base logic
@@ -595,7 +611,9 @@ public override sealed void Delete([ValidatedNotNull]Topic topic, bool isRecursi
595611
/*------------------------------------------------------------------------------------------------------------------------
596612
| Execute core implementation
597613
\-----------------------------------------------------------------------------------------------------------------------*/
598-
DeleteTopic(topic);
614+
if (!topic.IsNew) {
615+
DeleteTopic(topic);
616+
}
599617

600618
/*------------------------------------------------------------------------------------------------------------------------
601619
| Remove from parent

0 commit comments

Comments
 (0)