Skip to content

Commit 9f058aa

Browse files
author
Jason Valdez
committed
Added comments to BulkLoader
1 parent a89ae6e commit 9f058aa

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

src/ivaldez.SqlBulkLoader/BulkLoader.cs

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ public class BulkLoader : IBulkLoader
1515
{
1616
private readonly ISqlBulkCopyUtility _sqlBulkCopyUtility;
1717

18+
/// <summary>
19+
/// Constructor that uses default SqlBulkCopyUtility class.
20+
/// </summary>
1821
public BulkLoader()
1922
{
2023
_sqlBulkCopyUtility = new SqlBulkCopyUtility();
2124
}
2225

26+
/// <summary>
27+
/// Constructor with SQL BulkCopy Utility. Interface allows for extension and testing.
28+
/// </summary>
29+
/// <param name="sqlBulkCopyUtility"></param>
2330
public BulkLoader(ISqlBulkCopyUtility sqlBulkCopyUtility)
2431
{
2532
_sqlBulkCopyUtility = sqlBulkCopyUtility;
@@ -121,35 +128,10 @@ public void Insert<T>(string tableName,
121128
}
122129
}
123130

124-
private void BulkCopyWithNoBatching<T>(string tableName, SqlConnection conn, IEnumerable<T> dataToInsert,
125-
SqlBulkCopyOptions options, TargetProperty[] targetProperties)
126-
{
127-
_sqlBulkCopyUtility.BulkCopy(tableName, conn, options, targetProperties, dataToInsert);
128-
}
129-
130-
private void BulkCopyWithBatching<T>(string tableName, SqlConnection conn, IEnumerable<T> dataToInsert, int batchSize,
131-
SqlBulkCopyOptions options, TargetProperty[] targetProperties)
132-
{
133-
var batch = new List<T>(batchSize);
134-
135-
foreach (var item in dataToInsert)
136-
{
137-
batch.Add(item);
138-
139-
if (batch.Count >= batchSize)
140-
{
141-
_sqlBulkCopyUtility.BulkCopy(tableName, conn, options, targetProperties, batch);
142-
batch.Clear();
143-
}
144-
}
145-
146-
if (batch.Any())
147-
{
148-
_sqlBulkCopyUtility.BulkCopy(tableName, conn, options, targetProperties, batch);
149-
batch.Clear();
150-
}
151-
}
152-
131+
/// <summary>
132+
/// SqlBulkCopyUtility wraps the basic functionality for bulkCopy. The interface is provided so that
133+
/// the basic class can be extended if necessary, as well as for testing.
134+
/// </summary>
153135
public class SqlBulkCopyUtility: ISqlBulkCopyUtility
154136
{
155137
public void BulkCopy<T>(string tableName, SqlConnection conn, SqlBulkCopyOptions options,
@@ -176,6 +158,10 @@ public void BulkCopy<T>(string tableName, SqlConnection conn, SqlBulkCopyOptions
176158
}
177159
}
178160

161+
/// <summary>
162+
/// SqlBulkCopyUtility wraps the basic functionality for bulkCopy. The interface is provided so that
163+
/// the basic class can be extended if necessary, as well as for testing.
164+
/// </summary>
179165
public interface ISqlBulkCopyUtility
180166
{
181167
void BulkCopy<T>(string tableName, SqlConnection conn, SqlBulkCopyOptions options,
@@ -190,6 +176,35 @@ public class TargetProperty
190176
public string OriginalName { get; set; }
191177
}
192178

179+
private void BulkCopyWithNoBatching<T>(string tableName, SqlConnection conn, IEnumerable<T> dataToInsert,
180+
SqlBulkCopyOptions options, TargetProperty[] targetProperties)
181+
{
182+
_sqlBulkCopyUtility.BulkCopy(tableName, conn, options, targetProperties, dataToInsert);
183+
}
184+
185+
private void BulkCopyWithBatching<T>(string tableName, SqlConnection conn, IEnumerable<T> dataToInsert, int batchSize,
186+
SqlBulkCopyOptions options, TargetProperty[] targetProperties)
187+
{
188+
var batch = new List<T>(batchSize);
189+
190+
foreach (var item in dataToInsert)
191+
{
192+
batch.Add(item);
193+
194+
if (batch.Count >= batchSize)
195+
{
196+
_sqlBulkCopyUtility.BulkCopy(tableName, conn, options, targetProperties, batch);
197+
batch.Clear();
198+
}
199+
}
200+
201+
if (batch.Any())
202+
{
203+
_sqlBulkCopyUtility.BulkCopy(tableName, conn, options, targetProperties, batch);
204+
batch.Clear();
205+
}
206+
}
207+
193208
private static TargetProperty[] GetTargetProperties<T>(List<string> propertiesToIgnore,
194209
Dictionary<string, string> renameFields)
195210
{
@@ -220,6 +235,9 @@ private static TargetProperty[] GetTargetProperties<T>(List<string> propertiesTo
220235
}
221236
}
222237

238+
/// <summary>
239+
/// Factory class used to create default instances of the BulkLoader
240+
/// </summary>
223241
public class BulkLoaderFactory
224242
{
225243
public static IBulkLoader Create()

0 commit comments

Comments
 (0)