Skip to content

Commit

Permalink
Cache invalid entity names as well as valid details
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Mar 30, 2022
1 parent b7f582b commit 3fbce8d
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions MarkMpn.Sql4Cds.Engine/AttributeMetadataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class AttributeMetadataCache : IAttributeMetadataCache
private readonly ISet<string> _loading;
private readonly IDictionary<string, EntityMetadata> _minimalMetadata;
private readonly ISet<string> _minimalLoading;
private readonly IDictionary<string, Exception> _invalidEntities;

/// <summary>
/// Creates a new <see cref="AttributeMetadataCache"/>
Expand All @@ -31,6 +32,7 @@ public AttributeMetadataCache(IOrganizationService org)
_loading = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
_minimalMetadata = new Dictionary<string, EntityMetadata>(StringComparer.OrdinalIgnoreCase);
_minimalLoading = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
_invalidEntities = new Dictionary<string, Exception>(StringComparer.OrdinalIgnoreCase);
}

/// <inheritdoc cref="IAttributeMetadataCache.this{string}"/>
Expand All @@ -41,14 +43,25 @@ public EntityMetadata this[string name]
if (_metadata.TryGetValue(name, out var value))
return value;

var metadata = (RetrieveEntityResponse)_org.Execute(new RetrieveEntityRequest
if (_invalidEntities.TryGetValue(name, out var cachedEx))
throw cachedEx;

try
{
LogicalName = name.ToLowerInvariant(),
EntityFilters = EntityFilters.Attributes | EntityFilters.Relationships
});
var metadata = (RetrieveEntityResponse)_org.Execute(new RetrieveEntityRequest
{
LogicalName = name.ToLowerInvariant(),
EntityFilters = EntityFilters.Attributes | EntityFilters.Relationships
});

_metadata[name] = metadata.EntityMetadata;
return metadata.EntityMetadata;
_metadata[name] = metadata.EntityMetadata;
return metadata.EntityMetadata;
}
catch (Exception ex)
{
_invalidEntities[name] = ex;
throw;
}
}
}

Expand Down

0 comments on commit 3fbce8d

Please sign in to comment.