-
Notifications
You must be signed in to change notification settings - Fork 1
/
Context.cs
313 lines (275 loc) · 9.08 KB
/
Context.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
#region Related components
using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Transactions;
using net.vieapps.Components.Utility;
#endregion
namespace net.vieapps.Components.Repository
{
/// <summary>
/// Context for holding the transaction and state while processing
/// </summary>
[DebuggerDisplay("Operation = {Operation}, Type = {EntityDefinition.Type.FullName}")]
public class RepositoryContext : IDisposable
{
#region Properties
/// <summary>
/// Gets the identity of the context
/// </summary>
public string ID { get; internal set; } = UtilityService.NewUUID;
/// <summary>
/// Gets the operation of the context
/// </summary>
public RepositoryOperation Operation
{
get;
#if DEBUG
set;
#else
internal set;
#endif
}
/// <summary>
/// Gets the entity definition of the context
/// </summary>
public EntityDefinition EntityDefinition
{
get;
#if DEBUG
set;
#else
internal set;
#endif
}
/// <summary>
/// Gets the alias type name of the context (if got alias type name, means working with diffirent data source because the module is alias of other module)
/// </summary>
public string AliasTypeName
{
get;
#if DEBUG
set;
#else
internal set;
#endif
}
/// <summary>
/// Gets the state that determines to use transaction or don't use
/// </summary>
public bool UseTransaction { get; internal set; } = false;
/// <summary>
/// Gets the exception that got while processing
/// </summary>
public Exception Exception { get; internal set; }
internal ConcurrentDictionary<string, Dictionary<string, object>> PreviousStateData { get; set; }
internal ConcurrentDictionary<string, Dictionary<string, object>> CurrentStateData { get; set; }
internal TransactionScope SqlTransaction { get; set; }
internal MongoDB.Driver.IClientSessionHandle NoSqlSession { get; set; }
#endregion
#region Prepare
internal void Prepare(RepositoryOperation operation = RepositoryOperation.Query, EntityDefinition entityDefinition = null, string aliasTypeName = null, MongoDB.Driver.IClientSessionHandle nosqlSession = null)
{
this.ID = this.ID ?? UtilityService.NewUUID;
this.PreviousStateData = this.PreviousStateData ?? new ConcurrentDictionary<string, Dictionary<string, object>>(StringComparer.OrdinalIgnoreCase);
this.CurrentStateData = this.CurrentStateData ?? new ConcurrentDictionary<string, Dictionary<string, object>>(StringComparer.OrdinalIgnoreCase);
this.Operation = operation;
if (entityDefinition != null)
this.EntityDefinition = entityDefinition;
if (!string.IsNullOrWhiteSpace(aliasTypeName))
this.AliasTypeName = aliasTypeName;
if (this.UseTransaction)
{
this.NoSqlSession = this.NoSqlSession ?? nosqlSession;
this.StartTransaction();
}
}
internal void Prepare<T>(RepositoryOperation operation = RepositoryOperation.Query, MongoDB.Driver.IClientSessionHandle nosqlSession = null) where T : class
=> this.Prepare(operation, RepositoryMediator.GetEntityDefinition<T>(), null, nosqlSession);
#endregion
#region Constructors & Destructors
/// <summary>
/// Creates new context for working with repositories
/// </summary>
/// <param name="useTransaction">true to use transaction with default settings (async flow is enabled); false to not</param>
/// <param name="nosqlSession">The client session of NoSQL database</param>
public RepositoryContext(bool useTransaction = true, MongoDB.Driver.IClientSessionHandle nosqlSession = null) : this(null, useTransaction, nosqlSession) { }
/// <summary>
/// Creates new context for working with repositories
/// </summary>
/// <param name="useTransaction">true to use transaction with default settings (async flow is enabled); false to not</param>
/// <param name="nosqlSession">The client session of NoSQL database</param>
public RepositoryContext(EntityDefinition entityDefinition, bool useTransaction = true, MongoDB.Driver.IClientSessionHandle nosqlSession = null)
{
if (entityDefinition != null)
this.EntityDefinition = entityDefinition;
this.UseTransaction = useTransaction;
this.NoSqlSession = nosqlSession;
this.Prepare();
}
public void Dispose()
{
this.EndTransaction();
GC.SuppressFinalize(this);
}
~RepositoryContext()
=> this.Dispose();
#endregion
#region Start/Commit/Abort Transaction
/// <summary>
/// Starts the transaction
/// </summary>
/// <param name="transactionOptions"></param>
public void StartTransaction(MongoDB.Driver.TransactionOptions transactionOptions = null)
{
if (this.UseTransaction)
{
this.SqlTransaction = this.SqlTransaction ?? SqlHelper.CreateTransaction();
this.NoSqlSession?.StartTransaction(transactionOptions);
}
}
/// <summary>
/// Commits the transaction (indicates that all operations within the context are completed successfully)
/// </summary>
public void CommitTransaction()
{
if (this.UseTransaction)
{
this.NoSqlSession?.CommitTransaction();
this.SqlTransaction?.Complete();
}
}
/// <summary>
/// Aborts the transaction
/// </summary>
public void AbortTransaction()
{
if (this.UseTransaction)
this.NoSqlSession?.AbortTransaction();
}
/// <summary>
/// Ends the transaction and marks this context is completed
/// </summary>
public void EndTransaction()
{
try
{
if (this.Exception == null)
this.CommitTransaction();
else
this.AbortTransaction();
}
catch (ObjectDisposedException) { }
catch (TransactionAbortedException)
{
if (this.Exception != null)
throw;
}
catch (Exception)
{
throw;
}
finally
{
this.SqlTransaction?.Dispose();
this.SqlTransaction = null;
this.NoSqlSession?.Dispose();
this.NoSqlSession = null;
}
}
#endregion
#region State data
internal Dictionary<string, object> GetStateData<T>(T @object) where T : class
{
// initialize
var stateData = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
if (@object == null)
return stateData;
// standard properties && fields
var entityDefinition = RepositoryMediator.GetEntityDefinition<T>();
entityDefinition.Attributes.ForEach(attribute =>
{
try
{
stateData[attribute.Name] = @object.GetAttributeValue(attribute);
}
catch { }
});
// extended properties
if (@object is IBusinessEntity businessEntity)
businessEntity?.ExtendedProperties?.ForEach(kvp =>
{
try
{
stateData[$"ExtendedProperties.{kvp.Key}"] = kvp.Value;
}
catch { }
});
return stateData;
}
internal Dictionary<string, object> SetPreviousState<T>(T @object, Dictionary<string, object> stateData = null) where T : class
{
stateData = stateData ?? this.GetStateData(@object);
this.PreviousStateData[@object.GetCacheKey(true)] = stateData;
return stateData;
}
/// <summary>
/// Gets the previous state of the object
/// </summary>
/// <param name="object">The object that need to get previous state</param>
/// <returns></returns>
public Dictionary<string, object> GetPreviousState<T>(T @object) where T : class
=> @object != null && this.PreviousStateData.TryGetValue(@object.GetCacheKey(true), out var stateData) ? stateData : null;
internal Dictionary<string, object> SetCurrentState<T>(T @object, Dictionary<string, object> stateData = null) where T : class
{
stateData = stateData ?? this.GetStateData(@object);
this.CurrentStateData[@object.GetCacheKey(true)] = stateData;
return stateData;
}
/// <summary>
/// Gets the current state of the object
/// </summary>
/// <param name="object">The object that need to get current state</param>
/// <returns></returns>
public Dictionary<string, object> GetCurrentState<T>(T @object) where T : class
=> @object != null
? this.CurrentStateData.TryGetValue(@object.GetCacheKey(true), out var stateData)
? stateData
: this.SetCurrentState(@object)
: null;
/// <summary>
/// Finds the dirty attributes (means the changed attributes)
/// </summary>
/// <param name="previousStateData">The previous state</param>
/// <param name="currentStateData">The current state</param>
/// <returns>The collection of dirty attributes (means the collection of changed attributes)</returns>
public HashSet<string> FindDirty(Dictionary<string, object> previousStateData, Dictionary<string, object> currentStateData)
{
if (currentStateData == null || currentStateData.Count < 0)
return new HashSet<string>();
else if (previousStateData == null || previousStateData.Count < 0)
return currentStateData.Keys.ToHashSet(false);
var dirtyAttributes = new HashSet<string>();
currentStateData.ForEach(kvp =>
{
previousStateData.TryGetValue(kvp.Key, out var previousState);
if (kvp.Value == null)
{
if (previousState != null)
dirtyAttributes.Add(kvp.Key);
}
else
{
if (previousState == null)
dirtyAttributes.Add(kvp.Key);
else if (!kvp.Value.Equals(previousState))
dirtyAttributes.Add(kvp.Key);
}
});
return dirtyAttributes;
}
#endregion
}
}