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

Commit 6dbf878

Browse files
committed
Minor code amends
- DittoChainContext - renamed parameter vars - DittoCacheableAttribute - references the local `cacheKeyBuilderType` as this could be different than the instance property value. - UmbracoPropertiesAttribute - corrected a mistake with the `Inherited` bool value. - UmbracoPropertyAttribute - removed the dependency on Umbraco.Core `IsNullOrWhiteSpace()` extension method. This incurred extra allocations. - MockPublishedContent - switched from `SingleOrDefault` to `FirstOrDefault` - to reduce the number of iterations of the lookup.
1 parent dae3f09 commit 6dbf878

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

src/Our.Umbraco.Ditto/ComponentModel/Attributes/DittoCacheableAttribute.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ protected DittoCacheableAttribute()
4949
/// <param name="cacheContext">The cache context.</param>
5050
/// <param name="refresher">The refresher.</param>
5151
/// <returns>Returns the output type.</returns>
52-
/// <exception cref="System.ApplicationException">Expected a cache key builder of type + typeof(DittoProcessorCacheKeyBuilder) + but got + CacheKeyBuilderType</exception>
52+
/// <exception cref="ApplicationException">Expected a cache key builder of type DittoProcessorCacheKeyBuilder but got CacheKeyBuilderType.</exception>
5353
internal TOuputType GetCacheItem<TOuputType>(DittoCacheContext cacheContext, Func<TOuputType> refresher)
5454
{
55+
// TODO: [LK:2018-01-18] Review this, does `cacheContext` need to be passed in?
56+
// Given that the values are available on the instance.
57+
5558
// If no cache duration set, (and in debug mode AND NOT a unit-test), then just run the refresher
5659
if (this.CacheDuration == 0 || (Ditto.IsDebuggingEnabled && Ditto.IsRunningInUnitTest == false))
5760
{
@@ -64,7 +67,7 @@ internal TOuputType GetCacheItem<TOuputType>(DittoCacheContext cacheContext, Fun
6467
// Check the cache key builder type
6568
if (typeof(DittoCacheKeyBuilder).IsAssignableFrom(cacheKeyBuilderType) == false)
6669
{
67-
throw new ApplicationException($"Expected a cache key builder of type {typeof(DittoCacheKeyBuilder)} but got {this.CacheKeyBuilderType}");
70+
throw new ApplicationException($"Expected a cache key builder of type {typeof(DittoCacheKeyBuilder)} but got {cacheKeyBuilderType}");
6871
}
6972

7073
// Construct the cache key builder

src/Our.Umbraco.Ditto/ComponentModel/Processors/Contexts/DittoChainContext.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,26 @@ internal ProcessorContextsCollection(IEnumerable<DittoProcessorContext> processo
4848
/// Adds a range of processor contexts to the collection chain.
4949
/// </summary>
5050
/// <param name="ctxs">An enumerable of processor contexts.</param>
51-
public void AddRange(IEnumerable<DittoProcessorContext> ctxs)
51+
public void AddRange(IEnumerable<DittoProcessorContext> contexts)
5252
{
53-
if (ctxs == null)
53+
if (contexts == null)
5454
{
5555
return;
5656
}
5757

58-
foreach (var ctx in ctxs)
58+
foreach (var context in contexts)
5959
{
60-
Add(ctx);
60+
Add(context);
6161
}
6262
}
6363

6464
/// <summary>
6565
/// Adds a processor context to the collection chain.
6666
/// </summary>
6767
/// <param name="ctx">The processor context.</param>
68-
public void Add(DittoProcessorContext ctx)
68+
public void Add(DittoProcessorContext context)
6969
{
70-
_processorContexts.AddOrUpdate(ctx.GetType(), ctx, (type, ctx2) => ctx2); // Don't override if already exists
70+
_processorContexts.AddOrUpdate(context.GetType(), context, (type, ctx) => ctx); // Don't override if already exists
7171
}
7272

7373
/// <summary>

src/Our.Umbraco.Ditto/ComponentModel/Processors/UmbracoPropertiesAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Our.Umbraco.Ditto
55
/// <summary>
66
/// The Umbraco properties attribute.
77
/// </summary>
8-
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
8+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
99
public sealed class UmbracoPropertiesAttribute : Attribute
1010
{
1111
/// <summary>

src/Our.Umbraco.Ditto/ComponentModel/Processors/UmbracoPropertyAttribute.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Linq;
3-
using System.Reflection;
4-
using Umbraco.Core;
53
using Umbraco.Core.Logging;
64
using Umbraco.Core.Models;
75
using Umbraco.Web;
@@ -133,14 +131,14 @@ public override object ProcessValue()
133131
}
134132

135133
// Try fetching the alt value.
136-
if ((propertyValue == null || propertyValue.ToString().IsNullOrWhiteSpace())
134+
if ((propertyValue == null || (propertyValue is string tmp && string.IsNullOrWhiteSpace(tmp)))
137135
&& string.IsNullOrWhiteSpace(altUmbracoPropertyName) == false)
138136
{
139137
propertyValue = GetPropertyValue(content, altUmbracoPropertyName, recursive);
140138
}
141139

142140
// Try setting the default value.
143-
if ((propertyValue == null || propertyValue.ToString().IsNullOrWhiteSpace())
141+
if ((propertyValue == null || (propertyValue is string tmp2 && string.IsNullOrWhiteSpace(tmp2)))
144142
&& defaultValue != null)
145143
{
146144
propertyValue = defaultValue;
@@ -165,13 +163,13 @@ protected object GetPropertyValue(IPublishedContent content, string umbracoPrope
165163
propertyValue = GetClassPropertyValue(content, umbracoPropertyName);
166164
}
167165

168-
if ((propertyValue == null || propertyValue.ToString().IsNullOrWhiteSpace())
166+
if ((propertyValue == null || (propertyValue is string tmp && string.IsNullOrWhiteSpace(tmp)))
169167
&& (PropertySource != PropertySource.InstanceProperties))
170168
{
171169
propertyValue = GetUmbracoPropertyValue(content, umbracoPropertyName, recursive);
172170
}
173171

174-
if ((propertyValue == null || propertyValue.ToString().IsNullOrWhiteSpace())
172+
if ((propertyValue == null || (propertyValue is string tmp2 && string.IsNullOrWhiteSpace(tmp2)))
175173
&& PropertySource == PropertySource.UmbracoThenInstanceProperties)
176174
{
177175
propertyValue = GetClassPropertyValue(content, umbracoPropertyName);
@@ -194,10 +192,10 @@ private object GetClassPropertyValue(IPublishedContent content, string umbracoPr
194192
{
195193
if (Ditto.IsDebuggingEnabled
196194
&& PropertySource == PropertySource.InstanceThenUmbracoProperties
197-
&& Ditto.IPublishedContentProperties.Any(x => x.Name.InvariantEquals(umbracoPropertyName))
195+
&& Ditto.IPublishedContentProperties.Any(x => string.Equals(x.Name, umbracoPropertyName, StringComparison.InvariantCultureIgnoreCase))
198196
&& content.HasProperty(umbracoPropertyName))
199197
{
200-
// Property is an IPublishedContent property and an umbraco property exists so warn the user
198+
// Property is an IPublishedContent property and an Umbraco property exists so warn the user
201199
LogHelper.Warn<UmbracoPropertyAttribute>($"The property {umbracoPropertyName} being mapped from content type {contentType.Name}'s instance properties hides a property in the Umbraco properties collection of the same name. It is recommended that you avoid using Umbraco property aliases that conflict with IPublishedContent instance property names, but if you can't avoid this and you require access to the hidden property you can use the PropertySource parameter of the processors attribute to override the order in which properties are checked.");
202200
}
203201

@@ -219,11 +217,11 @@ private object GetUmbracoPropertyValue(IPublishedContent content, string umbraco
219217
{
220218
if (Ditto.IsDebuggingEnabled
221219
&& PropertySource == PropertySource.UmbracoThenInstanceProperties
222-
&& Ditto.IPublishedContentProperties.Any(x => x.Name.InvariantEquals(umbracoPropertyName))
220+
&& Ditto.IPublishedContentProperties.Any(x => string.Equals(x.Name, umbracoPropertyName, StringComparison.InvariantCultureIgnoreCase))
223221
&& content.HasProperty(umbracoPropertyName))
224222
{
225-
// Property is an IPublishedContent property and an umbraco property exists so warn the user
226-
LogHelper.Warn<UmbracoPropertyAttribute>($"The property {umbracoPropertyName} being mapped from the Umbraco properties collection hides an instance property of the same name on content type {content.GetType().Name}. It is recommended that you avoid using Umbraco property aliases that conflict with IPublishedContent instance property names, but if you can't avoid this and you require access to the hidden property you can use the PropertySource parameter of the processors attribute to override the order in which properties are checked.");
223+
// Property is an IPublishedContent property and an Umbraco property exists so warn the user
224+
LogHelper.Warn<UmbracoPropertyAttribute>($"The property {umbracoPropertyName} being mapped from the Umbraco properties collection hides an instance property of the same name on content type {content}. It is recommended that you avoid using Umbraco property aliases that conflict with IPublishedContent instance property names, but if you can't avoid this and you require access to the hidden property you can use the PropertySource parameter of the processors attribute to override the order in which properties are checked.");
227225
}
228226

229227
return content.GetPropertyValue(umbracoPropertyName, recursive);

tests/Our.Umbraco.Ditto.Tests/Mocks/MockPublishedContent.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Collections.ObjectModel;
44
using System.Linq;
55
using Moq;
6-
using Umbraco.Core;
76
using Umbraco.Core.Logging;
87
using Umbraco.Core.Models;
98
using Umbraco.Core.Models.PublishedContent;
@@ -50,7 +49,7 @@ public IPublishedProperty GetProperty(string alias)
5049

5150
public IPublishedProperty GetProperty(string alias, bool recurse)
5251
{
53-
var prop = Properties.SingleOrDefault(p => p.PropertyTypeAlias.InvariantEquals(alias));
52+
var prop = Properties.FirstOrDefault(p => string.Equals(p.PropertyTypeAlias, alias, StringComparison.InvariantCultureIgnoreCase));
5453

5554
if (prop == null && recurse && Parent != null)
5655
{

0 commit comments

Comments
 (0)