Skip to content

Commit b5f2265

Browse files
committed
Consolidated the content type validation logic
Previously, the `contentType` (now `contentTypeDescriptor`) was defined in one block, but validated in a later block. These are now brought together. In addition, the `GetContentTypeDescriptors()` collection is confirmed to contain the content type before retrieving it. Otherwise, the validation will never fire, and instead a less-intuitive `IndexOutOfRangeException` will be thrown. It's worth noting that we no longer _need_ the `ContentTypeDescriptor` here. Instead, this is needed by the new `GetAttributes()` and `GetNullableAttributes()` methods on `TopicRepositoryBase` (see #6662f07, #2237a02), which call `GetContentTypes()` on their own. To reduce the number of times this needs to be looked up and validated, it may make more sense to just relay this to `GetAttributes()` and `GetNullableAttributes()` in the future; we'll need to consider that.
1 parent daa6705 commit b5f2265

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.Data.SqlClient;
1616
using OnTopic.Attributes;
1717
using OnTopic.Internal.Diagnostics;
18+
using OnTopic.Metadata;
1819
using OnTopic.Repositories;
1920

2021
namespace OnTopic.Data.Sql {
@@ -659,7 +660,14 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
659660
/*------------------------------------------------------------------------------------------------------------------------
660661
| Validate content type
661662
\-----------------------------------------------------------------------------------------------------------------------*/
662-
var contentType = GetContentTypeDescriptors()[topic.Attributes.GetValue("ContentType", "Page")?? "Page"];
663+
var contentTypes = GetContentTypeDescriptors();
664+
var contentType = topic.Attributes.GetValue("ContentType", "Page")?? "Page";
665+
var contentTypeDescriptor = contentTypes.GetTopic(contentType) as ContentTypeDescriptor;
666+
667+
Contract.Assume(
668+
contentTypeDescriptor,
669+
$"The Topics repository or database does not contain a ContentTypeDescriptor for the {contentType} content type."
670+
);
663671

664672
/*------------------------------------------------------------------------------------------------------------------------
665673
| Establish attribute containers with schema
@@ -678,11 +686,6 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
678686
}
679687
);
680688

681-
Contract.Assume(
682-
contentType,
683-
$"The Topics repository or database does not contain a ContentTypeDescriptor for the {contentType} content type."
684-
);
685-
686689
/*------------------------------------------------------------------------------------------------------------------------
687690
| Add indexed attributes that are dirty
688691
\-----------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)