Skip to content

Commit 9a7ef01

Browse files
committed
Introduced ResetPermittedContentTypes()
Similar to the previously introduced `ResetAttributeDescriptors()` (b0c5a5e), this exposes a new `internal` helper method, `ResetPermittedContentTypes()`, which is called from `TopicRepositoryBase.Save()` when a `ContentTypeDescriptor` is updated, assuming its relationships have been updated. In practice, this might be called rarely enough, and may be fast enough to assemble, that we could just do away with the cache entirely. That said, this is a familiar and easy pattern to apply alongside `ResetAttributeDescriptors()`, so we'll stick with this for now. Also created a corresponding unit test to validate this functionality.
1 parent b2f27e8 commit 9a7ef01

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

OnTopic.Tests/TopicRepositoryBaseTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,30 @@ public void Save_ContentTypeDescriptor_UpdatesContentTypeCache() {
380380

381381
}
382382

383+
/*==========================================================================================================================
384+
| TEST: SAVE: CONTENT TYPE DESCRIPTOR: UPDATES PERMITTED CONTENT TYPES
385+
\-------------------------------------------------------------------------------------------------------------------------*/
386+
/// <summary>
387+
/// Loads the <see cref="TopicRepositoryBase.GetContentTypeDescriptors()"/>, then saves an existing <see cref=
388+
/// "ContentTypeDescriptor"/> via <see cref="TopicRepositoryBase.Save(Topic, Boolean, Boolean)"/>, and ensures that
389+
/// it the <see cref="ContentTypeDescriptor.PermittedContentTypes"/> cache is updated.
390+
/// </summary>
391+
[TestMethod]
392+
public void Save_ContentTypeDescriptor_UpdatesPermittedContentTypes() {
393+
394+
var contentTypes = _topicRepository.GetContentTypeDescriptors();
395+
var pageContentType = contentTypes.GetTopic("Page");
396+
var lookupContentType = contentTypes.GetTopic("Lookup");
397+
var initialCount = pageContentType.PermittedContentTypes.Count;
398+
399+
pageContentType.Relationships.SetTopic("ContentTypes", lookupContentType);
400+
401+
_topicRepository.Save(pageContentType);
402+
403+
Assert.AreNotEqual<int>(initialCount, pageContentType.PermittedContentTypes.Count);
404+
405+
}
406+
383407
/*==========================================================================================================================
384408
| TEST: DELETE: CONTENT TYPE DESCRIPTOR: UPDATES CONTENT TYPE CACHE
385409
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic/Metadata/ContentTypeDescriptor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,21 @@ internal void ResetAttributeDescriptors() {
238238
}
239239
}
240240

241+
/*==========================================================================================================================
242+
| METHOD: RESET PERMITTED CONTENT TYPES
243+
\-------------------------------------------------------------------------------------------------------------------------*/
244+
/// <summary>
245+
/// Resets the list of <see cref="ContentTypeDescriptor"/>s stored in the <see cref="PermittedContentTypes"/> collection
246+
/// on this <see cref="ContentTypeDescriptor"/>.
247+
/// </summary>
248+
/// <remarks>
249+
/// Each <see cref="ContentTypeDescriptor"/> has a <see cref="PermittedContentTypes"/> collection which includes the <see
250+
/// cref="ContentTypeDescriptor"/>s associated with that <see cref="ContentTypeDescriptor"/>. As this is cached, however,
251+
/// the value should be reset whenever a <see cref="ContentTypeDescriptor"/> has been modified, in case those values have
252+
/// changed.
253+
/// </remarks>
254+
internal void ResetPermittedContentTypes() => _permittedContentTypes = null;
255+
241256
/*==========================================================================================================================
242257
| METHOD: IS TYPE OF?
243258
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic/Repositories/TopicRepositoryBase.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,21 @@ public virtual int Save([ValidatedNotNull]Topic topic, bool isRecursive = false,
355355
/*------------------------------------------------------------------------------------------------------------------------
356356
| If new content type, add to cache
357357
\-----------------------------------------------------------------------------------------------------------------------*/
358+
var asContentType = topic as ContentTypeDescriptor;
358359
if (
359360
topic.IsNew &&
360-
topic is ContentTypeDescriptor descriptor &&
361+
asContentType != null &&
361362
_contentTypeDescriptors != null &&
362363
!_contentTypeDescriptors.Contains(topic.Key)
363364
) {
364-
_contentTypeDescriptors.Add(descriptor);
365+
_contentTypeDescriptors.Add(asContentType);
366+
}
367+
368+
/*------------------------------------------------------------------------------------------------------------------------
369+
| If content type, and relationships have been updated, refresh permitted content types
370+
\-----------------------------------------------------------------------------------------------------------------------*/
371+
if (asContentType != null && asContentType.Relationships.IsDirty()) {
372+
asContentType.ResetPermittedContentTypes();
365373
}
366374

367375
/*------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)