-
Notifications
You must be signed in to change notification settings - Fork 795
/
RowFieldsProvider.cs
103 lines (92 loc) · 3.81 KB
/
RowFieldsProvider.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
using Microsoft.Extensions.DependencyInjection;
using System.Threading;
namespace Serenity.Data;
/// <summary>
/// Contains extension methods and settings for row fields providers
/// </summary>
public static class RowFieldsProvider
{
private static IRowFieldsProvider defaultProvider;
private static readonly AsyncLocal<IRowFieldsProvider> localProvider;
static RowFieldsProvider()
{
defaultProvider = FallbackRowFieldsProvider.Instance;
localProvider = new AsyncLocal<IRowFieldsProvider>();
}
/// <summary>
/// Gets current row fields provider. Returns async local provider if available,
/// otherwise the default provider.
/// </summary>
public static IRowFieldsProvider Current => localProvider.Value ?? defaultProvider;
/// <summary>
/// Sets default row fields provider. This instance is required
/// as rows might have to be created in contexts where dependency injection
/// is not possible, like deserialization. If using a DI container,
/// set this at startup to the same singleton service you register with DI.
/// </summary>
/// <param name="provider">Provider. Required.</param>
/// <returns>Old default provider</returns>
public static IRowFieldsProvider SetDefault(IRowFieldsProvider provider)
{
var old = defaultProvider;
defaultProvider = provider ?? throw new ArgumentNullException(nameof(provider));
return old;
}
/// <summary>
/// Sets default row fields provider by resolving it from the service provider.
/// </summary>
/// <param name="services">Services. Required.</param>
/// <returns>Old default provider</returns>
public static IRowFieldsProvider SetDefaultFrom(IServiceProvider services)
{
return SetDefault((services ?? throw new ArgumentNullException(nameof(services)))
.GetRequiredService<IRowFieldsProvider>());
}
/// <summary>
/// Sets local row fields provider for current thread and async context.
/// Useful for background tasks, async methods, and testing to set provider locally and for
/// auto spawned threads.
/// </summary>
/// <param name="provider">Row fields provider. Can be null.</param>
/// <returns>Old local provider if any.</returns>
public static IRowFieldsProvider SetLocal(IRowFieldsProvider provider)
{
var old = localProvider.Value;
localProvider.Value = provider;
return old;
}
/// <summary>
/// Sets local row fields provider by resolving it from the service provider.
/// </summary>
/// <param name="services">Services. Required.</param>
/// <returns>Old default provider</returns>
public static IRowFieldsProvider SetLocalFrom(IServiceProvider services)
{
return SetLocal((services ?? throw new ArgumentNullException(nameof(services)))
.GetRequiredService<IRowFieldsProvider>());
}
/// <summary>
/// Resolves a fields class using current row fields provider
/// </summary>
/// <typeparam name="TFields">The type of the fields.</typeparam>
/// <returns></returns>
public static TFields Resolve<TFields>()
where TFields : RowFieldsBase
{
return (TFields)Current.Resolve(typeof(TFields));
}
/// <summary>
/// Resolves an aliased fields class using current row fields provider
/// </summary>
/// <typeparam name="TFields">The type of the fields.</typeparam>
/// <param name="alias">Alias for fields</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">alias</exception>
public static TFields Resolve<TFields>(string alias)
where TFields : RowFieldsBase
{
if (alias == null)
throw new ArgumentNullException(nameof(alias));
return (TFields)Current.ResolveWithAlias(typeof(TFields), alias);
}
}