Skip to content

Commit c633f97

Browse files
committedSep 8, 2024
Update DbContextBulkTransaction code style
1 parent a31213f commit c633f97

File tree

1 file changed

+60
-47
lines changed

1 file changed

+60
-47
lines changed
 

‎EFCore.BulkExtensions.Core/DbContextBulkTransaction.cs

+60-47
Original file line numberDiff line numberDiff line change
@@ -12,97 +12,110 @@ internal static class DbContextBulkTransaction
1212
{
1313
public static void Execute<T>(DbContext context, Type? type, IEnumerable<T> entities, OperationType operationType, BulkConfig? bulkConfig, Action<decimal>? progress) where T : class
1414
{
15-
SqlAdaptersMapping.ProviderName = context.Database.ProviderName;
15+
UpdateSqlAdaptersProps(context);
1616

1717
type ??= typeof(T);
1818

1919
using (ActivitySources.StartExecuteActivity(operationType, entities.Count()))
2020
{
21-
if (entities.Count() == 0 &&
22-
operationType != OperationType.InsertOrUpdateOrDelete &&
23-
operationType != OperationType.Truncate &&
24-
operationType != OperationType.SaveChanges &&
25-
(bulkConfig == null || bulkConfig.CustomSourceTableName == null) &&
26-
(bulkConfig == null || bulkConfig.DataReader == null))
27-
{
28-
return;
29-
}
21+
if (!IsValidTransaction(entities, operationType, bulkConfig)) return;
3022

3123
if (operationType == OperationType.SaveChanges)
3224
{
3325
DbContextBulkTransactionSaveChanges.SaveChanges(context, bulkConfig, progress);
3426
return;
3527
}
36-
else if (bulkConfig?.IncludeGraph == true)
28+
29+
if (bulkConfig?.IncludeGraph == true)
3730
{
3831
DbContextBulkTransactionGraphUtil.ExecuteWithGraph(context, entities, operationType, bulkConfig, progress);
32+
return;
3933
}
40-
else
41-
{
42-
TableInfo tableInfo = TableInfo.CreateInstance(context, type, entities, operationType, bulkConfig);
4334

44-
if (operationType == OperationType.Insert && !tableInfo.BulkConfig.SetOutputIdentity && tableInfo.BulkConfig.CustomSourceTableName == null)
45-
{
35+
var tableInfo = TableInfo.CreateInstance(context, type, entities, operationType, bulkConfig);
36+
37+
switch (operationType)
38+
{
39+
case OperationType.Insert when tableInfo.BulkConfig is { SetOutputIdentity: false, CustomSourceTableName: null }:
4640
SqlBulkOperation.Insert(context, type, entities, tableInfo, progress);
47-
}
48-
else if (operationType == OperationType.Read)
49-
{
41+
break;
42+
43+
case OperationType.Read:
5044
SqlBulkOperation.Read(context, type, entities, tableInfo, progress);
51-
}
52-
else if (operationType == OperationType.Truncate)
53-
{
45+
break;
46+
47+
case OperationType.Truncate:
5448
SqlBulkOperation.Truncate(context, tableInfo);
55-
}
56-
else
57-
{
49+
break;
50+
51+
default:
5852
SqlBulkOperation.Merge(context, type, entities, tableInfo, operationType, progress);
59-
}
53+
break;
6054
}
6155
}
6256
}
6357

6458
public static async Task ExecuteAsync<T>(DbContext context, Type? type, IEnumerable<T> entities, OperationType operationType, BulkConfig? bulkConfig, Action<decimal>? progress, CancellationToken cancellationToken = default) where T : class
6559
{
66-
SqlAdaptersMapping.ProviderName = context.Database.ProviderName;
60+
UpdateSqlAdaptersProps(context);
6761

6862
type ??= typeof(T);
6963

7064
using (ActivitySources.StartExecuteActivity(operationType, entities.Count()))
7165
{
72-
if (entities.Count() == 0 && operationType != OperationType.InsertOrUpdateOrDelete && operationType != OperationType.Truncate && operationType != OperationType.SaveChanges)
73-
{
74-
return;
75-
}
66+
if (!IsValidTransaction(entities, operationType, bulkConfig)) return;
7667

7768
if (operationType == OperationType.SaveChanges)
7869
{
7970
await DbContextBulkTransactionSaveChanges.SaveChangesAsync(context, bulkConfig, progress, cancellationToken).ConfigureAwait(false);
71+
return;
8072
}
81-
else if(bulkConfig?.IncludeGraph == true)
73+
74+
if (bulkConfig?.IncludeGraph == true)
8275
{
8376
await DbContextBulkTransactionGraphUtil.ExecuteWithGraphAsync(context, entities, operationType, bulkConfig, progress, cancellationToken).ConfigureAwait(false);
77+
return;
8478
}
85-
else
86-
{
87-
TableInfo tableInfo = TableInfo.CreateInstance(context, type, entities, operationType, bulkConfig);
8879

89-
if (operationType == OperationType.Insert && !tableInfo.BulkConfig.SetOutputIdentity)
90-
{
80+
var tableInfo = TableInfo.CreateInstance(context, type, entities, operationType, bulkConfig);
81+
82+
switch (operationType)
83+
{
84+
case OperationType.Insert when !tableInfo.BulkConfig.SetOutputIdentity:
9185
await SqlBulkOperation.InsertAsync(context, type, entities, tableInfo, progress, cancellationToken).ConfigureAwait(false);
92-
}
93-
else if (operationType == OperationType.Read)
94-
{
86+
break;
87+
88+
case OperationType.Read:
9589
await SqlBulkOperation.ReadAsync(context, type, entities, tableInfo, progress, cancellationToken).ConfigureAwait(false);
96-
}
97-
else if (operationType == OperationType.Truncate)
98-
{
90+
break;
91+
92+
case OperationType.Truncate:
9993
await SqlBulkOperation.TruncateAsync(context, tableInfo, cancellationToken).ConfigureAwait(false);
100-
}
101-
else
102-
{
94+
break;
95+
96+
default:
10397
await SqlBulkOperation.MergeAsync(context, type, entities, tableInfo, operationType, progress, cancellationToken).ConfigureAwait(false);
104-
}
98+
break;
10599
}
106100
}
107101
}
102+
103+
#region SqlAdapters Settings
104+
private static void UpdateSqlAdaptersProps(DbContext context)
105+
{
106+
SqlAdaptersMapping.ProviderName = context.Database.ProviderName;
107+
}
108+
#endregion
109+
110+
#region Transaction Validators
111+
private static bool IsValidTransaction<T>(IEnumerable<T> entities, OperationType operationType, BulkConfig? bulkConfig)
112+
{
113+
return entities.Any() ||
114+
operationType == OperationType.Truncate ||
115+
operationType == OperationType.SaveChanges ||
116+
operationType == OperationType.InsertOrUpdateOrDelete ||
117+
bulkConfig is { CustomSourceTableName: not null } ||
118+
bulkConfig is { DataReader: not null };
119+
}
120+
#endregion
108121
}

0 commit comments

Comments
 (0)
Please sign in to comment.