Skip to content

Commit 68492d2

Browse files
committed
Implemented new TryAdd() method
With the new `TryAdd()` method introduced (027ae25), we can now standardize how derived classes handle conditional adds, whereas previously there was a different approach in each of the derived classes. This improves consistency and readability.
1 parent 027ae25 commit 68492d2

File tree

3 files changed

+33
-44
lines changed

3 files changed

+33
-44
lines changed

OnTopic.ViewModels/TopicViewModelLookupService.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,20 @@ public TopicViewModelLookupService(IEnumerable<Type>? types = null, Type? defaul
3535
/*------------------------------------------------------------------------------------------------------------------------
3636
| Ensure local view models are accounted for
3737
\-----------------------------------------------------------------------------------------------------------------------*/
38-
AddIfMissing(typeof(ContentItemTopicViewModel));
39-
AddIfMissing(typeof(ContentListTopicViewModel));
40-
AddIfMissing(typeof(IndexTopicViewModel));
41-
AddIfMissing(typeof(ItemTopicViewModel));
42-
AddIfMissing(typeof(ListTopicViewModel));
43-
AddIfMissing(typeof(LookupListItemTopicViewModel));
44-
AddIfMissing(typeof(NavigationTopicViewModel));
45-
AddIfMissing(typeof(PageGroupTopicViewModel));
46-
AddIfMissing(typeof(PageTopicViewModel));
47-
AddIfMissing(typeof(SectionTopicViewModel));
48-
AddIfMissing(typeof(SlideTopicViewModel));
49-
AddIfMissing(typeof(SlideshowTopicViewModel));
50-
AddIfMissing(typeof(TopicViewModel));
51-
AddIfMissing(typeof(VideoTopicViewModel));
52-
53-
/*------------------------------------------------------------------------------------------------------------------------
54-
| Function: Add If Missing
55-
\-----------------------------------------------------------------------------------------------------------------------*/
56-
void AddIfMissing(Type type) {
57-
if (!Contains(type.Name)) {
58-
Add(type);
59-
}
60-
}
38+
TryAdd(typeof(ContentItemTopicViewModel));
39+
TryAdd(typeof(ContentListTopicViewModel));
40+
TryAdd(typeof(IndexTopicViewModel));
41+
TryAdd(typeof(ItemTopicViewModel));
42+
TryAdd(typeof(ListTopicViewModel));
43+
TryAdd(typeof(LookupListItemTopicViewModel));
44+
TryAdd(typeof(NavigationTopicViewModel));
45+
TryAdd(typeof(PageGroupTopicViewModel));
46+
TryAdd(typeof(PageTopicViewModel));
47+
TryAdd(typeof(SectionTopicViewModel));
48+
TryAdd(typeof(SlideTopicViewModel));
49+
TryAdd(typeof(SlideshowTopicViewModel));
50+
TryAdd(typeof(TopicViewModel));
51+
TryAdd(typeof(VideoTopicViewModel));
6152

6253
}
6354

OnTopic/DefaultTopicLookupService.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,24 @@ public DefaultTopicLookupService(IEnumerable<Type>? types = null, Type? defaultT
3838
/*------------------------------------------------------------------------------------------------------------------------
3939
| Ensure editor types are accounted for
4040
\-----------------------------------------------------------------------------------------------------------------------*/
41-
if (!Contains(nameof(ContentTypeDescriptor))) Add(typeof(ContentTypeDescriptor));
42-
if (!Contains(nameof(AttributeDescriptor))) Add(typeof(AttributeDescriptor));
43-
if (!Contains(nameof(BooleanAttribute))) Add(typeof(BooleanAttribute));
44-
if (!Contains(nameof(DateTimeAttribute))) Add(typeof(DateTimeAttribute));
45-
if (!Contains(nameof(FileListAttribute))) Add(typeof(FileListAttribute));
46-
if (!Contains(nameof(FilePathAttribute))) Add(typeof(FilePathAttribute));
47-
if (!Contains(nameof(HtmlAttribute))) Add(typeof(HtmlAttribute));
48-
if (!Contains(nameof(LastModifiedAttribute))) Add(typeof(LastModifiedAttribute));
49-
if (!Contains(nameof(LastModifiedByAttribute))) Add(typeof(LastModifiedByAttribute));
50-
if (!Contains(nameof(NestedTopicListAttribute))) Add(typeof(NestedTopicListAttribute));
51-
if (!Contains(nameof(NumberAttribute))) Add(typeof(NumberAttribute));
52-
if (!Contains(nameof(QueryableTopicListAttribute))) Add(typeof(QueryableTopicListAttribute));
53-
if (!Contains(nameof(RelationshipAttribute))) Add(typeof(RelationshipAttribute));
54-
if (!Contains(nameof(TextAreaAttribute))) Add(typeof(TextAreaAttribute));
55-
if (!Contains(nameof(TextAttribute))) Add(typeof(TextAttribute));
56-
if (!Contains(nameof(TokenizedTopicListAttribute))) Add(typeof(TokenizedTopicListAttribute));
57-
if (!Contains(nameof(TopicListAttribute))) Add(typeof(TopicListAttribute));
58-
if (!Contains(nameof(TopicReferenceAttribute))) Add(typeof(TopicReferenceAttribute));
41+
TryAdd(typeof(ContentTypeDescriptor));
42+
TryAdd(typeof(AttributeDescriptor));
43+
TryAdd(typeof(BooleanAttribute));
44+
TryAdd(typeof(DateTimeAttribute));
45+
TryAdd(typeof(FileListAttribute));
46+
TryAdd(typeof(FilePathAttribute));
47+
TryAdd(typeof(HtmlAttribute));
48+
TryAdd(typeof(LastModifiedAttribute));
49+
TryAdd(typeof(LastModifiedByAttribute));
50+
TryAdd(typeof(NestedTopicListAttribute));
51+
TryAdd(typeof(NumberAttribute));
52+
TryAdd(typeof(QueryableTopicListAttribute));
53+
TryAdd(typeof(RelationshipAttribute));
54+
TryAdd(typeof(TextAreaAttribute));
55+
TryAdd(typeof(TextAttribute));
56+
TryAdd(typeof(TokenizedTopicListAttribute));
57+
TryAdd(typeof(TopicListAttribute));
58+
TryAdd(typeof(TopicReferenceAttribute));
5959

6060
}
6161

OnTopic/Reflection/DynamicTypeLookupService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public DynamicTypeLookupService(Func<Type, bool> predicate, Type? defaultType =
4343
| Populate collection
4444
\-----------------------------------------------------------------------------------------------------------------------*/
4545
foreach (var type in matchedTypes) {
46-
if (!Contains(type)) {
47-
Add(type);
48-
}
46+
TryAdd(type);
4947
}
5048

5149
}

0 commit comments

Comments
 (0)