-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ModularApplicationContext.cs
67 lines (58 loc) · 1.88 KB
/
ModularApplicationContext.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
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace OrchardCore.Modules
{
public interface IApplicationContext
{
Application Application { get; }
}
public class ModularApplicationContext : IApplicationContext
{
private readonly IHostEnvironment _environment;
private readonly IEnumerable<IModuleNamesProvider> _moduleNamesProviders;
private Application _application;
private static readonly object _initLock = new();
public ModularApplicationContext(IHostEnvironment environment, IEnumerable<IModuleNamesProvider> moduleNamesProviders)
{
_environment = environment;
_moduleNamesProviders = moduleNamesProviders;
}
public Application Application
{
get
{
EnsureInitialized();
return _application;
}
}
private void EnsureInitialized()
{
if (_application == null)
{
lock (_initLock)
{
_application ??= new Application(_environment, GetModules());
}
}
}
private IEnumerable<Module> GetModules()
{
var modules = new ConcurrentBag<Module>
{
new Module(_environment.ApplicationName, true),
};
var names = _moduleNamesProviders
.SelectMany(p => p.GetModuleNames())
.Where(n => n != _environment.ApplicationName)
.Distinct();
Parallel.ForEach(names, new ParallelOptions { MaxDegreeOfParallelism = 8 }, (name) =>
{
modules.Add(new Module(name, false));
});
return modules;
}
}
}