Version -alpha.30 with ModelsBuilder -alpha.13

This commit is contained in:
Stephan
2017-10-26 11:25:01 +02:00
parent edc5b398bd
commit 2305426dda
13 changed files with 91 additions and 30 deletions

View File

@@ -74,6 +74,39 @@ namespace Umbraco.Core.Models.PublishedContent
return def.MakeGenericType(args);
}
/// <summary>
/// Gets the actual Clr type name by replacing model types, if any.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="map">The model types map.</param>
/// <returns>The actual Clr type name.</returns>
public static string MapToName(Type type, Dictionary<string, string> map)
{
if (type is ModelType modelType)
{
if (map.TryGetValue(modelType.ContentTypeAlias, out var actualTypeName))
return actualTypeName;
throw new InvalidOperationException($"Don't know how to map ModelType with content type alias \"{modelType.ContentTypeAlias}\".");
}
if (type is ModelTypeArrayType arrayType)
{
if (map.TryGetValue(arrayType.ContentTypeAlias, out var actualTypeName))
return actualTypeName + "[]";
throw new InvalidOperationException($"Don't know how to map ModelType with content type alias \"{arrayType.ContentTypeAlias}\".");
}
if (type.IsGenericType == false)
return type.FullName;
var def = type.GetGenericTypeDefinition();
if (def == null)
throw new InvalidOperationException("panic");
var args = type.GetGenericArguments().Select(x => MapToName(x, map)).ToArray();
var defFullName = def.FullName.Substring(0, def.FullName.IndexOf('`'));
return defFullName + "<" + string.Join(", ", args) + ">";
}
/// <summary>
/// Gets a value indicating whether two <see cref="Type"/> instances are equal.
/// </summary>
@@ -229,12 +262,10 @@ namespace Umbraco.Core.Models.PublishedContent
public override Guid GUID { get; } = Guid.NewGuid();
/// <inheritdoc />
//public override Module Module => throw new NotSupportedException();
public override Module Module => GetType().Module;
public override Module Module => GetType().Module; // hackish but FullName requires something
/// <inheritdoc />
//public override Assembly Assembly => throw new NotSupportedException();
public override Assembly Assembly => GetType().Assembly;
public override Assembly Assembly => GetType().Assembly; // hackish but FullName requires something
/// <inheritdoc />
public override string FullName => Name;
@@ -354,8 +385,8 @@ namespace Umbraco.Core.Models.PublishedContent
public override string Name { get; }
public override Guid GUID { get; } = Guid.NewGuid();
public override Module Module => throw new NotSupportedException();
public override Assembly Assembly => throw new NotSupportedException();
public override Module Module =>GetType().Module; // hackish but FullName requires something
public override Assembly Assembly => GetType().Assembly; // hackish but FullName requires something
public override string FullName => Name;
public override string Namespace => string.Empty;
public override string AssemblyQualifiedName => Name;

View File

@@ -10,14 +10,14 @@ namespace Umbraco.Core.Models.PublishedContent
/// <remarks>By default, the name of the class is assumed to be the content type alias. The
/// <c>PublishedContentModelAttribute</c> can be used to indicate a different alias.</remarks>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class PublishedContentModelAttribute : Attribute
public sealed class PublishedModelAttribute : Attribute
{
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="PublishedContentModelAttribute" /> class with a content type alias.
/// Initializes a new instance of the <see cref="PublishedModelAttribute" /> class with a content type alias.
/// </summary>
/// <param name="contentTypeAlias">The content type alias.</param>
public PublishedContentModelAttribute(string contentTypeAlias)
public PublishedModelAttribute(string contentTypeAlias)
{
if (string.IsNullOrWhiteSpace(contentTypeAlias)) throw new ArgumentNullOrEmptyException(nameof(contentTypeAlias));
ContentTypeAlias = contentTypeAlias;

View File

@@ -64,7 +64,7 @@ namespace Umbraco.Core.Models.PublishedContent
if (constructor == null)
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);
var attribute = type.GetCustomAttribute<PublishedModelAttribute>(false);
var typeName = attribute == null ? type.Name : attribute.ContentTypeAlias;
if (modelInfos.TryGetValue(typeName, out var modelInfo))