Skip to content

Commit 6687431

Browse files
committed
Merge branch 'feature/StaticTypeLookupService-helpers' into develop
Introduced the `AddOrReplace()` (cd92791) and `TryAdd()` (027ae25) methods—both `protected`—to `StaticTypeLookupService` to simplify how derived implementations handled checking for and existing registrations. Implemented `TryAdd()` on all derived classes that were part of this solution (68492d2).
2 parents b00cfad + 68492d2 commit 6687431

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-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
}

OnTopic/StaticTypeLookupService.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.Reflection;
99
using OnTopic.Internal.Collections;
10+
using OnTopic.Internal.Diagnostics;
1011

1112
namespace OnTopic {
1213

@@ -94,6 +95,37 @@ public StaticTypeLookupService(
9495
/// </summary>
9596
protected void Add(Type type) => _typeCollection.Add(type);
9697

98+
/*==========================================================================================================================
99+
| METHOD: TRY ADD
100+
\-------------------------------------------------------------------------------------------------------------------------*/
101+
/// <summary>
102+
/// Determines if a <see cref="Type"/> with the given <see cref="MemberInfo.Name"/> exists. If it does, returns <c>false
103+
/// </c>; otherwise, adds the <see cref="Type"/> and returns <c>true</c>.
104+
/// </summary>
105+
protected bool TryAdd(Type type) {
106+
Contract.Requires(type, nameof(type));
107+
if (Contains(type.Name)) {
108+
return false;
109+
}
110+
Add(type);
111+
return true;
112+
}
113+
114+
/*==========================================================================================================================
115+
| METHOD: ADD OR REPLACE
116+
\-------------------------------------------------------------------------------------------------------------------------*/
117+
/// <summary>
118+
/// Determines if a <see cref="Type"/> with the given <see cref="MemberInfo.Name"/> exists. If it does, it is replaced.
119+
/// Otherwise, it is added.
120+
/// </summary>
121+
protected void AddOrReplace(Type type) {
122+
Contract.Requires(type, nameof(type));
123+
if (_typeCollection.Contains(type.Name)) {
124+
Remove(type.Name);
125+
}
126+
Add(type);
127+
}
128+
97129
/*==========================================================================================================================
98130
| METHOD: CONTAINS
99131
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)