GrainStoragePostConfigureOptions.cs
4.79 KB
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
106
107
108
109
110
111
112
113
114
115
116
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Orleans;
using Pole.Orleans.Provider.EntityframeworkCore.Conventions;
using System;
using System.Collections.Generic;
using System.Text;
namespace Pole.Orleans.Provider.EntityframeworkCore
{
public class GrainStoragePostConfigureOptions<TContext, TGrain, TGrainState, TEntity>
: IPostConfigureOptions<GrainStorageOptions<TContext, TGrain, TEntity>>
where TContext : DbContext
where TGrain : Grain<TGrainState>
where TGrainState : new()
where TEntity : class
{
public IGrainStorageConvention<TContext, TGrain, TEntity> Convention { get; }
public IGrainStorageConvention DefaultConvention { get; }
public GrainStoragePostConfigureOptions(IServiceProvider serviceProvider)
{
DefaultConvention =
(IGrainStorageConvention)serviceProvider.GetRequiredService(typeof(IGrainStorageConvention));
Convention = (IGrainStorageConvention<TContext, TGrain, TEntity>)
serviceProvider.GetService(typeof(IGrainStorageConvention<TContext, TGrain, TEntity>));
}
public void PostConfigure(string name, GrainStorageOptions<TContext, TGrain, TEntity> options)
{
if (!string.Equals(name, typeof(TGrain).FullName))
throw new Exception("Post configure on wrong grain type.");
if (options.IsPersistedFunc == null)
options.IsPersistedFunc =
DefaultConvention.CreateIsPersistedFunc<TEntity>(options);
// Configure ETag
if (options.ShouldUseETag)
{
if (!string.IsNullOrWhiteSpace(options.ETagPropertyName))
DefaultConvention.ConfigureETag(options.ETagPropertyName, options);
}
if (options.ReadStateAsync == null)
{
if (options.DbSetAccessor == null)
options.DbSetAccessor = Convention?.CreateDefaultDbSetAccessorFunc()
?? DefaultConvention.CreateDefaultDbSetAccessorFunc<TContext, TEntity>();
if (Convention != null)
Convention.SetDefaultKeySelector(options);
else
DefaultConvention.SetDefaultKeySelectors(options);
if (options.PreCompileReadQuery)
{
options.ReadStateAsync
= Convention?.CreatePreCompiledDefaultReadStateFunc(options)
?? DefaultConvention
.CreatePreCompiledDefaultReadStateFunc(options);
}
else
{
options.ReadStateAsync
= Convention?.CreateDefaultReadStateFunc()
?? DefaultConvention
.CreateDefaultReadStateFunc(options);
}
}
if (options.ReadStateNoTrackingAsync == null)
{
if (options.DbSetNoTrackingAccessor == null)
options.DbSetNoTrackingAccessor = Convention?.CreateDefaultDbSetNoTrackingAccessorFunc()
?? DefaultConvention.CreateDefaultDbSetNoTrackingAccessorFunc<TContext, TEntity>();
if (Convention != null)
Convention.SetDefaultKeySelector(options);
else
DefaultConvention.SetDefaultKeySelectors(options);
if (options.PreCompileReadQuery)
{
options.ReadStateNoTrackingAsync
= Convention?.CreatePreCompiledDefaultReadStateNoTrackingFunc(options)
?? DefaultConvention
.CreatePreCompiledDefaultReadStateNoTrackingFunc(options);
}
else
{
options.ReadStateNoTrackingAsync
= Convention?.CreateDefaultReadStateFunc()
?? DefaultConvention
.CreateDefaultReadStateFunc(options);
}
}
if (options.SetEntity == null)
options.SetEntity =
Convention?.GetSetterFunc()
?? DefaultConvention.GetSetterFunc<TGrainState, TEntity>();
if (options.GetEntity == null)
options.GetEntity =
Convention?.GetGetterFunc()
?? DefaultConvention.GetGetterFunc<TGrainState, TEntity>();
DefaultConvention.FindAndConfigureETag(options, options.ShouldUseETag);
// todo: Validate options
options.IsConfigured = true;
}
}
}