forked from Avanade/Beef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseBase.cs
474 lines (409 loc) · 25.3 KB
/
DatabaseBase.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using Beef.Entities;
using Beef.Mapper;
using Beef.RefData;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
namespace Beef.Data.Database
{
/// <summary>
/// Represents the base class for encapsulating the database access layer using old skool ADO.NET - because sometimes it is all you need, and it is super efficient.
/// </summary>
/// <remarks>Provides automatic database connection management opening on first use and closing on <see cref="Dispose(bool)"/>.</remarks>
public abstract class DatabaseBase : IDatabase
{
private readonly object _lock = new object();
private DbConnection? _connection;
private bool _disposed;
private ILogger? _logger;
private DatabaseEventOutboxInvoker? _eventOutboxInvoker;
/// <summary>
/// Transforms and throws the <see cref="IBusinessException"/> equivalent for the <see cref="SqlException"/> known list.
/// </summary>
/// <param name="sex">The <see cref="SqlException"/>.</param>
public static void ThrowTransformedSqlException(SqlException sex)
{
if (sex == null)
throw new ArgumentNullException(nameof(sex));
var msg = sex.Message?.TrimEnd();
if (string.IsNullOrEmpty(msg))
msg = null;
switch (sex.Number)
{
case 56001: throw new ValidationException(msg, sex);
case 56002: throw new BusinessException(msg, sex);
case 56003: throw new AuthorizationException(msg, sex);
case 56004: throw new ConcurrencyException(msg, sex);
case 56005: throw new NotFoundException(msg, sex);
case 56006: throw new ConflictException(msg, sex);
case 56007: throw new DuplicateException(msg, sex);
default:
if (AlwaysCheckSqlDuplicateErrorNumbers && SqlDuplicateErrorNumbers.Contains(sex.Number))
throw new DuplicateException(null, sex);
break;
}
}
/// <summary>
/// Indicates whether to always check the <see cref="SqlDuplicateErrorNumbers"/> when executing the <see cref="ThrowTransformedSqlException(SqlException)"/> method.
/// </summary>
public static bool AlwaysCheckSqlDuplicateErrorNumbers { get; set; } = true;
/// <summary>
/// Gets or sets the list of known <see cref="SqlException.Number"/> values for the <see cref="ThrowTransformedSqlException(SqlException)"/> method.
/// </summary>
/// <remarks>See https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors
/// and https://docs.microsoft.com/en-us/azure/sql-database/sql-database-develop-error-messages </remarks>
public static List<int> SqlDuplicateErrorNumbers { get; } = new List<int>(new int[] { 2601, 2627 });
/// <summary>
/// Gets or sets the list of known <see cref="SqlException.Number"/> values that are considered transient; candidates for a retry.
/// </summary>
/// <remarks>See https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors
/// and https://docs.microsoft.com/en-us/azure/sql-database/sql-database-develop-error-messages </remarks>
public static List<int> SqlTransientErrorNumbers { get; } = new List<int>(new int[]
{
-1, -2, 701, 1204, 1205, 1222, 8645, 8651, 30053, // https://stackoverflow.com/questions/4821668/what-is-good-c-sharp-coding-style-for-catching-sqlexception-and-retrying
10928, 10929, 10053, 10054, 10060, 40540, 40143, 233, 64, // https://github.com/Azure/elastic-db-tools/blob/master/Src/ElasticScale.Client/ElasticScale.Common/TransientFaultHandling/Implementation/SqlDatabaseTransientErrorDetectionStrategy.cs
4060, 40197, 40501, 40613, 49918, 49919, 49920, 4221, // https://docs.microsoft.com/en-us/azure/sql-database/sql-database-develop-error-messages
1222, 1421, 1807, 3928, 5030, 7604, 8628, 8654, 17197, 17830, 17889, 18486 // https://github.com/marinoscar/CommonHelpers/blob/master/SqlErrorCodes.cs
});
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseBase"/> class for a <paramref name="connectionString"/> and <paramref name="provider"/>.
/// </summary>
/// <param name="connectionString">The connection string.</param>
/// <param name="provider">The optional data provider (e.g. Microsoft.Data.SqlClient); defaults to <see cref="SqlClientFactory"/>.</param>
/// <param name="invoker">Enables the <see cref="Invoker"/> to be overridden; defaults to <see cref="DatabaseInvoker"/>.</param>
protected DatabaseBase(string connectionString, DbProviderFactory? provider = null, DatabaseInvoker? invoker = null)
{
ConnectionString = !string.IsNullOrEmpty(connectionString) ? connectionString : throw new ArgumentNullException(nameof(connectionString));
Provider = provider ?? SqlClientFactory.Instance;
Invoker = invoker ?? new DatabaseInvoker();
}
/// <summary>
/// Gets the database connection string.
/// </summary>
public string ConnectionString { get; private set; }
/// <summary>
/// Gets the <see cref="DbProviderFactory"/>.
/// </summary>
public DbProviderFactory Provider { get; private set; }
/// <summary>
/// Gets the <see cref="DatabaseInvoker"/>.
/// </summary>
public DatabaseInvoker Invoker { get; private set; }
/// <summary>
/// Gets or sets the <see cref="DatabaseEventOutboxInvoker"/>. This defaults where not explicitly overridden.
/// </summary>
/// <returns>The <see cref="IDatabase.EventOutboxInvoker"/>.</returns>
public DatabaseEventOutboxInvoker EventOutboxInvoker
{
get => _eventOutboxInvoker ??= new DatabaseEventOutboxInvoker(this);
set
{
if (value != null && value.Database != this)
throw new ArgumentException("The DatabaseEventOutboxInvoker.Database property must be the same instance as this DatabaseBase instance.", nameof(EventOutboxInvoker));
_eventOutboxInvoker = value;
}
}
/// <summary>
/// Gets or sets the <see cref="DatabaseWildcard"/> to enable wildcard replacement.
/// </summary>
public DatabaseWildcard Wildcard { get; set; } = new DatabaseWildcard();
/// <summary>
/// Determines (overrides) the <see cref="Entities.DateTimeTransform"/> to be used when retrieving (see <see cref="DatabaseRecord.GetValue{DateTime}(string)"/>) a <see cref="DateTime"/> value
/// from a <see cref="DatabaseRecord"/>.
/// </summary>
public DateTimeTransform DateTimeTransform { get; set; } = DateTimeTransform.UseDefault;
/// <summary>
/// Gets or sets the <see cref="SqlException"/> handler (by default set up to execute <see cref="ThrowTransformedSqlException(SqlException)"/>).
/// </summary>
public Action<SqlException> ExceptionHandler { get; set; } = (sex) => ThrowTransformedSqlException(sex);
/// <summary>
/// Gets or sets the stored procedure name used by <see cref="SetSqlSessionContext(DbConnection)"/>; defaults to '[dbo].[spSetSessionContext]'.
/// </summary>
public string SessionContextStoredProcedure { get; set; } = "[dbo].[spSetSessionContext]";
/// <summary>
/// Gets the <see cref="ILogger"/>.
/// </summary>
internal ILogger Logger => _logger ??= Diagnostics.Logger.Create<DatabaseBase>();
/// <summary>
/// Gets the unique database instance identifier.
/// </summary>
public Guid DatabaseId { get; } = Guid.NewGuid();
/// <summary>
/// Sets the SQL session context using the specified values by invoking the <see cref="SessionContextStoredProcedure"/> using parameters named
/// <see cref="DatabaseColumns.SessionContextUsername"/> and <see cref="DatabaseColumns.SessionContextTimestamp"/>.
/// </summary>
/// <param name="dbConnection">The <see cref="DbConnection"/>.</param>
/// <param name="username">The username.</param>
/// <param name="timestamp">The timestamp <see cref="DateTime"/> (where <c>null</c> the value will default to <see cref="DateTime.Now"/>).</param>
/// <param name="tenantId">The tenant identifer (where <c>null</c> the value will not be used).</param>
/// <param name="userId">The unique user identifier.</param>
public void SetSqlSessionContext(DbConnection dbConnection, string username, DateTime? timestamp, Guid? tenantId = null, string? userId = null)
{
if (dbConnection == null)
throw new ArgumentNullException(nameof(dbConnection));
if (string.IsNullOrEmpty(SessionContextStoredProcedure))
throw new InvalidOperationException("The SessionContextStoredProcedure property must have a value.");
var cmd = dbConnection.CreateCommand();
cmd.CommandText = SessionContextStoredProcedure;
cmd.CommandType = CommandType.StoredProcedure;
var p = cmd.CreateParameter();
p.ParameterName = "@" + DatabaseColumns.SessionContextUsername;
p.Value = username;
cmd.Parameters.Add(p);
p = cmd.CreateParameter();
p.ParameterName = "@" + DatabaseColumns.SessionContextTimestamp;
p.Value = timestamp ?? Entities.Cleaner.Clean(DateTime.Now);
cmd.Parameters.Add(p);
if (tenantId.HasValue)
{
p = cmd.CreateParameter();
p.ParameterName = "@" + DatabaseColumns.SessionContextTenantId;
p.Value = tenantId.Value;
cmd.Parameters.Add(p);
}
if (!string.IsNullOrEmpty(userId))
{
p = cmd.CreateParameter();
p.ParameterName = "@" + DatabaseColumns.SessionContextUserId;
p.Value = userId;
cmd.Parameters.Add(p);
}
cmd.ExecuteNonQuery();
}
/// <summary>
/// Sets the SQL session context using the <see cref="ExecutionContext"/> (invokes <see cref="SetSqlSessionContext(DbConnection, string, DateTime?, Guid?, string?)"/> using
/// <see cref="ExecutionContext.Username"/>, <see cref="ExecutionContext.Timestamp"/> and <see cref="ExecutionContext.TenantId"/>).
/// </summary>
/// <param name="dbConnection">The <see cref="DbConnection"/>.</param>
public void SetSqlSessionContext(DbConnection dbConnection)
{
var ec = ExecutionContext.Current ?? throw new InvalidOperationException("The ExecutionContext.Current must have an instance to SetSqlSessionContext.");
SetSqlSessionContext(dbConnection, ec.Username, ec.Timestamp, ec.TenantId, ec.UserId);
}
/// <summary>
/// Gets the <see cref="DbConnection"/> (automatically creates and opens on first access, then closes when disposed).
/// </summary>
/// <returns>The underlying <see cref="DbConnection"/>.</returns>
public DbConnection GetConnection()
{
if (_connection != null)
return _connection;
lock (_lock)
{
if (_connection != null)
return _connection;
Logger.LogDebug("Creating and opening the database connection. DatabaseId: {0}", DatabaseId);
DbConnection conn = Provider.CreateConnection();
conn.ConnectionString = ConnectionString;
conn.Open();
OnConnectionOpen(conn);
return _connection = conn;
}
}
/// <summary>
/// Occurs when a connection is opened before any corresponding data access is performed.
/// </summary>
/// <param name="dbConnection">The <see cref="DbConnection"/>.</param>
/// <remarks>This is where the <see cref="SetSqlSessionContext(DbConnection)"/> should be invoked; nothing is performed by default.</remarks>
public virtual void OnConnectionOpen(DbConnection dbConnection) { }
/// <summary>
/// Creates a stored procedure <see cref="DatabaseCommand"/>.
/// </summary>
/// <param name="storedProcedure">The stored procedure name.</param>
/// <returns>A <see cref="DatabaseCommand"/>.</returns>
public DatabaseCommand StoredProcedure(string storedProcedure) => new DatabaseCommand(this, storedProcedure, CommandType.StoredProcedure);
/// <summary>
/// Creates a SQL statement <see cref="DatabaseCommand"/>.
/// </summary>
/// <param name="sqlStatement">The SQL statement.</param>
/// <returns>A <see cref="DatabaseCommand"/>.</returns>
public DatabaseCommand SqlStatement(string sqlStatement) => new DatabaseCommand(this, sqlStatement, CommandType.Text);
/// <summary>
/// Creates a <see cref="DatabaseQuery{T}"/> to enable select-like capabilities.
/// </summary>
/// <param name="queryArgs">The <see cref="DatabaseArgs{T}"/>.</param>
/// <param name="queryParams">The query <see cref="DatabaseParameters"/> delegate.</param>
/// <returns>A <see cref="DatabaseQuery{T}"/>.</returns>
public DatabaseQuery<T> Query<T>(DatabaseArgs<T> queryArgs, Action<DatabaseParameters>? queryParams = null) where T : class, new() => new DatabaseQuery<T>(this, queryArgs, queryParams);
/// <summary>
/// Gets the entity for the specified <paramref name="keys"/> mapping to <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <param name="getArgs">The <see cref="DatabaseArgs{T}"/>.</param>
/// <param name="keys">The key values.</param>
/// <returns>The entity value where found; otherwise, <c>null</c>.</returns>
public Task<T?> GetAsync<T>(DatabaseArgs<T> getArgs, params IComparable[] keys) where T : class, new()
{
if (getArgs == null)
throw new ArgumentNullException(nameof(getArgs));
return StoredProcedure(getArgs.StoredProcedure).Params((p) => getArgs.Mapper.GetKeyParams(p, OperationTypes.Get, keys)).SelectFirstOrDefaultAsync<T>(getArgs.Mapper);
}
/// <summary>
/// Performs a create for the specified stored procedure and value (reselects where specified).
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="saveArgs">The <see cref="DatabaseArgs{T}"/>.</param>
/// <param name="value">The value to insert.</param>
/// <returns>The value (reselected where specified).</returns>
/// <remarks>Automatically invokes <see cref="DatabaseParameters.AddChangeLogParameters(Entities.ChangeLog, bool, bool, ParameterDirection)"/>.</remarks>
public async Task<T> CreateAsync<T>(DatabaseArgs<T> saveArgs, T value) where T : class, new()
{
if (saveArgs == null)
throw new ArgumentNullException(nameof(saveArgs));
if (value == null)
throw new ArgumentNullException(nameof(value));
var cmd = StoredProcedure(saveArgs.StoredProcedure);
saveArgs.Mapper.GetKeyParams(cmd.Parameters, OperationTypes.Create, value);
cmd.Params(value, saveArgs.Mapper.MapToDb, OperationTypes.Create);
if (saveArgs.Refresh)
{
cmd.Param("@" + DatabaseColumns.ReselectRecordName, true);
return await cmd.SelectFirstOrDefaultAsync(saveArgs.Mapper).ConfigureAwait(false) ?? throw new NotFoundException();
}
// NOTE: without refresh, fields like IDs and RowVersion are not automatically updated.
await cmd.NonQueryAsync().ConfigureAwait(false);
return value;
}
/// <summary>
/// Performs an update for the specified stored procedure and value (reselects where specified).
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="saveArgs">The <see cref="DatabaseArgs{T}"/>.</param>
/// <param name="value">The value to update.</param>
/// <returns>The value (reselected where specified).</returns>
/// <remarks>Automatically invokes <see cref="DatabaseParameters.AddRowVersionParameter{T}(T, ParameterDirection)"/> and <see cref="DatabaseParameters.AddChangeLogParameters{T}(T, bool, bool, ParameterDirection)"/>.</remarks>
public async Task<T> UpdateAsync<T>(DatabaseArgs<T> saveArgs, T value) where T : class, new()
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (saveArgs == null)
throw new ArgumentNullException(nameof(saveArgs));
var cmd = StoredProcedure(saveArgs.StoredProcedure);
saveArgs.Mapper.GetKeyParams(cmd.Parameters, OperationTypes.Update, value);
cmd.Params(value, saveArgs.Mapper.MapToDb, OperationTypes.Update);
if (saveArgs.Refresh)
{
cmd.Param("@" + DatabaseColumns.ReselectRecordName, true);
return await cmd.SelectFirstOrDefaultAsync(saveArgs.Mapper).ConfigureAwait(false) ?? throw new NotFoundException();
}
// NOTE: without refresh, fields like IDs and RowVersion are not automatically updated.
await cmd.NonQueryAsync().ConfigureAwait(false);
return value;
}
/// <summary>
/// Performs a delete for the specified <paramref name="keys"/>.
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="saveArgs">The <see cref="DatabaseArgs{T}"/>.</param>
/// <param name="keys">The key values.</param>
public async Task DeleteAsync<T>(DatabaseArgs<T> saveArgs, params IComparable[] keys) where T : class, new()
{
if (saveArgs == null)
throw new ArgumentNullException(nameof(saveArgs));
var rowsAffected = await StoredProcedure(saveArgs.StoredProcedure).Params((p) => saveArgs.Mapper.GetKeyParams(p, OperationTypes.Delete, keys)).NonQueryAsync().ConfigureAwait(false);
if (rowsAffected == 0)
throw new NotFoundException();
}
/// <summary>
/// Executes a <b>ReferenceData</b> query updating the <paramref name="coll"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="ReferenceDataBase"/> <see cref="Type"/>.</typeparam>
/// <param name="coll">The <see cref="ReferenceDataCollectionBase{TItem}"/>.</param>
/// <param name="storedProcedure">The stored procedure name.</param>
/// <param name="idColumnName">The <see cref="ReferenceDataBase.Id"/> column name override; defaults to <see cref="DatabaseRefDataColumns.IdColumnName"/>.</param>
/// <param name="additionalProperties">The additional properties action that enables non-standard properties to be updated from the <see cref="DatabaseRecord"/>.</param>
/// <param name="additionalDatasetRecords">The additional dataset record delegates where additional datasets are returned.</param>
/// <param name="confirmItemIsToBeAdded">The action to confirm whether the item is to be added (defaults to <c>true</c>).</param>
public async Task GetRefDataAsync<TColl, TItem>(TColl coll, string storedProcedure, string? idColumnName = null,
Action<DatabaseRecord, TItem, DatabaseRecordFieldCollection>? additionalProperties = null,
Action<DatabaseRecord>[]? additionalDatasetRecords = null,
Func<DatabaseRecord, TItem, bool>? confirmItemIsToBeAdded = null)
where TColl : ReferenceDataCollectionBase<TItem>
where TItem : ReferenceDataBase, new()
{
Check.NotNull(coll, nameof(coll));
DatabaseRecordFieldCollection? fields = null;
var idCol = idColumnName ?? DatabaseRefDataColumns.IdColumnName;
var typeCode = ReferenceDataBase.GetIdTypeCode(typeof(TItem));
var list = new List<Action<DatabaseRecord>>
{
(dr) =>
{
if (fields == null)
{
fields = dr.GetFields();
if (!fields.Contains(idCol) || !fields.Contains(DatabaseRefDataColumns.CodeColumnName))
throw new InvalidOperationException("The query must return as a minimum the Id and Code columns as per the configured names.");
}
TItem item = new TItem()
{
Id = typeCode switch
{
ReferenceDataIdTypeCode.Guid => (object)dr.GetValue<Guid>(fields[idCol].Index),
ReferenceDataIdTypeCode.Int32 => (object)dr.GetValue<int>(fields[idCol].Index),
ReferenceDataIdTypeCode.Int64 => (object)dr.GetValue<long>(fields[idCol].Index),
_ => (object)dr.GetValue<string>(fields[idCol].Index)
},
Code = dr.GetValue<string>(fields[DatabaseRefDataColumns.CodeColumnName].Index),
Text = !fields.Contains(DatabaseRefDataColumns.TextColumnName) ? null : dr.GetValue<string>(fields[DatabaseRefDataColumns.TextColumnName].Index),
Description = !fields.Contains(DatabaseRefDataColumns.DescriptionColumnName) ? null : dr.GetValue<string>(fields[DatabaseRefDataColumns.DescriptionColumnName].Index),
SortOrder = !fields.Contains(DatabaseRefDataColumns.SortOrderColumnName) ? 0 : dr.GetValue<int>(fields[DatabaseRefDataColumns.SortOrderColumnName].Index),
IsActive = !fields.Contains(DatabaseRefDataColumns.IsActiveColumnName) || dr.GetValue<bool>(fields[DatabaseRefDataColumns.IsActiveColumnName].Index),
StartDate = !fields.Contains(DatabaseRefDataColumns.StartDateColumnName) ? null : dr.GetValue<DateTime?>(fields[DatabaseRefDataColumns.StartDateColumnName].Index),
EndDate = !fields.Contains(DatabaseRefDataColumns.EndDateColumnName) ? null : dr.GetValue<DateTime?>(fields[DatabaseRefDataColumns.EndDateColumnName].Index),
ETag = !fields.Contains(DatabaseRefDataColumns.ETagColumnName) ? null : dr.GetRowVersion(fields[DatabaseRefDataColumns.ETagColumnName].Index)
};
var cl = new Beef.Entities.ChangeLog
{
CreatedBy = !fields.Contains(DatabaseColumns.CreatedByName) ? null : dr.GetValue<string>(fields[DatabaseColumns.CreatedByName].Index),
CreatedDate = !fields.Contains(DatabaseColumns.CreatedDateName) ? (DateTime?)null : dr.GetValue<DateTime>(fields[DatabaseColumns.CreatedDateName].Index),
UpdatedBy = !fields.Contains(DatabaseColumns.UpdatedByName) ? null : dr.GetValue<string>(fields[DatabaseColumns.UpdatedByName].Index),
UpdatedDate = !fields.Contains(DatabaseColumns.UpdatedDateName) ? (DateTime?)null : dr.GetValue<DateTime>(fields[DatabaseColumns.UpdatedDateName].Index)
};
if (!cl.IsInitial)
item.ChangeLog = cl;
additionalProperties?.Invoke(dr, item, fields);
if (confirmItemIsToBeAdded == null || confirmItemIsToBeAdded(dr, item))
coll.Add(item);
}
};
if (additionalDatasetRecords != null && additionalDatasetRecords.Length > 0)
list.AddRange(additionalDatasetRecords);
await StoredProcedure(storedProcedure).SelectQueryMultiSetAsync(list.ToArray()).ConfigureAwait(false);
}
/// <summary>
/// Releases the unmanaged resources used by the <see cref="DatabaseBase"/> and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (_connection != null)
{
Logger.LogDebug("Closing and disposing the database connection. DatabaseId: {0}", DatabaseId);
_connection.Close();
_connection.Dispose();
}
}
_disposed = true;
}
}
/// <summary>
/// Closes and disposes the <see cref="DatabaseBase"/>.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}