Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.
Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.
This is how I do it. I highly recommend this approach because it keeps everything in one spot with this init module having a single responsibility. I don't think you will cause any performance impacts because my guess is that Epi does the same thing with an init module by scanning the code, finding usages of the attribute, and updating the AvailableTypesRepository.
Here's an example for references:
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class PageRestrictionInitializationModel : IInitializableModule
{
private IContentTypeRepository _contentTypeRepository;
private IAvailableSettingsRepository _availableSettingsRepository;
private ILogger _log;
public void Initialize(InitializationEngine context)
{
_contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
_availableSettingsRepository = ServiceLocator.Current.GetInstance<IAvailableSettingsRepository>();
_log = LogManager.GetLogger();
// pages that should be leaves and not allow anything
DisallowAll<EventPage>();
DisallowAll<NewsPage>();
DisallowAll<BlogPage>();
DisallowAll<ProductPage>();
// Home Page
SetPageRestriction<HomePage>(new List<Type>
{
typeof(NewsEventsLandingPage),
typeof(BlogLandingPage),
typeof(GeneralPage),
typeof(ProductCategoryPage)
});
SetPageRestriction<NewsEventsLandingPage>(new List<Type>
{
typeof(EventPage),
typeof(NewsPage)
});
// ... etc ...
}
private void DisallowAll<T>()
{
var page = _contentTypeRepository.Load(typeof(T));
var setting = new AvailableSetting
{
Availability = Availability.None
};
_availableSettingsRepository.RegisterSetting(page, setting);
}
public void Uninitialize(InitializationEngine context)
{
}
private void SetPageRestriction<T>(IEnumerable<Type> pageTypes)
{
var page = _contentTypeRepository.Load(typeof(T));
var setting = new AvailableSetting
{
Availability = Availability.Specific
};
foreach (var pageType in pageTypes)
{
var contentType = _contentTypeRepository.Load(pageType);
setting.AllowedContentTypeNames.Add(contentType.Name);
}
_availableSettingsRepository.RegisterSetting(page, setting);
_log.Log(Level.Debug, $"{page.Name} restriction added.");
}
}
I would like to group all my AvailableContentTypes settings inside an initializationModule so that I can find all the settings in one place. Is there a performance issue with that kind of approach ? We are talking about roughly ~30 PageType with ~5 types of pages as possible children & Availability.Specific for every settings.