-
Notifications
You must be signed in to change notification settings - Fork 1
/
Starter.cs
325 lines (299 loc) · 17 KB
/
Starter.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#region Related components
using System;
using System.Xml;
using System.Linq;
using System.Reflection;
using System.Configuration;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using net.vieapps.Components.Utility;
#endregion
namespace net.vieapps.Components.Repository
{
public static class RepositoryStarter
{
/// <summary>
/// Initializes all types of the assembly
/// </summary>
/// <param name="assembly">The assembly for initializing</param>
/// <param name="tracker">The tracker for tracking logs</param>
public static void Initialize(Assembly assembly, Action<string, Exception> tracker = null)
{
try
{
var types = assembly.GetExportedTypes();
tracker?.Invoke($"Initialize the assembly: {assembly.GetName().Name} with {types.Length} exported type(s)", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"Initialize the assembly: {assembly.GetName().Name} with {types.Length} exported type(s)", null);
// repositories
types.Where(type => type.IsDefined<RepositoryAttribute>(false)).ForEach(type =>
{
tracker?.Invoke($"Register the repository: {type.GetTypeName()}", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"Register the repository: {type.GetTypeName()}", null);
RepositoryDefinition.Register(type, tracker);
});
// entities
types.Where(type => type.IsDefined<EntityAttribute>(false)).ForEach(type =>
{
tracker?.Invoke($"Register the repository entity: {type.GetTypeName()}", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"Register the repository entity: {type.GetTypeName()}", null);
EntityDefinition.Register(type);
});
// event handlers
types.Where(type => type.IsDefined<EventHandlersAttribute>(false))
.Where(type => typeof(IPreCreateHandler).IsAssignableFrom(type) || typeof(IPostCreateHandler).IsAssignableFrom(type)
|| typeof(IPreGetHandler).IsAssignableFrom(type) || typeof(IPostGetHandler).IsAssignableFrom(type)
|| typeof(IPreUpdateHandler).IsAssignableFrom(type) || typeof(IPostUpdateHandler).IsAssignableFrom(type)
|| typeof(IPreDeleteHandler).IsAssignableFrom(type) || typeof(IPostDeleteHandler).IsAssignableFrom(type))
.ForEach(type =>
{
tracker?.Invoke($"Register the event-handler: {type.GetTypeName()}", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"Register the event-handler: {type.GetTypeName()}", null);
RepositoryMediator.EventHandlers.Add(type);
});
}
catch (ReflectionTypeLoadException ex)
{
if (ex.LoaderExceptions.FirstOrDefault(e => e is System.IO.FileNotFoundException) == null)
{
tracker?.Invoke($"Error occurred while initializing the assembly: {assembly.GetName().Name}", ex);
if (tracker == null)
RepositoryMediator.WriteLogs($"Error occurred while initializing the assembly: {assembly.GetName().Name}", ex);
ex.LoaderExceptions.ForEach(exception => tracker?.Invoke(null, exception));
throw;
}
}
catch (Exception ex)
{
tracker?.Invoke($"Error occurred while initializing the assembly: {assembly.GetName().Name}", ex);
if (tracker == null)
RepositoryMediator.WriteLogs($"Error occurred while initializing the assembly: {assembly.GetName().Name}", ex);
throw;
}
}
/// <summary>
/// Initializes all types in assemblies
/// </summary>
/// <param name="assemblies">The collection of assemblies</param>
/// <param name="tracker">The tracker for tracking logs</param>
/// <param name="updateFromConfigurationFile">true to update other settings from configuration file on the disc</param>
/// <param name="config">The XML node that contains configuration</param>
public static void Initialize(IEnumerable<Assembly> assemblies, Action<string, Exception> tracker = null, bool updateFromConfigurationFile = true, XmlNode config = null)
{
tracker?.Invoke($"Start to initialize repositories & entities [{assemblies.Select(a => a.GetName().Name).ToString(", ")}]", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"Start to initialize repositories & entities [{assemblies.Select(a => a.GetName().Name).ToString(", ")}]");
// initialize & register all types
assemblies.ForEach(assembly => RepositoryStarter.Initialize(assembly, tracker));
// read configuration
if (updateFromConfigurationFile && config == null)
{
if (ConfigurationManager.GetSection(UtilityService.GetAppSetting("Section:Repositories", "net.vieapps.repositories")) is AppConfigurationSectionHandler configSection)
config = configSection.Section;
if (config == null)
throw new ConfigurationErrorsException("Cannot find the configuration section (might be named as 'net.vieapps.repositories') in the configuration file");
}
// update configuration
if (config != null)
try
{
// update settings of data sources
if (config.SelectNodes("dataSources/dataSource") is XmlNodeList dataSourceNodes)
RepositoryMediator.ConstructDataSources(dataSourceNodes.ToList(), tracker);
// update settings of repositories
if (updateFromConfigurationFile && config.SelectNodes("repository") is XmlNodeList repositoryNodes)
repositoryNodes.ToList().ForEach(repositoryNode =>
{
// update repository
var settingsJson = repositoryNode.ToJson();
tracker?.Invoke($"Update settings of a repository => {settingsJson}", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"Update settings of a repository => {settingsJson}");
RepositoryDefinition.Update(settingsJson, tracker);
// update repository entities
if (repositoryNode.SelectNodes("entity") is XmlNodeList entityNodes)
entityNodes.ToList().ForEach(repositoryEntityNode =>
{
settingsJson = repositoryEntityNode.ToJson();
tracker?.Invoke($"Update settings of a repository entity => {settingsJson}", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"Update settings of a repository entity => {settingsJson}");
EntityDefinition.Update(settingsJson, tracker);
});
});
// default data sources
RepositoryMediator.DefaultVersionDataSourceName = config.Attributes["versionDataSource"]?.Value;
RepositoryMediator.DefaultTrashDataSourceName = config.Attributes["trashDataSource"]?.Value;
// ensure schemas (SQL)
if ("true".IsEquals(config.Attributes["ensureSchemas"]?.Value))
Task.Run(async () => await RepositoryMediator.EntityDefinitions.ForEachAsync(async definition =>
{
var primaryDataSource = RepositoryMediator.GetPrimaryDataSource(null, definition);
primaryDataSource = primaryDataSource != null && primaryDataSource.Mode.Equals(RepositoryMode.SQL)
? primaryDataSource
: null;
await RepositoryStarter.EnsureSqlSchemasAsync(definition, primaryDataSource, tracker).ConfigureAwait(false);
var secondaryDataSource = RepositoryMediator.GetSecondaryDataSource(null, definition);
secondaryDataSource = secondaryDataSource != null && secondaryDataSource.Mode.Equals(RepositoryMode.SQL)
? secondaryDataSource
: null;
await RepositoryStarter.EnsureSqlSchemasAsync(definition, secondaryDataSource, tracker).ConfigureAwait(false);
await RepositoryMediator.GetSyncDataSources(null, definition)
.Where(dataSource => dataSource.Mode.Equals(RepositoryMode.SQL) && !dataSource.Name.IsEquals(primaryDataSource?.Name) && !dataSource.Name.IsEquals(secondaryDataSource?.Name))
.ForEachAsync(async dataSource => await RepositoryStarter.EnsureSqlSchemasAsync(definition, dataSource, tracker).ConfigureAwait(false), true, false)
.ConfigureAwait(false);
}, true, false)).ConfigureAwait(false);
// ensure indexes (NoSQL)
if ("true".IsEquals(config.Attributes["ensureIndexes"]?.Value))
Task.Run(async () => await RepositoryMediator.EntityDefinitions.ForEachAsync(async definition =>
{
var primaryDataSource = RepositoryMediator.GetPrimaryDataSource(null, definition);
primaryDataSource = primaryDataSource != null && primaryDataSource.Mode.Equals(RepositoryMode.NoSQL)
? primaryDataSource
: null;
await RepositoryStarter.EnsureNoSqlIndexesAsync(definition, primaryDataSource, tracker).ConfigureAwait(false);
var secondaryDataSource = RepositoryMediator.GetSecondaryDataSource(null, definition);
secondaryDataSource = secondaryDataSource != null && secondaryDataSource.Mode.Equals(RepositoryMode.NoSQL)
? secondaryDataSource
: null;
await RepositoryStarter.EnsureNoSqlIndexesAsync(definition, secondaryDataSource, tracker).ConfigureAwait(false);
await RepositoryMediator.GetSyncDataSources(null, definition)
.Where(dataSource => dataSource.Mode.Equals(RepositoryMode.NoSQL) && !dataSource.Name.IsEquals(primaryDataSource?.Name) && !dataSource.Name.IsEquals(secondaryDataSource?.Name))
.ForEachAsync(async dataSource => await RepositoryStarter.EnsureNoSqlIndexesAsync(definition, dataSource, tracker).ConfigureAwait(false), true, false)
.ConfigureAwait(false);
}, true, false)).ConfigureAwait(false);
}
catch (Exception ex)
{
tracker?.Invoke($"Error occurred while updating the repository => {ex.Message}", ex);
if (tracker == null)
RepositoryMediator.WriteLogs($"Error occurred while updating the repository => {ex.Message}", ex);
throw;
}
else
{
tracker?.Invoke("No configuration to update", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs("No configuration to update");
}
tracker?.Invoke($"Total of registered repositories: {RepositoryMediator.RepositoryDefinitions.Count}", null);
tracker?.Invoke($"Total of registered repository entities: {RepositoryMediator.EntityDefinitions.Count}", null);
tracker?.Invoke($"Total of registered event-handlers: {RepositoryMediator.EventHandlers.Count}", null);
tracker?.Invoke($"Total of registered data-sources: {RepositoryMediator.DataSources.Count}", null);
tracker?.Invoke($"Default data-source for storing version contents: {RepositoryMediator.DefaultVersionDataSourceName ?? "(None)"}", null);
tracker?.Invoke($"Default data-source for storing trash contents: {RepositoryMediator.DefaultTrashDataSourceName ?? "(None)"}", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
{
RepositoryMediator.WriteLogs($"Total of registered repositories: {RepositoryMediator.RepositoryDefinitions.Count}", null);
RepositoryMediator.WriteLogs($"Total of registered repository entities: {RepositoryMediator.EntityDefinitions.Count}", null);
RepositoryMediator.WriteLogs($"Total of registered event-handlers: {RepositoryMediator.EventHandlers.Count}", null);
RepositoryMediator.WriteLogs($"Total of registered data-sources: {RepositoryMediator.DataSources.Count}", null);
RepositoryMediator.WriteLogs($"Default data-source for storing version contents: {RepositoryMediator.DefaultVersionDataSourceName ?? "(None)"}", null);
RepositoryMediator.WriteLogs($"Default data-source for storing trash contents: {RepositoryMediator.DefaultTrashDataSourceName ?? "(None)"}", null);
}
}
/// <summary>
/// Initializes all types of all assemblies of the current app
/// </summary>
/// <param name="selector">The function to select assemblies to initialize</param>
/// <param name="tracker">The tracker for tracking logs</param>
public static void Initialize(Func<IEnumerable<Assembly>> selector = null, Action<string, Exception> tracker = null)
=> RepositoryStarter.Initialize(selector != null
? selector()
: new[] { Assembly.GetCallingAssembly() }.Concat(Assembly.GetCallingAssembly()
.GetReferencedAssemblies()
.Where(an => !an.Name.IsStartsWith("api-ms") && !an.Name.IsStartsWith("clr") && !an.Name.IsStartsWith("mscor") && !an.Name.IsStartsWith("sos") && !an.Name.IsStartsWith("lib")
&& !an.Name.IsStartsWith("System") && !an.Name.IsStartsWith("Microsoft") && !an.Name.IsStartsWith("Windows") && !an.Name.IsEquals("NETStandard")
&& !an.Name.IsStartsWith("Newtonsoft") && !an.Name.IsStartsWith("WampSharp") && !an.Name.IsStartsWith("StackExchange.")
&& !an.Name.IsStartsWith("Serilog") && !an.Name.IsStartsWith("MsgPack") && !an.Name.IsStartsWith("ExcelData")
&& !an.Name.IsStartsWith("MongoDB") && !an.Name.IsStartsWith("MySql") && !an.Name.IsStartsWith("Npgsql")
&& !an.Name.IsEndsWith(".Abstractions") && !an.Name.IsStartsWith("VIEApps.Components")
)
.Select(an => Assembly.Load(an))
),
tracker
);
/// <summary>
/// Constructs data-sources
/// </summary>
/// <param name="datasourceNodes"></param>
/// <param name="tracker"></param>
public static void ConstructDataSources(List<XmlNode> datasourceNodes, Action<string, Exception> tracker = null)
=> RepositoryMediator.ConstructDataSources(datasourceNodes, tracker);
/// <summary>
/// Constructs data-sources
/// </summary>
/// <param name="datasourceNodes"></param>
/// <param name="tracker"></param>
public static void ConstructDataSources(XmlNodeList datasourceNodes, Action<string, Exception> tracker = null)
=> RepositoryStarter.ConstructDataSources(datasourceNodes.ToList(), tracker);
/// <summary>
/// Constructs SQL database factory providers
/// </summary>
/// <param name="dbProviderFactoryNodes"></param>
/// <param name="tracker"></param>
public static void ConstructDbProviderFactories(List<XmlNode> dbProviderFactoryNodes, Action<string, Exception> tracker = null)
=> DbProvider.ConstructDbProviderFactories(dbProviderFactoryNodes, tracker);
/// <summary>
/// Constructs SQL database factory providers
/// </summary>
/// <param name="dbProviderFactoryNodes"></param>
/// <param name="tracker"></param>
public static void ConstructDbProviderFactories(XmlNodeList dbProviderFactoryNodes, Action<string, Exception> tracker = null)
=> RepositoryStarter.ConstructDbProviderFactories(dbProviderFactoryNodes.ToList(), tracker);
/// <summary>
/// Ensures schemas of an entity in SQL database
/// </summary>
/// <param name="definition"></param>
/// <param name="dataSource"></param>
/// <param name="tracker"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task EnsureSqlSchemasAsync(EntityDefinition definition, DataSource dataSource, Action<string, Exception> tracker = null, CancellationToken cancellationToken = default)
{
if (definition != null && dataSource != null && dataSource.Mode.Equals(RepositoryMode.SQL))
try
{
tracker?.Invoke($"Ensure schemas of SQL: {definition.Type} [{dataSource.Name} @ {dataSource.Mode} => {definition.TableName}]", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"Ensure schemas of SQL: {definition.Type} [{dataSource.Name} @ {dataSource.Mode} => {definition.TableName}]", null);
await definition.EnsureSchemasAsync(dataSource, tracker, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
tracker?.Invoke($"Error occurred while ensuring schemas of SQL: {definition.Type} [{dataSource.Name} @ {dataSource.Mode} => {definition.TableName}]", ex);
if (tracker == null)
RepositoryMediator.WriteLogs($"Error occurred while ensuring schemas of SQL: {definition.Type} [{dataSource.Name} @ {dataSource.Mode} => {definition.TableName}]", ex);
}
}
/// <summary>
/// Ensures indexes of an entity in No SQL database
/// </summary>
/// <param name="definition"></param>
/// <param name="dataSource"></param>
/// <param name="tracker"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task EnsureNoSqlIndexesAsync(EntityDefinition definition, DataSource dataSource, Action<string, Exception> tracker = null, CancellationToken cancellationToken = default)
{
if (definition != null && dataSource != null && dataSource.Mode.Equals(RepositoryMode.NoSQL))
try
{
tracker?.Invoke($"Ensure indexes of No SQL: {definition.Type} [{dataSource.Name} @ {dataSource.Mode} => {definition.CollectionName}]", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"Ensure indexes of No SQL: {definition.Type} [{dataSource.Name} @ {dataSource.Mode} => {definition.CollectionName}]", null);
await definition.EnsureIndexesAsync(dataSource, tracker, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
tracker?.Invoke($"Error occurred while ensuring indexes of No SQL: {definition.Type} [{dataSource.Name} @ {dataSource.Mode} => {definition.CollectionName}]", ex);
if (tracker == null)
RepositoryMediator.WriteLogs($"Cannot ensure indexes of No SQL: {definition.Type} [{dataSource.Name} @ {dataSource.Mode} => {definition.CollectionName}]", ex);
}
}
}
}