Facade cleanup and refactoring

This commit is contained in:
Stephan
2017-09-29 15:51:33 +02:00
parent ccd54392b7
commit bcbd4289ea
34 changed files with 882 additions and 954 deletions

View File

@@ -14,7 +14,7 @@ namespace Umbraco.Core.Models.PublishedContent
private class ModelInfo
{
public Type ParameterType { get; set; }
public Func<IPublishedElement, IPublishedElement> Ctor { get; set; }
public Func<object, object> Ctor { get; set; }
public Type ModelType { get; set; }
}
@@ -69,8 +69,9 @@ namespace Umbraco.Core.Models.PublishedContent
if (modelInfos.TryGetValue(typeName, out var modelInfo))
throw new InvalidOperationException($"Both types {type.FullName} and {modelInfo.ModelType.FullName} want to be a model type for content type with alias \"{typeName}\".");
var ctorFunc = ReflectionUtilities.EmitCtor<Func<IPublishedElement, IPublishedElement>>(constructor);
modelInfos[typeName] = new ModelInfo { ParameterType = parameterType, ModelType = type, Ctor = ctorFunc };
// have to use an unsafe ctor because we don't know the types, really
var modelCtor = ReflectionUtilities.EmitCtorUnsafe<Func<object, object>>(constructor);
modelInfos[typeName] = new ModelInfo { ParameterType = parameterType, ModelType = type, Ctor = modelCtor };
ModelTypeMap[typeName] = type;
}
@@ -90,7 +91,8 @@ namespace Umbraco.Core.Models.PublishedContent
if (modelInfo.ParameterType.IsAssignableFrom(element.GetType()) == false)
throw new InvalidOperationException($"Model {modelInfo.ModelType} expects argument of type {modelInfo.ParameterType.FullName}, but got {element.GetType().FullName}.");
return modelInfo.Ctor(element);
// can cast, because we checked when creating the ctor
return (IPublishedElement) modelInfo.Ctor(element);
}
}
}