Skip to content

Commit ebb0863

Browse files
author
Cesar Romero Silva
committed
* Refactory
1 parent 3ccb3cd commit ebb0863

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

BulkOperation.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public static class BulkOperation
1515
await connection.OpenAsync(cancellationToken).ConfigureAwait(true);
1616

1717
const int recordsToGenerate = 100_000;
18-
Log.Write($"Gerando {recordsToGenerate:n0} registros clientes");
18+
ConsoleLog.Write($"Gerando {recordsToGenerate:n0} registros clientes");
1919
var customers = Customer.Generate(recordsToGenerate);
2020

21-
Log.Write("Inserindo registros no banco de dados");
21+
ConsoleLog.Write("Inserindo registros no banco de dados");
2222
using (var insertBulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, null))
2323
{
2424
using (var customerReader = new ObjectDataReader<Customer>(customers.GetEnumerator()))
@@ -33,26 +33,26 @@ public static class BulkOperation
3333
{
3434
try
3535
{
36-
Log.Write("Criando tabela temporária e copiando registros");
36+
ConsoleLog.Write("Criando tabela temporária e copiando registros");
3737
command.CommandText = BulkUpdate.CreateTempTable;
3838
command.ExecuteNonQuery();
3939
try
4040
{
41-
Log.Write("Atualizando registros: Tabela temporária -> Customers");
41+
ConsoleLog.Write("Atualizando registros: Tabela temporária -> Customers");
4242
command.CommandTimeout = 300;
4343
command.CommandText = BulkUpdate.UpdateTable;
4444
command.ExecuteNonQuery();
4545
}
4646
finally
4747
{
48-
Log.Write("Excluindo tabela temporária");
48+
ConsoleLog.Write("Excluindo tabela temporária");
4949
command.CommandText = BulkUpdate.DropTempTable;
5050
command.ExecuteNonQuery();
5151
}
5252
}
5353
catch (Exception e)
5454
{
55-
Log.Write(e.Message);
55+
ConsoleLog.Write(e.Message);
5656
}
5757
}
5858
}
@@ -77,13 +77,13 @@ private static void MapEntity(this SqlBulkCopy bulk, string tableName)
7777
private static void ConfigureOptions(this SqlBulkCopy bulk)
7878
{
7979
bulk.EnableStreaming = true;
80-
bulk.BatchSize = 10_000;
81-
bulk.NotifyAfter = 1_000;
80+
bulk.BatchSize = 10_000;
81+
bulk.NotifyAfter = 1_000;
8282
}
8383

8484
private static void ConfigureLog(this SqlBulkCopy bulk)
8585
{
86-
bulk.SqlRowsCopied += (sender, e) => Log.Write($"{DateTime.Now} RowsCopied: {e.RowsCopied:n0}");
86+
bulk.SqlRowsCopied += (sender, e) => ConsoleLog.Write($"{DateTime.Now} RowsCopied: {e.RowsCopied:n0}");
8787
}
8888
}
8989
}

Log.cs renamed to ConsoleLog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BulkOperations
44
{
5-
public static class Log
5+
public static class ConsoleLog
66
{
77
public static void Write(string value)
88
{

ObjectDataReader.cs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ namespace BulkOperations
1616
public class ObjectDataReader<T> : DbDataReader
1717
{
1818
private readonly IEnumerator<T> _iterator;
19-
private readonly IDictionary<string, int> _propertyNameToOrdinal = new Dictionary<string, int>();
20-
private readonly IDictionary<int, string> _ordinalToPropertyName = new Dictionary<int, string>();
21-
private Func<T, object>[] _getPropertyValueFuncs;
19+
private readonly IDictionary<string, int> _propToOrdinalTable = new Dictionary<string, int>();
20+
private readonly IDictionary<int, string> _ordinalToPropTable = new Dictionary<int, string>();
21+
private Func<T, object>[] _getPropValueFunc;
2222

2323
public ObjectDataReader(IEnumerator<T> items)
2424
{
@@ -29,14 +29,14 @@ public ObjectDataReader(IEnumerator<T> items)
2929
private void Initialize()
3030
{
3131
var properties = typeof(T).GetProperties();
32-
_getPropertyValueFuncs = new Func<T, object>[properties.Length];
32+
_getPropValueFunc = new Func<T, object>[properties.Length];
3333

3434
var ordinal = 0;
3535
foreach (var property in properties)
3636
{
3737
var propertyName = property.Name;
38-
_propertyNameToOrdinal.Add(propertyName, ordinal);
39-
_ordinalToPropertyName.Add(ordinal, propertyName);
38+
_propToOrdinalTable.Add(propertyName, ordinal);
39+
_ordinalToPropTable.Add(ordinal, propertyName);
4040

4141
var parameterExpression = Expression.Parameter(typeof(T), "x");
4242
var func = (Func<T, object>)Expression.Lambda(
@@ -46,7 +46,7 @@ private void Initialize()
4646
parameterExpression)
4747
.Compile();
4848

49-
_getPropertyValueFuncs[ordinal] = func;
49+
_getPropValueFunc[ordinal] = func;
5050

5151
ordinal++;
5252
}
@@ -60,7 +60,7 @@ public override bool Read()
6060

6161
public override int GetOrdinal(string name)
6262
{
63-
return (_propertyNameToOrdinal.TryGetValue(name, out var ordinal)) ? ordinal : -1;
63+
return (_propToOrdinalTable.TryGetValue(name, out var ordinal)) ? ordinal : -1;
6464
}
6565

6666
public override bool IsDBNull(int ordinal)
@@ -70,11 +70,10 @@ public override bool IsDBNull(int ordinal)
7070

7171
public override object GetValue(int ordinal)
7272
{
73-
var func = _getPropertyValueFuncs[ordinal];
73+
var func = _getPropValueFunc[ordinal];
7474
return func(_iterator.Current);
7575
}
7676

77-
// optional
7877
public override int GetValues(object[] values)
7978
{
8079
var max = Math.Min(values.Length, FieldCount);
@@ -92,13 +91,12 @@ public override int GetValues(object[] values)
9291

9392
public override int Depth => 0;
9493

95-
public override int FieldCount => _ordinalToPropertyName.Count;
94+
public override int FieldCount => _ordinalToPropTable.Count;
9695

9796
public override bool HasRows => true;
9897

9998
public override bool IsClosed => _iterator != null;
10099

101-
public override int RecordsAffected => throw new NotImplementedException();
102100

103101
public override bool GetBoolean(int ordinal)
104102
{
@@ -110,26 +108,11 @@ public override byte GetByte(int ordinal)
110108
return (byte)GetValue(ordinal);
111109
}
112110

113-
public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length)
114-
{
115-
throw new NotImplementedException();
116-
}
117-
118111
public override char GetChar(int ordinal)
119112
{
120113
return (char)GetValue(ordinal);
121114
}
122115

123-
public override long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length)
124-
{
125-
throw new NotImplementedException();
126-
}
127-
128-
public override string GetDataTypeName(int ordinal)
129-
{
130-
throw new NotImplementedException();
131-
}
132-
133116
public override DateTime GetDateTime(int ordinal)
134117
{
135118
return (DateTime)GetValue(ordinal);
@@ -145,11 +128,6 @@ public override double GetDouble(int ordinal)
145128
return (double)GetValue(ordinal);
146129
}
147130

148-
public override IEnumerator GetEnumerator()
149-
{
150-
throw new NotImplementedException();
151-
}
152-
153131
public override Type GetFieldType(int ordinal)
154132
{
155133
var value = GetValue(ordinal);
@@ -183,7 +161,7 @@ public override long GetInt64(int ordinal)
183161

184162
public override string GetName(int ordinal)
185163
{
186-
return _ordinalToPropertyName.TryGetValue(ordinal, out var name) ? name : null;
164+
return _ordinalToPropTable.TryGetValue(ordinal, out var name) ? name : null;
187165
}
188166

189167
public override string GetString(int ordinal)
@@ -195,5 +173,30 @@ public override bool NextResult()
195173
{
196174
return false;
197175
}
176+
177+
#region NotImplemented
178+
public override int RecordsAffected =>
179+
throw new NotImplementedException();
180+
181+
public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length)
182+
{
183+
throw new NotImplementedException();
184+
}
185+
186+
public override long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length)
187+
{
188+
throw new NotImplementedException();
189+
}
190+
191+
public override string GetDataTypeName(int ordinal)
192+
{
193+
throw new NotImplementedException();
194+
}
195+
196+
public override IEnumerator GetEnumerator()
197+
{
198+
throw new NotImplementedException();
199+
}
200+
#endregion
198201
}
199202
}

Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ private static void Main(string[] args)
1010
var stopwatch = new Stopwatch();
1111
stopwatch.Start();
1212

13-
Log.Write("Início processamento");
13+
ConsoleLog.Write("Início processamento");
1414

1515
BulkOperation.InsertAsync().Wait();
1616

1717
stopwatch.Stop();
1818

1919

20-
Log.Write($"Processamento concluído em {stopwatch.ElapsedTimeFmt()}");
20+
ConsoleLog.Write($"Processamento concluído em {stopwatch.ElapsedTimeFmt()}");
2121

2222
Console.WriteLine("");
2323
Console.WriteLine("Tecle <ENTER> para finalizar...");

0 commit comments

Comments
 (0)