avoid repeated calls to GetObjectNamesForType in DependencyResolver

This commit is contained in:
Steve Bohlen
2015-02-03 02:10:54 -05:00
parent 4d8d84ba43
commit 01d4db4e51

View File

@@ -27,6 +27,7 @@ namespace Spring.Web.Mvc
private static readonly ILog logger = LogManager.GetLogger(typeof(SpringMvcDependencyResolver));
private readonly ConcurrentBag<Type> _nonResolvableTypes = new ConcurrentBag<Type>();
private readonly ConcurrentDictionary<Type, string> _resolvedNames = new ConcurrentDictionary<Type, string>();
/// <summary>
/// Initializes a new instance of the <see cref="SpringMvcDependencyResolver"/> class.
@@ -103,11 +104,20 @@ namespace Spring.Web.Mvc
}
else
{
// fall back to more expensive searching with type
var matchingServices = ApplicationContext.GetObjectNamesForType(serviceType);
if (matchingServices.Count > 0)
string resolvedName;
if (_resolvedNames.TryGetValue(serviceType, out resolvedName))
{
service = ApplicationContext.GetObject(matchingServices[0]);
service = ApplicationContext.GetObject(resolvedName);
}
else
{
// fall back to more expensive searching with type
var matchingServices = ApplicationContext.GetObjectNamesForType(serviceType);
if (matchingServices.Count > 0)
{
_resolvedNames.TryAdd(serviceType, matchingServices[0]);
service = ApplicationContext.GetObject(matchingServices[0]);
}
}
}