Skip to content

Commit 67845b8

Browse files
author
Cesar Romero Silva
committed
* Reafactory
1 parent 7a9f121 commit 67845b8

5 files changed

+16
-18
lines changed

Diff for: BulkOperation.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ public static void Run()
1212
{
1313
var stopwatch = new Stopwatch();
1414
stopwatch.Start();
15-
1615
ConsoleLog.Write("Início processamento");
1716

18-
BulkOperation.InsertAsync().Wait();
17+
InsertAsync().Wait();
1918

2019
stopwatch.Stop();
21-
2220
ConsoleLog.Write($"Processamento concluído em {stopwatch.ElapsedTimeFmt()}");
2321
}
2422

25-
public static async Task InsertAsync(CancellationToken cancellationToken = default(CancellationToken))
23+
public static async Task InsertAsync(CancellationToken cancellationToken = default(CancellationToken), SqlTransaction transaction = null)
2624
{
2725
using (var connection = new SqlConnection())
2826
{
@@ -34,7 +32,7 @@ public static void Run()
3432
var customers = Customer.Generate(recordsToGenerate);
3533

3634
ConsoleLog.Write("Inserindo registros no banco de dados");
37-
using (var insertBulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, null))
35+
using (var insertBulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, transaction))
3836
{
3937
using (var customerReader = new ObjectDataReader<Customer>(customers.GetEnumerator()))
4038
{
@@ -92,8 +90,8 @@ private static void MapEntity(this SqlBulkCopy bulk, string tableName)
9290
private static void ConfigureOptions(this SqlBulkCopy bulk)
9391
{
9492
bulk.EnableStreaming = true;
95-
bulk.BatchSize = 10_000;
96-
bulk.NotifyAfter = 1_000;
93+
bulk.BatchSize = 10_000;
94+
bulk.NotifyAfter = 1_000;
9795
}
9896

9997
private static void ConfigureLog(this SqlBulkCopy bulk)

Diff for: Customer.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public static IEnumerable<Customer> Generate(int count)
3131
{
3232
yield return new Customer
3333
{
34-
Id = Guid.NewGuid(),
35-
FirstName = "FirstName" + i,
36-
LastName = "LastName" + i,
34+
Id = Guid.NewGuid(),
35+
FirstName = "First Name " + i,
36+
LastName = "Last Name " + i,
3737
DateOfBirth = DateTime.Now
3838
};
3939
}

Diff for: ObjectDataReader.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ namespace BulkOperations
1515
/// </summary>
1616
public class ObjectDataReader<T> : DbDataReader
1717
{
18-
private readonly IEnumerator<T> _iterator;
18+
private readonly IEnumerator<T> _items;
1919
private readonly IDictionary<string, int> _propToOrdinalTable = new Dictionary<string, int>();
2020
private readonly IDictionary<int, string> _ordinalToPropTable = new Dictionary<int, string>();
2121
private Func<T, object>[] _getPropValueFunc;
2222

2323
public ObjectDataReader(IEnumerator<T> items)
2424
{
25-
_iterator = items ?? throw new ArgumentNullException(nameof(items));
25+
_items = items ?? throw new ArgumentNullException(nameof(items));
2626
Initialize();
2727
}
2828

@@ -52,10 +52,9 @@ private void Initialize()
5252
}
5353
}
5454

55-
// required
5655
public override bool Read()
5756
{
58-
return _iterator.MoveNext();
57+
return _items.MoveNext();
5958
}
6059

6160
public override int GetOrdinal(string name)
@@ -71,7 +70,7 @@ public override bool IsDBNull(int ordinal)
7170
public override object GetValue(int ordinal)
7271
{
7372
var func = _getPropValueFunc[ordinal];
74-
return func(_iterator.Current);
73+
return func(_items.Current);
7574
}
7675

7776
public override int GetValues(object[] values)
@@ -95,7 +94,7 @@ public override int GetValues(object[] values)
9594

9695
public override bool HasRows => true;
9796

98-
public override bool IsClosed => _iterator != null;
97+
public override bool IsClosed => _items != null;
9998

10099

101100
public override bool GetBoolean(int ordinal)

Diff for: StateMachineBug.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public StateMachineBug(string title)
5858
_machine = new StateMachine<State, Trigger>(State.Open);
5959

6060
// Instantiate a new trigger with a parameter.
61-
_assignTrigger = _machine.SetTriggerParameters<string>(Trigger.Assign);
61+
_assignTrigger = _machine
62+
.SetTriggerParameters<string>(Trigger.Assign);
6263

6364
// Configure the Open state
6465
_machine.Configure(State.Open)

Diff for: StateMachinePhoneCall.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public StateMachinePhoneCall(string caller)
9292
.OnExit(t => StopCallTimer())
9393
.InternalTransition(Trigger.MuteMicrophone, t => OnMute())
9494
.InternalTransition(Trigger.UnmuteMicrophone, t => OnUnmute())
95-
.InternalTransition<int>(_setVolumeTrigger, (volume, t) => OnSetVolume(volume))
95+
.InternalTransition(_setVolumeTrigger, (volume, t) => OnSetVolume(volume))
9696
.Permit(Trigger.LeftMessage, State.OffHook)
9797
.Permit(Trigger.PlacedOnHold, State.OnHold);
9898

0 commit comments

Comments
 (0)