Skip to content

Commit 01d4db4

Browse files
committed
avoid repeated calls to GetObjectNamesForType in DependencyResolver
1 parent 4d8d84b commit 01d4db4

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/Spring/Spring.Web.Mvc3/SpringMvcDependencyResolver.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class SpringMvcDependencyResolver : IDependencyResolver
2727

2828
private static readonly ILog logger = LogManager.GetLogger(typeof(SpringMvcDependencyResolver));
2929
private readonly ConcurrentBag<Type> _nonResolvableTypes = new ConcurrentBag<Type>();
30+
private readonly ConcurrentDictionary<Type, string> _resolvedNames = new ConcurrentDictionary<Type, string>();
3031

3132
/// <summary>
3233
/// Initializes a new instance of the <see cref="SpringMvcDependencyResolver"/> class.
@@ -103,11 +104,20 @@ public object GetService(Type serviceType)
103104
}
104105
else
105106
{
106-
// fall back to more expensive searching with type
107-
var matchingServices = ApplicationContext.GetObjectNamesForType(serviceType);
108-
if (matchingServices.Count > 0)
107+
string resolvedName;
108+
if (_resolvedNames.TryGetValue(serviceType, out resolvedName))
109109
{
110-
service = ApplicationContext.GetObject(matchingServices[0]);
110+
service = ApplicationContext.GetObject(resolvedName);
111+
}
112+
else
113+
{
114+
// fall back to more expensive searching with type
115+
var matchingServices = ApplicationContext.GetObjectNamesForType(serviceType);
116+
if (matchingServices.Count > 0)
117+
{
118+
_resolvedNames.TryAdd(serviceType, matchingServices[0]);
119+
service = ApplicationContext.GetObject(matchingServices[0]);
120+
}
111121
}
112122
}
113123

0 commit comments

Comments
 (0)