-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathMasaApp.cs
105 lines (80 loc) · 3.59 KB
/
MasaApp.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
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
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
namespace Masa.BuildingBlocks.Data;
public static class MasaApp
{
private static IServiceProvider? _rootServiceProvider;
public static IServiceProvider RootServiceProvider
{
get
{
if (_rootServiceProvider == null) Build();
return _rootServiceProvider!;
}
private set => _rootServiceProvider = value;
}
private static IServiceCollection Services { get; set; } = new ServiceCollection();
private static IEnumerable<Assembly>? Assemblies { get; set; }
/// <summary>
/// Global JsonSerializerOptions configuration
/// </summary>
private static JsonSerializerOptions? JsonSerializerOptions { get; set; }
public static void Build() => Build(Services.BuildServiceProvider());
public static void Build(IServiceProvider serviceProvider) => RootServiceProvider = serviceProvider;
public static TService? GetService<TService>()
=> GetService<TService>(RootServiceProvider);
public static TService? GetService<TService>(IServiceProvider serviceProvider)
=> serviceProvider.GetService<TService>();
public static TService GetRequiredService<TService>() where TService : notnull
=> GetRequiredService<TService>(RootServiceProvider);
public static TService GetRequiredService<TService>(IServiceProvider serviceProvider) where TService : notnull
=> serviceProvider.GetRequiredService<TService>();
public static void TrySetServiceCollection(IServiceCollection services)
{
if (Services.Count == 0) SetServiceCollection(services);
}
public static void SetServiceCollection(IServiceCollection services)
{
Services = services;
_rootServiceProvider = null;
}
public static IServiceCollection GetServices() => Services;
/// <summary>
/// Set the global Assembly collection (only if Assembly is not assigned a value)
/// </summary>
/// <param name="assemblies"></param>
public static void TrySetAssemblies(params Assembly[] assemblies)
{
ArgumentNullException.ThrowIfNull(assemblies);
Assemblies ??= assemblies;
}
/// <summary>
/// Set the global Assembly collection (only if Assembly is not assigned a value)
/// </summary>
/// <param name="assemblies"></param>
public static void TrySetAssemblies(IEnumerable<Assembly> assemblies)
{
ArgumentNullException.ThrowIfNull(assemblies);
Assemblies ??= assemblies.ToArray();
}
/// <summary>
/// Set the global Assembly collection
/// </summary>
/// <param name="assemblies"></param>
public static void SetAssemblies(params Assembly[] assemblies)
=> Assemblies = assemblies;
/// <summary>
/// Set the global Assembly collection
/// </summary>
/// <param name="assemblies"></param>
public static void SetAssemblies(IEnumerable<Assembly> assemblies)
=> Assemblies = assemblies;
public static IEnumerable<Assembly> GetAssemblies() => Assemblies ?? AppDomain.CurrentDomain.GetAssemblies();
public static void TrySetJsonSerializerOptions(JsonSerializerOptions jsonSerializerOptions)
{
if (JsonSerializerOptions == null) SetJsonSerializerOptions(jsonSerializerOptions);
}
public static void SetJsonSerializerOptions(JsonSerializerOptions jsonSerializerOptions)
=> JsonSerializerOptions = jsonSerializerOptions;
public static JsonSerializerOptions? GetJsonSerializerOptions() => JsonSerializerOptions;
}