Skip to content

Commit afbfa47

Browse files
committed
Migrated GetContentTypes(ContentTypeDescriptor) to new Refresh() method
Migrated the `TopicRepositoryBase.GetContentTypes(ContentTypeDescriptor)` overload to utilize the newly introduced `ContentTypeDescriptorCollection.Refresh()` method (3e3930f). Since the `GetContentTypes()` overload was already migrated, this allows the removal of unintuitive state tracking via the `_contentTypeDescriptors` field initialization in order to avoid a circular loop (since each function previously called into the other). and since `Refresh()` now has the core functionality which this overload was previously dependent on, that can now be removed. Indeed, this greatly simplifies the logic for this overload. As part of this, the logic also changes so that the source topic graph is always utilized, assuming it can be identified. This is actually faster than trying to merge the two collections via the `Refresh()` method. But, more importantly, it also avoids some complications caused by versioning conflicts between different in-memory representations of `ContentTypeDescriptor`s (e.g., one in `CachedTopicRepository`, another in `SqlTopicRepository`). By replacing the entire collection, we avoid these types of problems, and help ensure that the cache is always using the same topic graph that the application is operating off of.
1 parent 946ddb9 commit afbfa47

1 file changed

Lines changed: 18 additions & 33 deletions

File tree

OnTopic/Repositories/TopicRepositoryBase.cs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -107,38 +107,25 @@ public virtual ContentTypeDescriptorCollection GetContentTypeDescriptors() {
107107
/// <returns></returns>
108108
protected virtual ContentTypeDescriptorCollection GetContentTypeDescriptors(ContentTypeDescriptor? contentTypeDescriptors) {
109109

110-
/*------------------------------------------------------------------------------------------------------------------------
111-
| Initialize the collection from the repository
112-
>-------------------------------------------------------------------------------------------------------------------------
113-
| ### NOTE JJC2020519: We want to centralize the shared logic from the public GetContentTypeDescriptors() while still
114-
| ensuring the cache is first initialized from the underlying data store if this method is called directly. But we don't
115-
| want to repeatedly call the underlying data store if it's empty, nor do we want to create a circular loop if this is
116-
| being called from GetContentTypeDescriptors(). This is handled by initializing the _contentTypeDescriptors cache in the
117-
| GetContentTypeDescriptors() method as a way of tracking the initialization state.
118-
\-----------------------------------------------------------------------------------------------------------------------*/
119-
if (_contentTypeDescriptors == null) {
120-
_contentTypeDescriptors = GetContentTypeDescriptors();
121-
}
122110

123111
/*------------------------------------------------------------------------------------------------------------------------
124-
| Validate parameters
112+
| Establish cache
113+
>-------------------------------------------------------------------------------------------------------------------------
114+
| Instead of attempting to merge the source collection with the cached collection, we'll just recreate the cached
115+
| collection based on the source collection. This is faster, and helps avoid potential versioning conflicts caused by
116+
| mixing objects from different topic graphs. Instead, we always assume that the source collection is the most accurate
117+
| and relevant.
125118
\-----------------------------------------------------------------------------------------------------------------------*/
126-
if (contentTypeDescriptors == null) {
127-
return _contentTypeDescriptors;
128-
}
119+
_contentTypeDescriptors.Refresh(contentTypeDescriptors);
129120

130121
/*------------------------------------------------------------------------------------------------------------------------
131-
| Add available Content Types to the collection
122+
| Validate cache
123+
>-------------------------------------------------------------------------------------------------------------------------
124+
| If the source cache collection is empty then we'll want to defer to the existing version�or retrieve it from the
125+
| persistence layer�via GetContentTypeDescriptors().
132126
\-----------------------------------------------------------------------------------------------------------------------*/
133-
foreach (var topic in contentTypeDescriptors.FindAllByAttribute("ContentType", "ContentType")) {
134-
// Ensure the Topic is used as the strongly-typed ContentType
135-
// Add ContentType Topic to collection if not already added
136-
if (
137-
topic is ContentTypeDescriptor contentTypeDescriptor &&
138-
!_contentTypeDescriptors.Contains(contentTypeDescriptor.Key)
139-
) {
140-
_contentTypeDescriptors.Add(contentTypeDescriptor);
141-
}
127+
if (_contentTypeDescriptors.Count == 0) {
128+
GetContentTypeDescriptors();
142129
}
143130

144131
/*------------------------------------------------------------------------------------------------------------------------
@@ -184,15 +171,13 @@ topic is ContentTypeDescriptor contentTypeDescriptor &&
184171
}
185172

186173
/*------------------------------------------------------------------------------------------------------------------------
187-
| Retrieve content type
174+
| Attempt to update content types from source graph
188175
\-----------------------------------------------------------------------------------------------------------------------*/
189-
if (
190-
sourceTopic.GetByUniqueKey("Root:Configuration:ContentTypes") is ContentTypeDescriptor sourceContentTypes &&
191-
!contentTypes.Contains(sourceContentTypes)
192-
) {
193-
contentTypes = GetContentTypeDescriptors(sourceContentTypes);
194-
}
176+
SetContentTypeDescriptors(sourceTopic);
195177

178+
/*------------------------------------------------------------------------------------------------------------------------
179+
| Retrieve content type
180+
\-----------------------------------------------------------------------------------------------------------------------*/
196181
return contentTypes.Contains(contentType)? contentTypes[contentType] : null;
197182

198183
}

0 commit comments

Comments
 (0)