Files
Umbraco-CMS/src/Umbraco.Core/Models/PublishedContent/PublishedContentModelFactory.cs

103 lines
4.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2017-09-25 12:58:54 +02:00
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
namespace Umbraco.Core.Models.PublishedContent
{
/// <summary>
/// Implements a strongly typed content model factory
/// </summary>
public class PublishedContentModelFactory : IPublishedContentModelFactory
{
private readonly Dictionary<string, ModelInfo> _modelInfos;
private class ModelInfo
{
public Type ParameterType { get; set; }
2017-09-25 08:59:32 +02:00
public Func<IPublishedElement, IPublishedElement> Ctor { get; set; }
public Type ModelType { get; set; }
}
public Dictionary<string, Type> ModelTypeMap { get; }
2014-05-19 13:15:47 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="PublishedContentModelFactory"/> class with types.
/// </summary>
/// <param name="types">The model types.</param>
/// <remarks>
/// <para>Types must implement <c>IPublishedContent</c> and have a unique constructor that
/// accepts one IPublishedContent as a parameter.</para>
/// <para>To activate,</para>
/// <code>
2017-05-30 15:33:13 +02:00
/// var types = TypeLoader.Current.GetTypes{PublishedContentModel}();
2014-05-19 13:15:47 +02:00
/// var factory = new PublishedContentModelFactoryImpl(types);
/// PublishedContentModelFactoryResolver.Current.SetFactory(factory);
/// </code>
/// </remarks>
public PublishedContentModelFactory(IEnumerable<Type> types)
{
2017-09-25 08:59:32 +02:00
var ctorArgTypes = new[] { typeof(IPublishedElement) };
var modelInfos = new Dictionary<string, ModelInfo>(StringComparer.InvariantCultureIgnoreCase);
2017-09-25 12:58:54 +02:00
var exprs = new List<Expression<Func<IPublishedElement, IPublishedElement>>>();
ModelTypeMap = new Dictionary<string, Type>(StringComparer.InvariantCultureIgnoreCase);
foreach (var type in types)
{
ConstructorInfo constructor = null;
Type parameterType = null;
foreach (var ctor in type.GetConstructors())
{
var parms = ctor.GetParameters();
2017-09-25 08:59:32 +02:00
if (parms.Length == 1 && typeof (IPublishedElement).IsAssignableFrom(parms[0].ParameterType))
{
if (constructor != null)
2017-09-25 08:59:32 +02:00
throw new InvalidOperationException($"Type {type.FullName} has more than one public constructor with one argument of type, or implementing, IPublishedElement.");
constructor = ctor;
parameterType = parms[0].ParameterType;
}
}
if (constructor == null)
2017-09-25 08:59:32 +02:00
throw new InvalidOperationException($"Type {type.FullName} is missing a public constructor with one argument of type, or implementing, IPublishedElement.");
var attribute = type.GetCustomAttribute<PublishedContentModelAttribute>(false); // fixme rename FacadeModelAttribute
var typeName = attribute == null ? type.Name : attribute.ContentTypeAlias;
if (modelInfos.TryGetValue(typeName, out ModelInfo modelInfo))
throw new InvalidOperationException($"Both types {type.FullName} and {modelInfo.ModelType.FullName} want to be a model type for content type with alias \"{typeName}\".");
2017-09-25 12:58:54 +02:00
exprs.Add(Expression.Lambda<Func<IPublishedElement, IPublishedElement>>(Expression.New(constructor)));
modelInfos[typeName] = new ModelInfo { ParameterType = parameterType, ModelType = type };
ModelTypeMap[typeName] = type;
}
2017-09-25 12:58:54 +02:00
var compiled = ReflectionUtilities.CompileToDelegates(exprs.ToArray());
var i = 0;
foreach (var modelInfo in modelInfos.Values)
modelInfo.Ctor = compiled[i++];
_modelInfos = modelInfos.Count > 0 ? modelInfos : null;
}
2017-09-25 08:59:32 +02:00
public IPublishedElement CreateModel(IPublishedElement set)
{
// fail fast
if (_modelInfos == null)
return set;
if (_modelInfos.TryGetValue(set.ContentType.Alias, out ModelInfo modelInfo) == false)
return set;
// ReSharper disable once UseMethodIsInstanceOfType
if (modelInfo.ParameterType.IsAssignableFrom(set.GetType()) == false)
throw new InvalidOperationException($"Model {modelInfo.ModelType} expects argument of type {modelInfo.ParameterType.FullName}, but got {set.GetType().FullName}.");
return modelInfo.Ctor(set);
}
}
}