diff --git a/src/Our.Umbraco.Ditto/Attributes/DittoIgnoreAttribute.cs b/src/Our.Umbraco.Ditto/Attributes/DittoIgnoreAttribute.cs index 96e6f74..ebdcc1b 100644 --- a/src/Our.Umbraco.Ditto/Attributes/DittoIgnoreAttribute.cs +++ b/src/Our.Umbraco.Ditto/Attributes/DittoIgnoreAttribute.cs @@ -2,8 +2,8 @@ namespace Our.Umbraco.Ditto { - [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] - public class DittoIgnoreAttribute : Attribute - { - } + [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] + public class DittoIgnoreAttribute : Attribute + { + } } \ No newline at end of file diff --git a/src/Our.Umbraco.Ditto/Attributes/UmbracoPropertyAttribute.cs b/src/Our.Umbraco.Ditto/Attributes/UmbracoPropertyAttribute.cs index db2c542..fe9b8b7 100644 --- a/src/Our.Umbraco.Ditto/Attributes/UmbracoPropertyAttribute.cs +++ b/src/Our.Umbraco.Ditto/Attributes/UmbracoPropertyAttribute.cs @@ -2,23 +2,23 @@ namespace Our.Umbraco.Ditto { - [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] - public class UmbracoPropertyAttribute : Attribute - { - public string PropertyName { get; set; } + [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] + public class UmbracoPropertyAttribute : Attribute + { + public string PropertyName { get; set; } - public string AltPropertyName { get; set; } + public string AltPropertyName { get; set; } - public bool Recursive { get; set; } + public bool Recursive { get; set; } - public object DefaultValue { get; set; } + public object DefaultValue { get; set; } - public UmbracoPropertyAttribute(string propertyName, string altPropertyName = "", bool recursive = false, object defaultValue = null) - { - PropertyName = propertyName; - AltPropertyName = altPropertyName; - Recursive = recursive; - DefaultValue = defaultValue; - } - } + public UmbracoPropertyAttribute(string propertyName, string altPropertyName = "", bool recursive = false, object defaultValue = null) + { + PropertyName = propertyName; + AltPropertyName = altPropertyName; + Recursive = recursive; + DefaultValue = defaultValue; + } + } } \ No newline at end of file diff --git a/src/Our.Umbraco.Ditto/EventArgs/ConvertedTypeEventArgs.cs b/src/Our.Umbraco.Ditto/EventArgs/ConvertedTypeEventArgs.cs index faa77cc..94496f0 100644 --- a/src/Our.Umbraco.Ditto/EventArgs/ConvertedTypeEventArgs.cs +++ b/src/Our.Umbraco.Ditto/EventArgs/ConvertedTypeEventArgs.cs @@ -3,12 +3,12 @@ namespace Our.Umbraco.Ditto { - public class ConvertedTypeEventArgs : EventArgs - { - public IPublishedContent Content { get; set; } + public class ConvertedTypeEventArgs : EventArgs + { + public IPublishedContent Content { get; set; } - public object Converted { get; set; } - - public Type ConvertedType { get; set; } - } + public object Converted { get; set; } + + public Type ConvertedType { get; set; } + } } \ No newline at end of file diff --git a/src/Our.Umbraco.Ditto/EventArgs/ConvertingTypeEventArgs.cs b/src/Our.Umbraco.Ditto/EventArgs/ConvertingTypeEventArgs.cs index 2db07d8..862e6ff 100644 --- a/src/Our.Umbraco.Ditto/EventArgs/ConvertingTypeEventArgs.cs +++ b/src/Our.Umbraco.Ditto/EventArgs/ConvertingTypeEventArgs.cs @@ -3,8 +3,8 @@ namespace Our.Umbraco.Ditto { - public class ConvertingTypeEventArgs : CancelEventArgs - { - public IPublishedContent Content { get; set; } - } + public class ConvertingTypeEventArgs : CancelEventArgs + { + public IPublishedContent Content { get; set; } + } } \ No newline at end of file diff --git a/src/Our.Umbraco.Ditto/EventArgs/EventHandlers.cs b/src/Our.Umbraco.Ditto/EventArgs/EventHandlers.cs index ce30dcc..1d9d334 100644 --- a/src/Our.Umbraco.Ditto/EventArgs/EventHandlers.cs +++ b/src/Our.Umbraco.Ditto/EventArgs/EventHandlers.cs @@ -2,22 +2,22 @@ namespace Our.Umbraco.Ditto { - public static class EventHandlers - { - public static event EventHandler ConvertingType; + public static class EventHandlers + { + public static event EventHandler ConvertingType; - public static event EventHandler ConvertedType; + public static event EventHandler ConvertedType; - public static void CallConvertingTypeHandler(ConvertingTypeEventArgs args) - { - if (ConvertingType != null) - ConvertingType(null, args); - } + public static void CallConvertingTypeHandler(ConvertingTypeEventArgs args) + { + if (ConvertingType != null) + ConvertingType(null, args); + } - public static void CallConvertedTypeHandler(ConvertedTypeEventArgs args) - { - if (ConvertedType != null) - ConvertedType(null, args); - } - } + public static void CallConvertedTypeHandler(ConvertedTypeEventArgs args) + { + if (ConvertedType != null) + ConvertedType(null, args); + } + } } \ No newline at end of file diff --git a/src/Our.Umbraco.Ditto/Extensions/PublishedContentExtensions.cs b/src/Our.Umbraco.Ditto/Extensions/PublishedContentExtensions.cs index cee4dcb..296a38c 100644 --- a/src/Our.Umbraco.Ditto/Extensions/PublishedContentExtensions.cs +++ b/src/Our.Umbraco.Ditto/Extensions/PublishedContentExtensions.cs @@ -9,164 +9,164 @@ namespace Our.Umbraco.Ditto { - public static class PublishedContentExtensions - { - internal static object As(this IPublishedContent content, Type type, - Action convertingType = null, - Action convertedType = null) - { - object instance = null; - - if (content == null) - return instance; - - using (DisposableTimer.DebugDuration(type, string.Format("IPublishedContent As ({0})", content.DocumentTypeAlias), "Complete")) - { - var constructor = type.GetConstructors() - .OrderBy(x => x.GetParameters().Length) - .First(); - var constructorParams = constructor.GetParameters(); - - var args1 = new ConvertingTypeEventArgs { Content = content }; - - EventHandlers.CallConvertingTypeHandler(args1); - - if (!args1.Cancel && convertingType != null) - convertingType(args1); - - if (args1.Cancel) - return instance; - - if (constructorParams.Length == 0) - { - instance = Activator.CreateInstance(type); - } - else if (constructorParams.Length == 1 & constructorParams[0].ParameterType == typeof(IPublishedContent)) - { - instance = Activator.CreateInstance(type, content); - } - else - { - throw new InvalidOperationException("Type {0} has invalid constructor parameters"); - } - - var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); - var contentType = content.GetType(); - - foreach (var propertyInfo in properties.Where(x => x.CanWrite)) - { - using (DisposableTimer.DebugDuration(type, string.Format("ForEach Property ({1} {0})", propertyInfo.Name, content.Id), "Complete")) - { - // check for the ignore attribute - var ignoreAttr = propertyInfo.GetCustomAttribute(); - if (ignoreAttr != null) - continue; - - var umbracoPropertyName = propertyInfo.Name; - var altUmbracoPropertyName = string.Empty; - var recursive = false; - object defaultValue = null; - - var umbracoPropertyAttr = propertyInfo.GetCustomAttribute(); - if (umbracoPropertyAttr != null) - { - umbracoPropertyName = umbracoPropertyAttr.PropertyName; - altUmbracoPropertyName = umbracoPropertyAttr.AltPropertyName; - recursive = umbracoPropertyAttr.Recursive; - defaultValue = umbracoPropertyAttr.DefaultValue; - } - - // Try fetching the value - var contentProperty = contentType.GetProperty(umbracoPropertyName); - var propertyValue = contentProperty != null - ? contentProperty.GetValue(content, null) - : content.GetPropertyValue(umbracoPropertyName, - recursive); - - // Try fetching the alt value - if (propertyValue == null && !string.IsNullOrWhiteSpace(altUmbracoPropertyName)) - { - contentProperty = contentType.GetProperty(altUmbracoPropertyName); - propertyValue = contentProperty != null - ? contentProperty.GetValue(content, null) - : content.GetPropertyValue(altUmbracoPropertyName, - recursive); - } - - // Try setting the default value - if (propertyValue == null && defaultValue != null) - propertyValue = defaultValue; - - // Process the value - if (propertyValue != null) - { - if (propertyInfo.PropertyType.IsInstanceOfType(propertyValue)) - { - propertyInfo.SetValue(instance, propertyValue, null); - } - else - { - using (DisposableTimer.DebugDuration(type, string.Format("TypeConverter ({0}, {1})", content.Id, propertyInfo.Name), "Complete")) - { - var converterAttr = propertyInfo.GetCustomAttribute(); - if (converterAttr != null) - { - var converter = Activator.CreateInstance(Type.GetType(converterAttr.ConverterTypeName)) as TypeConverter; - propertyInfo.SetValue(instance, converter.ConvertFrom(propertyValue), null); - } - else - { - var convert = propertyValue.TryConvertTo(propertyInfo.PropertyType); - if (convert.Success) - { - propertyInfo.SetValue(instance, convert.Result, null); - } - } - } - } - } - } - } - - var args2 = new ConvertedTypeEventArgs - { - Content = content, - Converted = instance, - ConvertedType = type - }; - - if (convertedType != null) - convertedType(args2); - - EventHandlers.CallConvertedTypeHandler(args2); - - return args2.Converted; - } - } - - public static T As(this IPublishedContent content, - Action convertingType = null, - Action convertedType = null) - where T : class - { - return content.As(typeof(T), convertingType, convertedType) as T; - } - - public static IEnumerable As(this IEnumerable items, - string documentTypeAlias = null, - Action convertingType = null, - Action convertedType = null) - where T : class - { - using (DisposableTimer.DebugDuration>(string.Format("IEnumerable As ({0})", documentTypeAlias))) - { - if (string.IsNullOrWhiteSpace(documentTypeAlias)) - return items.Select(x => x.As(convertingType, convertedType)); - - return items - .Where(x => documentTypeAlias.InvariantEquals(x.DocumentTypeAlias)) - .Select(x => x.As(convertingType, convertedType)); - } - } - } + public static class PublishedContentExtensions + { + internal static object As(this IPublishedContent content, Type type, + Action convertingType = null, + Action convertedType = null) + { + object instance = null; + + if (content == null) + return instance; + + using (DisposableTimer.DebugDuration(type, string.Format("IPublishedContent As ({0})", content.DocumentTypeAlias), "Complete")) + { + var constructor = type.GetConstructors() + .OrderBy(x => x.GetParameters().Length) + .First(); + var constructorParams = constructor.GetParameters(); + + var args1 = new ConvertingTypeEventArgs { Content = content }; + + EventHandlers.CallConvertingTypeHandler(args1); + + if (!args1.Cancel && convertingType != null) + convertingType(args1); + + if (args1.Cancel) + return instance; + + if (constructorParams.Length == 0) + { + instance = Activator.CreateInstance(type); + } + else if (constructorParams.Length == 1 & constructorParams[0].ParameterType == typeof(IPublishedContent)) + { + instance = Activator.CreateInstance(type, content); + } + else + { + throw new InvalidOperationException("Type {0} has invalid constructor parameters"); + } + + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + var contentType = content.GetType(); + + foreach (var propertyInfo in properties.Where(x => x.CanWrite)) + { + using (DisposableTimer.DebugDuration(type, string.Format("ForEach Property ({1} {0})", propertyInfo.Name, content.Id), "Complete")) + { + // check for the ignore attribute + var ignoreAttr = propertyInfo.GetCustomAttribute(); + if (ignoreAttr != null) + continue; + + var umbracoPropertyName = propertyInfo.Name; + var altUmbracoPropertyName = string.Empty; + var recursive = false; + object defaultValue = null; + + var umbracoPropertyAttr = propertyInfo.GetCustomAttribute(); + if (umbracoPropertyAttr != null) + { + umbracoPropertyName = umbracoPropertyAttr.PropertyName; + altUmbracoPropertyName = umbracoPropertyAttr.AltPropertyName; + recursive = umbracoPropertyAttr.Recursive; + defaultValue = umbracoPropertyAttr.DefaultValue; + } + + // Try fetching the value + var contentProperty = contentType.GetProperty(umbracoPropertyName); + var propertyValue = contentProperty != null + ? contentProperty.GetValue(content, null) + : content.GetPropertyValue(umbracoPropertyName, + recursive); + + // Try fetching the alt value + if (propertyValue == null && !string.IsNullOrWhiteSpace(altUmbracoPropertyName)) + { + contentProperty = contentType.GetProperty(altUmbracoPropertyName); + propertyValue = contentProperty != null + ? contentProperty.GetValue(content, null) + : content.GetPropertyValue(altUmbracoPropertyName, + recursive); + } + + // Try setting the default value + if (propertyValue == null && defaultValue != null) + propertyValue = defaultValue; + + // Process the value + if (propertyValue != null) + { + if (propertyInfo.PropertyType.IsInstanceOfType(propertyValue)) + { + propertyInfo.SetValue(instance, propertyValue, null); + } + else + { + using (DisposableTimer.DebugDuration(type, string.Format("TypeConverter ({0}, {1})", content.Id, propertyInfo.Name), "Complete")) + { + var converterAttr = propertyInfo.GetCustomAttribute(); + if (converterAttr != null) + { + var converter = Activator.CreateInstance(Type.GetType(converterAttr.ConverterTypeName)) as TypeConverter; + propertyInfo.SetValue(instance, converter.ConvertFrom(propertyValue), null); + } + else + { + var convert = propertyValue.TryConvertTo(propertyInfo.PropertyType); + if (convert.Success) + { + propertyInfo.SetValue(instance, convert.Result, null); + } + } + } + } + } + } + } + + var args2 = new ConvertedTypeEventArgs + { + Content = content, + Converted = instance, + ConvertedType = type + }; + + if (convertedType != null) + convertedType(args2); + + EventHandlers.CallConvertedTypeHandler(args2); + + return args2.Converted; + } + } + + public static T As(this IPublishedContent content, + Action convertingType = null, + Action convertedType = null) + where T : class + { + return content.As(typeof(T), convertingType, convertedType) as T; + } + + public static IEnumerable As(this IEnumerable items, + string documentTypeAlias = null, + Action convertingType = null, + Action convertedType = null) + where T : class + { + using (DisposableTimer.DebugDuration>(string.Format("IEnumerable As ({0})", documentTypeAlias))) + { + if (string.IsNullOrWhiteSpace(documentTypeAlias)) + return items.Select(x => x.As(convertingType, convertedType)); + + return items + .Where(x => documentTypeAlias.InvariantEquals(x.DocumentTypeAlias)) + .Select(x => x.As(convertingType, convertedType)); + } + } + } } \ No newline at end of file diff --git a/src/Our.Umbraco.Ditto/Extensions/PublishedContentModelFactoryResolverExtensions.cs b/src/Our.Umbraco.Ditto/Extensions/PublishedContentModelFactoryResolverExtensions.cs index b2282e3..19773ad 100644 --- a/src/Our.Umbraco.Ditto/Extensions/PublishedContentModelFactoryResolverExtensions.cs +++ b/src/Our.Umbraco.Ditto/Extensions/PublishedContentModelFactoryResolverExtensions.cs @@ -4,15 +4,15 @@ namespace Our.Umbraco.Ditto { - public static class PublishedContentModelFactoryResolverExtensions - { - public static void SetFactory(this PublishedContentModelFactoryResolver resolver) - where T : IPublishedContent - { - var types = PluginManager.Current.ResolveTypes(); - var factory = new DittoPublishedContentModelFactory(types); + public static class PublishedContentModelFactoryResolverExtensions + { + public static void SetFactory(this PublishedContentModelFactoryResolver resolver) + where T : IPublishedContent + { + var types = PluginManager.Current.ResolveTypes(); + var factory = new DittoPublishedContentModelFactory(types); - resolver.SetFactory(factory); - } - } + resolver.SetFactory(factory); + } + } } \ No newline at end of file diff --git a/src/Our.Umbraco.Ditto/Extensions/RenderModelExtensions.cs b/src/Our.Umbraco.Ditto/Extensions/RenderModelExtensions.cs index 8cc574b..42b7653 100644 --- a/src/Our.Umbraco.Ditto/Extensions/RenderModelExtensions.cs +++ b/src/Our.Umbraco.Ditto/Extensions/RenderModelExtensions.cs @@ -4,20 +4,20 @@ namespace Our.Umbraco.Ditto { - public static class RenderModelExtensions - { - public static T As(this RenderModel model, - Action convertingType = null, - Action convertedType = null) - where T : RenderModel - { - if (model == null) - return default(T); + public static class RenderModelExtensions + { + public static T As(this RenderModel model, + Action convertingType = null, + Action convertedType = null) + where T : RenderModel + { + if (model == null) + return default(T); - using (DisposableTimer.DebugDuration(string.Format("RenderModel As ({0})", model.Content.DocumentTypeAlias))) - { - return model.Content.As(convertingType, convertedType); - } - } - } + using (DisposableTimer.DebugDuration(string.Format("RenderModel As ({0})", model.Content.DocumentTypeAlias))) + { + return model.Content.As(convertingType, convertedType); + } + } + } } \ No newline at end of file diff --git a/src/Our.Umbraco.Ditto/ModelFactory/DittoPublishedContentModelFactory.cs b/src/Our.Umbraco.Ditto/ModelFactory/DittoPublishedContentModelFactory.cs index 6409cdd..25356ab 100644 --- a/src/Our.Umbraco.Ditto/ModelFactory/DittoPublishedContentModelFactory.cs +++ b/src/Our.Umbraco.Ditto/ModelFactory/DittoPublishedContentModelFactory.cs @@ -7,51 +7,51 @@ namespace Our.Umbraco.Ditto { - public class DittoPublishedContentModelFactory : IPublishedContentModelFactory - { - private readonly Dictionary> _converters; - - public DittoPublishedContentModelFactory(IEnumerable types) - { - var converters = new Dictionary>(StringComparer.InvariantCultureIgnoreCase); - - foreach (var type in types.Where(x => typeof(IPublishedContent).IsAssignableFrom(x))) - { - Func func = (x) => - { - return x.As(type) as IPublishedContent; - }; - - var attribute = type.GetCustomAttribute(false); - var typeName = attribute == null ? type.Name : attribute.ContentTypeAlias; - - if (!converters.ContainsKey(typeName)) - converters.Add(typeName, func); - } - - _converters = converters.Count > 0 ? converters : null; - } - - public IPublishedContent CreateModel(IPublishedContent content) - { - if (_converters == null) - return content; - - var contentTypeAlias = content.DocumentTypeAlias; - Func converter; - - if (!_converters.TryGetValue(contentTypeAlias, out converter)) - return content; - - // HACK: [LK:2014-07-24] Using the `RequestCache` to store the result, as the model-factory can be called multiple times per request. - // The cache should only contain models have a corresponding converter, so in theory the result should be the same. - // Reason for caching, is that Ditto uses reflection to set property values, this can be a performance hit (especially when called multiple times). - return (IPublishedContent)ApplicationContext.Current.ApplicationCache.RequestCache.GetCacheItem( - string.Concat("DittoPublishedContentModelFactory.CreateModel_", content.Path), - () => - { - return converter(content); - }); - } - } + public class DittoPublishedContentModelFactory : IPublishedContentModelFactory + { + private readonly Dictionary> _converters; + + public DittoPublishedContentModelFactory(IEnumerable types) + { + var converters = new Dictionary>(StringComparer.InvariantCultureIgnoreCase); + + foreach (var type in types.Where(x => typeof(IPublishedContent).IsAssignableFrom(x))) + { + Func func = (x) => + { + return x.As(type) as IPublishedContent; + }; + + var attribute = type.GetCustomAttribute(false); + var typeName = attribute == null ? type.Name : attribute.ContentTypeAlias; + + if (!converters.ContainsKey(typeName)) + converters.Add(typeName, func); + } + + _converters = converters.Count > 0 ? converters : null; + } + + public IPublishedContent CreateModel(IPublishedContent content) + { + if (_converters == null) + return content; + + var contentTypeAlias = content.DocumentTypeAlias; + Func converter; + + if (!_converters.TryGetValue(contentTypeAlias, out converter)) + return content; + + // HACK: [LK:2014-07-24] Using the `RequestCache` to store the result, as the model-factory can be called multiple times per request. + // The cache should only contain models have a corresponding converter, so in theory the result should be the same. + // Reason for caching, is that Ditto uses reflection to set property values, this can be a performance hit (especially when called multiple times). + return (IPublishedContent)ApplicationContext.Current.ApplicationCache.RequestCache.GetCacheItem( + string.Concat("DittoPublishedContentModelFactory.CreateModel_", content.Path), + () => + { + return converter(content); + }); + } + } } \ No newline at end of file