Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #229 from leekelleher/devx/default-processor
Browse files Browse the repository at this point in the history
Internal refactor - Get Default Processor instance not type
  • Loading branch information
leekelleher authored Oct 3, 2017
2 parents 515ebcc + b81b501 commit f3a4c9d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Umbraco.Core;

namespace Our.Umbraco.Ditto
{
Expand Down Expand Up @@ -118,21 +119,21 @@ public void RegisterProcessorAttribute<TObjectType, TProcessorAttributeType>(TPr
}

/// <summary>
/// Gets the default processor attribute type for the given object type.
/// Gets the default processor for the given object type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>
/// Returns the default processor attribute type for the given object type.
/// Returns the default processor for the given object type.
/// </returns>
public Type GetDefaultProcessorType(Type objectType)
public DittoProcessorAttribute GetDefaultProcessorFor(Type objectType)
{
var attr = objectType.GetCustomAttribute<DittoDefaultProcessorAttribute>();
if (attr != null)
{
return attr.ProcessorType;
return (DittoProcessorAttribute)attr.ProcessorType.GetInstance();
}

return DefaultProcessorType;
return (DittoProcessorAttribute)DefaultProcessorType.GetInstance();
}

/// <summary>
Expand Down
18 changes: 6 additions & 12 deletions src/Our.Umbraco.Ditto/Extensions/PublishedContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,6 @@ private static object ConvertContent(
"A valid constructor is either an empty one, or one accepting a single IPublishedContent parameter.");
}

// Gets the default processor for this conversion.
var defaultProcessorType = DittoProcessorRegistry.Instance.GetDefaultProcessorType(type);
PropertyInfo[] lazyProperties = null;

// If not already an instance, create an instance of the object
Expand Down Expand Up @@ -378,7 +376,7 @@ private static object ConvertContent(
throw new InvalidOperationException($"Lazy property '{propertyInfo.Name}' of type '{type.AssemblyQualifiedName}' must be declared virtual in order to be lazy loadable.");
}

lazyMappings.Add(propertyInfo.Name, new Lazy<object>(() => GetProcessedValue(content, culture, type, propertyInfo, instance, defaultProcessorType, contextAccessor, chainContext)));
lazyMappings.Add(propertyInfo.Name, new Lazy<object>(() => GetProcessedValue(content, culture, type, propertyInfo, instance, contextAccessor, chainContext)));
}
}

Expand All @@ -395,7 +393,7 @@ private static object ConvertContent(
}

// Set the value normally.
var value = GetProcessedValue(content, culture, type, propertyInfo, instance, defaultProcessorType, contextAccessor, chainContext);
var value = GetProcessedValue(content, culture, type, propertyInfo, instance, contextAccessor, chainContext);

// This over 4x faster as propertyInfo.SetValue(instance, value, null);
FastPropertyAccessor.SetValue(propertyInfo, instance, value);
Expand All @@ -416,7 +414,6 @@ private static object ConvertContent(
/// <param name="targetType">The target type.</param>
/// <param name="propertyInfo">The <see cref="PropertyInfo" /> property info associated with the type.</param>
/// <param name="instance">The instance to assign the value to.</param>
/// <param name="defaultProcessorType">The default processor type.</param>
/// <param name="contextAccessor">The context accessor.</param>
/// <param name="chainContext">The <see cref="DittoChainContext"/> for the current processor chain.</param>
/// <returns>
Expand All @@ -428,7 +425,6 @@ private static object GetProcessedValue(
Type targetType,
PropertyInfo propertyInfo,
object instance,
Type defaultProcessorType,
IDittoContextAccessor contextAccessor,
DittoChainContext chainContext)
{
Expand All @@ -451,11 +447,11 @@ private static object GetProcessedValue(
if (cacheAttr != null)
{
var ctx = new DittoCacheContext(cacheAttr, content, targetType, propertyDescriptor, culture);
return cacheAttr.GetCacheItem(ctx, () => DoGetProcessedValue(content, propertyInfo, defaultProcessorType, contextAccessor, baseProcessorContext, chainContext));
return cacheAttr.GetCacheItem(ctx, () => DoGetProcessedValue(content, propertyInfo, contextAccessor, baseProcessorContext, chainContext));
}
else
{
return DoGetProcessedValue(content, propertyInfo, defaultProcessorType, contextAccessor, baseProcessorContext, chainContext);
return DoGetProcessedValue(content, propertyInfo, contextAccessor, baseProcessorContext, chainContext);
}
}
}
Expand All @@ -465,15 +461,13 @@ private static object GetProcessedValue(
/// </summary>
/// <param name="content">The content.</param>
/// <param name="propertyInfo">The property information.</param>
/// <param name="defaultProcessorType">The default processor type.</param>
/// <param name="contextAccessor">The context accessor.</param>
/// <param name="baseProcessorContext">The base processor context.</param>
/// <param name="chainContext">The <see cref="DittoChainContext"/> for the current processor chain.</param>
/// <returns>Returns the processed value.</returns>
private static object DoGetProcessedValue(
IPublishedContent content,
PropertyInfo propertyInfo,
Type defaultProcessorType,
IDittoContextAccessor contextAccessor,
DittoProcessorContext baseProcessorContext,
DittoChainContext chainContext)
Expand All @@ -485,8 +479,8 @@ private static object DoGetProcessedValue(

if (!processorAttrs.Any())
{
// Adds the default processor attribute
processorAttrs.Add((DittoProcessorAttribute)defaultProcessorType.GetInstance());
// Adds the default processor for this conversion
processorAttrs.Add(DittoProcessorRegistry.Instance.GetDefaultProcessorFor(baseProcessorContext.TargetType));
}

var propertyType = propertyInfo.PropertyType;
Expand Down

0 comments on commit f3a4c9d

Please sign in to comment.