This repository has been archived by the owner on Nov 25, 2019. It is now read-only.
/
HttpControllerTypeCache.cs
69 lines (57 loc) · 2.55 KB
/
HttpControllerTypeCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
namespace System.Web.Http.Dispatcher
{
/// <summary>
/// Manages a cache of <see cref="System.Web.Http.Controllers.IHttpController"/> types detected in the system.
/// </summary>
internal sealed class HttpControllerTypeCache
{
private readonly HttpConfiguration _configuration;
private readonly Lazy<Dictionary<string, ILookup<string, Type>>> _cache;
public HttpControllerTypeCache(HttpConfiguration configuration)
{
if (configuration == null)
{
throw Error.ArgumentNull("configuration");
}
_configuration = configuration;
_cache = new Lazy<Dictionary<string, ILookup<string, Type>>>(InitializeCache);
}
internal Dictionary<string, ILookup<string, Type>> Cache
{
get { return _cache.Value; }
}
public ICollection<Type> GetControllerTypes(string controllerName)
{
if (String.IsNullOrEmpty(controllerName))
{
throw Error.ArgumentNullOrEmpty("controllerName");
}
HashSet<Type> matchingTypes = new HashSet<Type>();
ILookup<string, Type> namespaceLookup;
if (_cache.Value.TryGetValue(controllerName, out namespaceLookup))
{
foreach (var namespaceGroup in namespaceLookup)
{
matchingTypes.UnionWith(namespaceGroup);
}
}
return matchingTypes;
}
private Dictionary<string, ILookup<string, Type>> InitializeCache()
{
IAssembliesResolver assembliesResolver = _configuration.Services.GetAssembliesResolver();
IHttpControllerTypeResolver controllersResolver = _configuration.Services.GetHttpControllerTypeResolver();
ICollection<Type> controllerTypes = controllersResolver.GetControllerTypes(assembliesResolver);
var groupedByName = controllerTypes.GroupBy(
t => t.Name.Substring(0, t.Name.Length - DefaultHttpControllerSelector.ControllerSuffix.Length),
StringComparer.OrdinalIgnoreCase);
return groupedByName.ToDictionary(
g => g.Key,
g => g.ToLookup(t => t.Namespace ?? String.Empty, StringComparer.OrdinalIgnoreCase),
StringComparer.OrdinalIgnoreCase);
}
}
}