Skip to content

Commit

Permalink
Added type caching to DefaultTypeResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
roryprimrose committed Apr 3, 2020
1 parent 41c6a8f commit fd1028d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions ModelBuilder/DefaultTypeResolver.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace ModelBuilder
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand All @@ -12,6 +11,8 @@
/// </summary>
public class DefaultTypeResolver : ITypeResolver
{
private Dictionary<Type, Type> _typeCache = new Dictionary<Type, Type>();

/// <inheritdoc />
public Type GetBuildType(IBuildConfiguration configuration, Type requestedType)
{
Expand All @@ -37,7 +38,16 @@ public Type GetBuildType(IBuildConfiguration configuration, Type requestedType)
if (requestedType.IsInterface
|| requestedType.IsAbstract)
{
return AutoResolveBuildType(requestedType);
if (_typeCache.ContainsKey(requestedType))
{
return _typeCache[requestedType];
}

var resolvedType = AutoResolveBuildType(requestedType);

_typeCache[requestedType] = resolvedType;

return resolvedType;
}

return requestedType;
Expand Down Expand Up @@ -151,8 +161,11 @@ private static Type AutoResolveBuildType(Type requestedType)
// Next give priority to a derived class name that has the same name as the requested type (without "Base" suffix for abstract classes)
var abstractNameToMatch = requestedType.Name;

#if NETSTANDARD2_0
abstractNameToMatch = abstractNameToMatch.Replace("Base", string.Empty);

#else
abstractNameToMatch = abstractNameToMatch.Replace("Base", string.Empty, StringComparison.CurrentCulture);
#endif
var abstractNameMatchingType = matchingTypes.FirstOrDefault(x => x.Name == abstractNameToMatch);

if (abstractNameMatchingType != null)
Expand Down

0 comments on commit fd1028d

Please sign in to comment.