-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBindingAttribute.cs
More file actions
166 lines (148 loc) · 6.19 KB
/
BindingAttribute.cs
File metadata and controls
166 lines (148 loc) · 6.19 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
namespace DigitalRuby.SimpleDi;
/// <summary>
/// Register a class for DI
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class BindingAttribute : Attribute
{
private static readonly ConcurrentDictionary<object, Type> factories = new();
/// <summary>
/// Service scope
/// </summary>
public ServiceLifetime Scope { get; }
/// <summary>
/// What to do if there is a conflict
/// </summary>
public ConflictResolution Conflict { get; }
/// <summary>
/// Control the order of processing, 0 is default value
/// </summary>
public int Order { get; init; }
/// <summary>
/// The interfaces to bind or null for all interfaces
/// </summary>
public IReadOnlyCollection<Type>? Interfaces { get; }
private static readonly IReadOnlyCollection<Type> globalInterfacesToIgnore = new HashSet<Type>
{
typeof(IDisposable),
typeof(IAsyncDisposable),
typeof(ICloneable),
typeof(IComparable),
typeof(IComparer),
typeof(IConvertible),
typeof(IEnumerable),
typeof(IEnumerator),
typeof(IEqualityComparer),
typeof(IEquatable<>),
typeof(IList),
typeof(IOrderedEnumerable<>),
typeof(IOrderedQueryable),
typeof(IOrderedQueryable<>),
typeof(IQueryable),
typeof(IQueryable<>),
typeof(IReadOnlyCollection<>),
typeof(IReadOnlyDictionary<,>),
typeof(IReadOnlyList<>),
typeof(IReadOnlySet<>),
typeof(ISet<>)
};
/// <summary>
/// Constructor
/// </summary>
/// <param name="scope">Lifetime of this service</param>
/// <param name="interfaces">The interfaces to bind, or pass null array for no interfaces, empty array for all interfaces</param>
public BindingAttribute(ServiceLifetime scope, params Type[]? interfaces)
{
Scope = scope;
Conflict = ConflictResolution.Add;
Interfaces = interfaces;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="scope">Lifetime of this service</param>
/// <param name="conflict">What to do if there is a conflict of multiple implementations for an interface</param>
/// <param name="interfaces">The interfaces to bind, or pass null array for no interfaces, empty array for all interfaces</param>
public BindingAttribute(ServiceLifetime scope, ConflictResolution conflict, params Type[]? interfaces)
{
Scope = scope;
Conflict = conflict;
Interfaces = interfaces;
}
/// <summary>
/// Bind a service given the life time
/// </summary>
/// <param name="services">Services</param>
/// <param name="type">Type of service</param>
/// <exception cref="System.InvalidOperationException">Hosted service that is not a singleton or a conflict resolution of Error and a binding already exists</exception>
public void BindServiceOfType(IServiceCollection services, Type type)
{
if (type.IsAbstract ||
(Scope != ServiceLifetime.Singleton && Scope != ServiceLifetime.Scoped && Scope != ServiceLifetime.Transient))
{
return;
}
if (Interfaces is not null && (Interfaces.Count == 0 || Interfaces.First() is not null))
{
var interfacesToBind = (Interfaces.Count == 0 ? type.GetInterfaces() : Interfaces);
foreach (var interfaceToBind in interfacesToBind)
{
if (!globalInterfacesToIgnore.Contains(interfaceToBind))
{
if (interfaceToBind == typeof(IHostedService))
{
if (Scope != ServiceLifetime.Singleton)
{
throw new InvalidOperationException("Hosted services should always be singletons");
}
}
var factory = (IServiceProvider provider) => provider.GetRequiredService(type);
factories[factory] = type;
var desc = new ServiceDescriptor(interfaceToBind, factory, Scope);
switch (Conflict)
{
case ConflictResolution.Add:
services.Add(desc);
break;
case ConflictResolution.Replace:
services.Replace(desc);
break;
case ConflictResolution.Skip:
services.TryAdd(desc);
break;
case ConflictResolution.Error:
var existing = services.FirstOrDefault(s => s.ServiceType == interfaceToBind);
if (existing is not null)
{
var impType = existing.ImplementationType?.FullName ?? existing.ImplementationInstance?.GetType().FullName;
if (string.IsNullOrWhiteSpace(impType) && existing.ImplementationFactory is not null)
{
if (factories.TryGetValue(existing.ImplementationFactory, out Type? foundTypeFactory))
{
impType = foundTypeFactory.FullName;
}
else
{
impType = "unknown";
}
}
var errorMessage = $"Concrete type {impType} is already bound to {interfaceToBind.FullName}, {type.FullName} cannot be bound";
throw new InvalidOperationException(errorMessage);
}
services.Add(desc);
break;
}
}
}
}
// register the concrete type
services.Add(new ServiceDescriptor(type, type, Scope));
}
/// <summary>
/// Clear all caches for the binding attribute
/// </summary>
internal static void Clear()
{
factories.Clear();
}
}