forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerServices.cs
More file actions
104 lines (94 loc) · 3.65 KB
/
ControllerServices.cs
File metadata and controls
104 lines (94 loc) · 3.65 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace System.Web.Http.Controllers
{
/// <summary>
/// Represents a container for services that can be specific to a controller.
/// This shadows the services from its parent <see cref="ServicesContainer"/>. A controller can either set a service here, or fall through
/// to the more global set of services.
/// </summary>
public class ControllerServices : ServicesContainer
{
// This lists specific services that have been over ridden for the controller.
// Anything missing means just fall through and ask the _parent.
// This dictionary is only written at initialization time, and then read-only during steady state.
// So it can be safely read from multiple threads after initialization.
private Dictionary<Type, object> _overrideSingle;
private Dictionary<Type, List<object>> _overrideMulti;
private readonly ServicesContainer _parent;
public ControllerServices(ServicesContainer parent)
{
if (parent == null)
{
throw Error.ArgumentNull("parent");
}
_parent = parent;
}
public override bool IsSingleService(Type serviceType)
{
return _parent.IsSingleService(serviceType);
}
public override object GetService(Type serviceType)
{
if (_overrideSingle != null)
{
object item;
if (_overrideSingle.TryGetValue(serviceType, out item))
{
return item;
}
}
return _parent.GetService(serviceType);
}
public override IEnumerable<object> GetServices(Type serviceType)
{
if (_overrideMulti != null)
{
List<object> list;
if (_overrideMulti.TryGetValue(serviceType, out list))
{
return list;
}
}
return _parent.GetServices(serviceType);
}
/// <inheritdoc/>
protected override void ReplaceSingle(Type serviceType, object service)
{
if (_overrideSingle == null)
{
_overrideSingle = new Dictionary<Type, object>();
}
_overrideSingle[serviceType] = service;
}
/// <inheritdoc/>
protected override void ClearSingle(Type serviceType)
{
if (_overrideSingle == null)
{
return;
}
_overrideSingle.Remove(serviceType);
}
// This is called to request a mutation to services.
[SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "want a mutable list")]
protected override List<object> GetServiceInstances(Type serviceType)
{
if (_overrideMulti == null)
{
_overrideMulti = new Dictionary<Type, List<object>>();
}
List<object> list;
if (!_overrideMulti.TryGetValue(serviceType, out list))
{
// Copy parents list.
list = new List<object>(_parent.GetServices(serviceType));
// Copy into per-controller. If they're asking for the list, the expectation is that it's going to get mutated.
_overrideMulti[serviceType] = list;
}
return list;
}
}
}