-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TypeFeatureProvider.cs
55 lines (46 loc) · 2.02 KB
/
TypeFeatureProvider.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using OrchardCore.Environment.Extensions.Features;
namespace OrchardCore.Environment.Extensions
{
public class TypeFeatureProvider : ITypeFeatureProvider
{
private readonly ConcurrentDictionary<Type, IEnumerable<IFeatureInfo>> _features = new();
public IExtensionInfo GetExtensionForDependency(Type dependency)
{
if (_features.TryGetValue(dependency, out var features))
{
return features.First().Extension;
}
throw new InvalidOperationException($"Could not resolve extension for type {dependency.Name}.");
}
public IFeatureInfo GetFeatureForDependency(Type dependency)
{
if (_features.TryGetValue(dependency, out var features))
{
// Gets the first feature that has the same ID as the extension, if any.
// Otherwise returns the first feature.
return features.FirstOrDefault(feature => feature.Extension.Id == feature.Id) ?? features.First();
}
throw new InvalidOperationException($"Could not resolve main feature for type {dependency.Name}.");
}
public IEnumerable<IFeatureInfo> GetFeaturesForDependency(Type dependency)
{
if (_features.TryGetValue(dependency, out var features))
{
return features;
}
throw new InvalidOperationException($"Could not resolve features for type {dependency.Name}.");
}
public IEnumerable<Type> GetTypesForFeature(IFeatureInfo feature)
{
return _features.Where(kv => kv.Value.Contains(feature)).Select(kv => kv.Key);
}
public void TryAdd(Type type, IFeatureInfo feature)
{
_features.AddOrUpdate(type, (key, value) => [value], (key, features, value) => features.Contains(value) ? features : features.Append(value).ToArray(), feature);
}
}
}