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

Commit

Permalink
AttributedTypeResolver - Adds lock to prevent concurrency issue
Browse files Browse the repository at this point in the history
Second attempt to fix #238
  • Loading branch information
leekelleher committed Sep 28, 2018
1 parent 619b9d5 commit 597e20b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected DittoProcessorAttribute()
{
if (Ditto.TryGetTypeAttribute(this.GetType(), out DittoProcessorMetaDataAttribute metaData, true) == false || metaData == null)
{
throw new ApplicationException("Ditto processor attributes require a DittoProcessorMetaData attribute to be applied to the class but none was found.");
throw new ApplicationException($"The Ditto processor attribute ('{this.GetType()}') requires a DittoProcessorMetaData attribute to be applied to the class but none was found.");
}

this.ValueType = metaData.ValueType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ namespace Our.Umbraco.Ditto
/// <typeparam name="TAttribute">A specific Ditto attribute type.</typeparam>
internal sealed class AttributedTypeResolver<TAttribute> where TAttribute : Attribute
{
private readonly ConcurrentDictionary<Type, TAttribute> _attributedTypeLookup;
private readonly object _lock = new object();

private readonly Dictionary<Type, TAttribute> _attributedTypeLookup;

private AttributedTypeResolver()
{
_attributedTypeLookup = new ConcurrentDictionary<Type, TAttribute>();
_attributedTypeLookup = new Dictionary<Type, TAttribute>();
}

private void Initialize(IEnumerable<Type> types, bool inherit = false)
{
if (types != null)
// NOTE: Lock when initializing the resolver, so that it can't be read from (at the same time).
lock (_lock)
{
foreach (var type in types)
if (types != null)
{
TryAddAttributedType(type, out TAttribute attribute, inherit);
foreach (var type in types)
{
TryAddAttributedType(type, out TAttribute attribute, inherit);
}
}
}
}
Expand Down Expand Up @@ -83,7 +89,8 @@ private bool TryAddAttributedType(Type type, out TAttribute attribute, bool inhe
{
if (_attributedTypeLookup.ContainsKey(type) == false)
{
return _attributedTypeLookup.TryAdd(type, attribute);
_attributedTypeLookup.Add(type, attribute);
return true;
}
else
{
Expand All @@ -103,14 +110,18 @@ private bool TryAddAttributedType(Type type, out TAttribute attribute, bool inhe
/// <returns>Returns the associated attribute for the given object-type.</returns>
public bool TryGetTypeAttribute(Type type, out TAttribute attribute, bool inherit = false)
{
bool result = _attributedTypeLookup.TryGetValue(type, out attribute);

if (result == false)
// NOTE: Lock when looking up from the resolver, to avoid concurrency issues.
lock (_lock)
{
result = TryAddAttributedType(type, out attribute, inherit);
}
bool result = _attributedTypeLookup.TryGetValue(type, out attribute);

return result;
if (result == false)
{
result = TryAddAttributedType(type, out attribute, inherit);
}

return result;
}
}
}
}

0 comments on commit 597e20b

Please sign in to comment.