Skip to content

Instantly share code, notes, and snippets.

@joseftw
Created April 10, 2016 14:46
Show Gist options
  • Save joseftw/c26dae7fa498cb27573d8feeade26a93 to your computer and use it in GitHub Desktop.
Save joseftw/c26dae7fa498cb27573d8feeade26a93 to your computer and use it in GitHub Desktop.
Shows how to "inject" own types to the AvailableContentTypes attribute at runtime in EPiServer
using System.Web.Mvc;
using EPiServer.DataAbstraction.RuntimeModel;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using ModularAllowedTypes.Business.Rendering;
using EPiServer.Web.Mvc;
using EPiServer.Web.Mvc.Html;
using StructureMap;
namespace InjectedAllowedTypes.Business.Initialization
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class DependencyResolverInitialization : IConfigurableModule
{
public void ConfigureContainer(ServiceConfigurationContext context)
{
context.Container.Configure(ConfigureContainer);
DependencyResolver.SetResolver(new StructureMapDependencyResolver(context.Container));
}
private static void ConfigureContainer(ConfigurationExpression container)
{
//Swap out the default ContentRenderer for our custom
container.For<IContentRenderer>().Use<ErrorHandlingContentRenderer>();
container.For<ContentAreaRenderer>().Use<AlloyContentAreaRenderer>();
//Implementations for custom interfaces can be registered here.
container.For<IAvailableModelSettingsRepository>().Use<InjectedAvailableModelSettingsRepository>();
}
public void Initialize(InitializationEngine context)
{
}
public void Uninitialize(InitializationEngine context)
{
}
public void Preload(string[] parameters)
{
}
}
}
using System;
using System.Collections.Generic;
using EPiServer.DataAbstraction;
using EPiServer.DataAbstraction.RuntimeModel;
using Feature.CoolPage;
using Feature.MediaPage;
namespace InjectedAllowedTypes
{
public static class InjectedAvailableModelSettings
{
public static Dictionary<Type, ContentTypeAvailableModelSetting> GetCustomAvailableModelSettings()
{
var mappedModelSettings = MappedModelSettings();
return mappedModelSettings;
}
private static Dictionary<Type, ContentTypeAvailableModelSetting> MappedModelSettings()
{
return new Dictionary<Type, ContentTypeAvailableModelSetting>
{
{
typeof(MediaPage) , new ContentTypeAvailableModelSetting
{
Availability = Availability.Specific,
Included = new HashSet<Type> {typeof(CoolPage) }
}
}
};
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer.DataAbstraction;
using EPiServer.DataAbstraction.RuntimeModel;
using EPiServer.ServiceLocation;
namespace InjectedAllowedTypes
{
public class InjectedAvailableModelSettingsRepository : AvailableModelSettingsRepository
{
private static readonly Dictionary<Type, ContentTypeAvailableModelSetting> CustomSettings = InjectedAvailableModelSettings.GetCustomAvailableModelSettings();
public InjectedAvailableModelSettingsRepository(ServiceAccessor<IContentTypeRepository> contentTypeRepository) : base(contentTypeRepository)
{
}
public override IDictionary<Type, ContentTypeAvailableModelSetting> ListRuntimeSettings()
{
var runtimeSettings = base.ListRuntimeSettings();
foreach (var customSetting in CustomSettings)
{
if (runtimeSettings.ContainsKey(customSetting.Key))
{
var merged = MergeSettings(customSetting.Value, runtimeSettings[customSetting.Key]);
runtimeSettings[customSetting.Key] = merged;
}
}
return runtimeSettings;
}
private ContentTypeAvailableModelSetting MergeSettings(ContentTypeAvailableModelSetting customSetting, ContentTypeAvailableModelSetting runtimeSetting)
{
var mergedSetting = new ContentTypeAvailableModelSetting();
mergedSetting.Excluded = new HashSet<Type>(customSetting.Excluded.Concat(runtimeSetting.Excluded).Distinct());
mergedSetting.Included = new HashSet<Type>(customSetting.Included.Concat(runtimeSetting.Included).Distinct());
mergedSetting.IncludedOn = new HashSet<Type>(customSetting.IncludedOn.Concat(runtimeSetting.IncludedOn).Distinct());
mergedSetting.Availability = customSetting.Availability;
return mergedSetting;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment