-
Notifications
You must be signed in to change notification settings - Fork 795
/
FallbackRowFieldsProvider.cs
66 lines (57 loc) · 2.04 KB
/
FallbackRowFieldsProvider.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
namespace Serenity.Data;
/// <summary>
/// Fallback row fields provider for cases where a IServiceProvider
/// is not available.
/// </summary>
/// <seealso cref="IRowFieldsProvider" />
public class FallbackRowFieldsProvider : IRowFieldsProvider
{
/// <summary>
/// The instance
/// </summary>
public static FallbackRowFieldsProvider Instance = new();
private readonly ConcurrentDictionary<Type, RowFieldsBase> byType;
private readonly ConcurrentDictionary<(Type type, string alias),
RowFieldsBase> byTypeAndAlias;
private FallbackRowFieldsProvider()
{
byType = new ConcurrentDictionary<Type, RowFieldsBase>();
byTypeAndAlias = new ConcurrentDictionary<(Type, string), RowFieldsBase>();
}
/// <summary>
/// Resolves the specified fields type.
/// </summary>
/// <param name="fieldsType">Type of the fields.</param>
/// <returns></returns>
public RowFieldsBase Resolve(Type fieldsType)
{
return byType.GetOrAdd(fieldsType, CreateType);
}
/// <summary>
/// Resolves the with alias.
/// </summary>
/// <param name="fieldsType">Type of the fields.</param>
/// <param name="alias">The alias.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">alias</exception>
public RowFieldsBase ResolveWithAlias(Type fieldsType, string alias)
{
if (string.IsNullOrEmpty(alias))
throw new ArgumentNullException(nameof(alias));
return byTypeAndAlias.GetOrAdd((fieldsType, alias),
tuple => CreateType(tuple.type, tuple.alias));
}
private RowFieldsBase CreateType(Type fieldsType)
{
return CreateType(fieldsType, null);
}
private RowFieldsBase CreateType(Type fieldsType, string alias)
{
var fields = (RowFieldsBase)Activator.CreateInstance(fieldsType);
fields.Initialize(annotations: null, SqlSettings.DefaultDialect);
if (alias != null)
fields.ReplaceAliasWith(alias);
fields.LockAlias();
return fields;
}
}