Skip to content

Commit a9ea634

Browse files
committed
Update GetContentTypeDescriptors() cache on Save(), Delete()
While many updates to the configuration metadata are made available in real time, some—such as adding or deleting `ContentTypeDescriptor`s—are not. This is because the `TopicRepositoryBase.GetContentTypeDecriptors()` method caches the list of content types so that it doesn't need to reload it from the persistence store each time its called. To mitigate this, we can dynamically update the `GetContentTypeDescriptors()` cache when we call `Save()` or `Delete()`. Specifically, if `Save()` is called with a `ContentTypeDescriptor` and that content type is new, then it will be added to the local `_contentTypeDescriptors` collection. Similarly, if `Delete()` is called with a `ContentTypeDescriptor` then that content type—along with all descendent content types—will be removed from the _contentTypeDescriptors` collection. This satisfies the requirements of the newly proposed feature enhancement: Update content type cache when adding or removing content types (#16).
1 parent 408d01a commit a9ea634

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

OnTopic.Tests/TopicRepositoryBaseTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,45 @@ public void GetContentTypeDescriptor_GetInvalidContentType_ReturnsNull() {
200200

201201
}
202202

203+
/*==========================================================================================================================
204+
| TEST: SAVE: CONTENT TYPE DESCRIPTOR: UPDATES CONTENT TYPE CACHE
205+
\-------------------------------------------------------------------------------------------------------------------------*/
206+
/// <summary>
207+
/// Loads the <see cref="TopicRepositoryBase.GetAttributes(Topic, Boolean?, Boolean?)"/>, then saves a new <see
208+
/// cref="ContentTypeDescriptor"/> via <see cref="TopicRepositoryBase.Save(Topic, Boolean, Boolean)"/>, and ensures that
209+
/// it is immediately reflected in the <see cref="TopicRepositoryBase"/> cache of <see cref="ContentTypeDescriptor"/>s.
210+
/// </summary>
211+
[TestMethod]
212+
public void Save_ContentTypeDescriptor_UpdatesContentTypeCache() {
213+
214+
var contentTypes = _topicRepository.GetContentTypeDescriptors();
215+
var topic = TopicFactory.Create("NewContentType", "ContentTypeDescriptor");
216+
217+
_topicRepository.Save(topic);
218+
219+
Assert.IsTrue(contentTypes.Contains(topic));
220+
221+
}
222+
223+
/*==========================================================================================================================
224+
| TEST: DELETE: CONTENT TYPE DESCRIPTOR: UPDATES CONTENT TYPE CACHE
225+
\-------------------------------------------------------------------------------------------------------------------------*/
226+
/// <summary>
227+
/// Loads the <see cref="TopicRepositoryBase.GetAttributes(Topic, Boolean?, Boolean?)"/>, then deletes one of the <see
228+
/// cref="ContentTypeDescriptor"/>s via <see cref="TopicRepositoryBase.Delete(Topic, Boolean)"/>, and ensures that it is
229+
/// immediately reflected in the <see cref="TopicRepositoryBase"/> cache of <see cref="ContentTypeDescriptor"/>s.
230+
/// </summary>
231+
[TestMethod]
232+
public void Delete_ContentTypeDescriptor_UpdatesContentTypeCache() {
233+
234+
var contentTypes = _topicRepository.GetContentTypeDescriptors();
235+
var contentType = contentTypes.Contains("Page")? contentTypes["Page"] : null;
236+
237+
_topicRepository.Delete(contentType);
238+
239+
Assert.IsFalse(contentTypes.Contains(contentType));
240+
241+
}
242+
203243
} //Class
204244
} //Namespace

OnTopic/Repositories/TopicRepositoryBase.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,18 @@ public virtual int Save([ValidatedNotNull, NotNull]Topic topic, bool isRecursive
315315
}
316316
}
317317

318+
/*------------------------------------------------------------------------------------------------------------------------
319+
| If new content type, remove from cache
320+
\-----------------------------------------------------------------------------------------------------------------------*/
321+
if (
322+
topic.Id < 0 &&
323+
topic is ContentTypeDescriptor &&
324+
_contentTypeDescriptors != null &&
325+
!_contentTypeDescriptors.Contains(topic)
326+
) {
327+
_contentTypeDescriptors.Add((ContentTypeDescriptor)topic);
328+
}
329+
318330
/*------------------------------------------------------------------------------------------------------------------------
319331
| Reset original key
320332
\-----------------------------------------------------------------------------------------------------------------------*/
@@ -403,6 +415,17 @@ public virtual void Delete([ValidatedNotNull, NotNull]Topic topic, bool isRecurs
403415
topic.Parent.Children.Remove(topic.Key);
404416
}
405417

418+
/*------------------------------------------------------------------------------------------------------------------------
419+
| If content type, remove from cache
420+
\-----------------------------------------------------------------------------------------------------------------------*/
421+
if (topic is ContentTypeDescriptor && _contentTypeDescriptors != null) {
422+
foreach (var contentTypeDescriptor in topic.FindAll(t => t is ContentTypeDescriptor).Cast<ContentTypeDescriptor>()) {
423+
if (_contentTypeDescriptors.Contains(contentTypeDescriptor)) {
424+
_contentTypeDescriptors.Remove(contentTypeDescriptor);
425+
}
426+
}
427+
}
428+
406429
}
407430

408431
/*==========================================================================================================================

0 commit comments

Comments
 (0)