Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Mimic.Web/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<add key="autoFormsAuthentication" value="false" />
<add key="dataAnnotations:dataTypeAttribute:disableRegEx" value="false" />
<add key="owin:appStartup" value="UmbracoDefaultOwinStartup" />
<add key="Umbraco.ModelsBuilder.Enable" value="true" />
<add key="Umbraco.ModelsBuilder.Enable" value="false" />
<add key="Umbraco.ModelsBuilder.ModelsMode" value="PureLive" />
</appSettings>
<!--
Expand Down
1 change: 1 addition & 0 deletions src/Mimic/Context/MapperContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public class MapperContext
{
public PropertyInfo Property { get; internal set; }
public IPublishedContent Content { get; set; }
public IPublishedElement Element { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/Mimic/Helpers/UmbracoPropertyLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public IPublishedProperty GetClosestProperty(IPublishedContent content, string p
{
return content.Properties.FirstOrDefault(property => property.Alias.ToLower() == propertyName.ToLower());
}

public IPublishedProperty GetClosestProperty(IPublishedElement element, string propertyName = "")
{
return element.Properties.FirstOrDefault(property => property.Alias.ToLower() == propertyName.ToLower());
}
}
}
72 changes: 72 additions & 0 deletions src/Mimic/IPublishedElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Mimic.Context;
using Mimic.PropertyMapperAttributes;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Persistence.Mappers;

namespace Mimic
{
public static class IPublishedElementExtensions
{
public static T As<T>(this IPublishedElement content)
{
var type = typeof(T);

return (T)As(content, type);
}

public static List<T> As<T>(this IEnumerable<IPublishedElement> elements)
{
var type = typeof(T);

return elements.Select(element => (T)As(element, type)).ToList();
}

public static object As(this IPublishedElement element, Type type)
{
if (element == null)
return null;

var context = new MapperContext { Element = element };

var instance = Activator.CreateInstance(type);

foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanWrite))
{
context.Property = property;

var mapper = ResolveMapper(element, type, property);

if (mapper is Ignore)
continue;

mapper.Context = context;

var value = mapper.ProcessValue(true);

property.SetValue(instance, value);
}

return instance;
}

private static PropertyMapperAttribute ResolveMapper(IPublishedElement element, Type type, PropertyInfo property)
{
//resolve by attribute
var attribute = property.GetCustomAttributes().FirstOrDefault(a => a is PropertyMapperAttribute);
if (attribute != null)
{
return (PropertyMapperAttribute)attribute;
}

return new SimpleType();
}
}
}
1 change: 1 addition & 0 deletions src/Mimic/Mimic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<Compile Include="Extentions\StringExtentions.cs" />
<Compile Include="Extentions\UmbracoHelperExtensions.cs" />
<Compile Include="Helpers\UmbracoPropertyLocator.cs" />
<Compile Include="IPublishedElementExtensions.cs" />
<Compile Include="IPublishedContentExtensions.cs" />
<Compile Include="Models\MimicNestedContent.cs" />
<Compile Include="Models\MimicPropertyClass.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/Mimic/PropertyMapperAttributes/Children.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Mimic.PropertyMapperAttributes
{
public class Children : PropertyMapperAttribute
{
public override object ProcessValue()
public override object ProcessValue(bool isElement = false)
{
if (Context.Property.PropertyType.GenericTypeArguments[0] == typeof (IPublishedContent))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Mimic/PropertyMapperAttributes/Ignore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Mimic.PropertyMapperAttributes
{
public class Ignore : PropertyMapperAttribute
{
public override object ProcessValue()
public override object ProcessValue(bool isElement = false)
{
return null;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Mimic/PropertyMapperAttributes/PropertyMapperAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public abstract class PropertyMapperAttribute : Attribute
{
public MapperContext Context { get; set; }
public IPublishedContent Content => Context.Content;
public IPublishedElement Element => Context.Element;

protected IPublishedProperty GetClosestProperty(string propertyName = "")
{
Expand All @@ -21,6 +22,13 @@ protected IPublishedProperty GetClosestProperty(string propertyName = "")
return new UmbracoPropertyLocator().GetClosestProperty(Content, propertyName);
}

public abstract object ProcessValue();
protected IPublishedProperty GetClosestElement(string propertyName = "")
{
propertyName = !string.IsNullOrEmpty(propertyName) ? propertyName : Context.Property.Name;

return new UmbracoPropertyLocator().GetClosestProperty(Element, propertyName);
}

public abstract object ProcessValue(bool isElement = false);
}
}
2 changes: 1 addition & 1 deletion src/Mimic/PropertyMapperAttributes/Self.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mimic.PropertyMapperAttributes
{
public class Self : PropertyMapperAttribute
{
public override object ProcessValue()
public override object ProcessValue(bool isElement = false)
{
return Content.As(Context.Property.PropertyType);
}
Expand Down
42 changes: 36 additions & 6 deletions src/Mimic/PropertyMapperAttributes/SimpleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace Mimic.PropertyMapperAttributes
{
public class SimpleType : PropertyMapperAttribute
{
public override object ProcessValue()
public override object ProcessValue(bool isElement = false)
{
var value = ResolveStictProperty();

return value ?? ResolveUmbracoProperty();
return value ?? ResolveUmbracoProperty(isElement);
}

protected object ResolveStictProperty()
Expand All @@ -38,9 +38,18 @@ protected object ResolveStictProperty()
return null;
}

protected object ResolveUmbracoProperty()
protected object ResolveUmbracoProperty(bool isElement = false)
{
var property = GetClosestProperty();
IPublishedProperty property = null;

if (isElement)
{
property = GetClosestElement();
}
else
{
property = GetClosestProperty();
}

if (property == null)
return null;
Expand Down Expand Up @@ -83,8 +92,6 @@ protected object ResolveUmbracoProperty()
var typedItem = item.As(arg);

value.Add(typedItem);

//value.Add(item.As(Context.Property.PropertyType.GenericTypeArguments[0]));
}
return value;
}
Expand All @@ -98,6 +105,29 @@ protected object ResolveUmbracoProperty()
{
return propertyValue.ToString();
}
else if (propertyValue is IEnumerable<IPublishedElement> &&
(Context.Property.PropertyType.GenericTypeArguments.Length > 0 && Context.Property.PropertyType.GenericTypeArguments[0] != typeof(IPublishedElement))
&&
!(propertyValue is IEnumerable<IPublishedContent>)
)
{
var propertyValueEnumerable = (IEnumerable<IPublishedElement>)propertyValue;
var value = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(Context.Property.PropertyType.GenericTypeArguments[0]));

var listType = typeof(List<>).MakeGenericType(Context.Property.PropertyType);
var list = (IList)Activator.CreateInstance(listType);


foreach (IPublishedElement item in propertyValueEnumerable)
{
var arg = Context.Property.PropertyType.GenericTypeArguments[0];

var typedItem = item.As(arg);

value.Add(typedItem);
}
return value;
}

if (propertyValue != null)
{
Expand Down