Skip to content

Instantly share code, notes, and snippets.

@nimacks
Created February 18, 2015 17:20
Show Gist options
  • Save nimacks/b3396686b33590e3ef7e to your computer and use it in GitHub Desktop.
Save nimacks/b3396686b33590e3ef7e to your computer and use it in GitHub Desktop.
BCP Import Refactoring
void Main()
{
//Setup Chain of Responsibility
ImportPhase readData = new DataReadingPhase();
ImportPhase mapData = new DataMappingPhase();
ImportPhase importData = new DataImportPhase();
readData.SetNextPhase(mapData);
mapData.SetNextPhase(importData);
int retryAttempts = 0;
FileImport import = new FileImport();
bool retry = readData.ProcessImport(import);
while(retry)
{
try
{
retry = readData.ProcessImport(import);
}
catch (Exception ex)
{
if(ex.Message.Contains("Timeout expired") && retryAttempts < 3)
{
retryAttempts ++;
}
}
}
}
// Define other methods and classes here
/// <summary>
/// The 'Handler' abstract class
/// </summary>
abstract class ImportPhase
{
protected ImportPhase nextPhase;
protected ImportPhase previousPhase;
public void SetNextPhase(ImportPhase nextPhase)
{
this.nextPhase = nextPhase;
}
public void SetPreviousPhase(ImportPhase previousPhase)
{
this.previousPhase = previousPhase;
}
public abstract bool ProcessImport(FileImport fileImport);
}
/// <summary>
/// The 'ConcreteHandler' class
/// </summary>
class DataReadingPhase : ImportPhase
{
public override void ProcessImport(FileImport fileImport)
{
bool retry = false;
try
{
//read number of files to be imported
//Create Delimited StreamReader to read data as a string
}
catch (Exception ex)
{
retry = true;
//Log error
}
if (nextPhase != null && !retry)
{
nextPhase.ProcessImport(fileImport);
}
return retry;
}
}
class DataMappingPhase : ImportPhase
{
public override bool ProcessImport(FileImport fileImport)
{
bool retry = false;
try
{
//create mappedImportObject
//BuildList of types to map from data
//Build list of columns to map from data
}
catch (Exception ex)
{
retry = true;
//Log error
}
if(nextPhase != null && !retry)
{
nextPhase.ProcessImport(fileImport);
}
return retry;
}
}
class DataImportPhase : ImportPhase
{
public override bool ProcessImport(FileImport fileImport)
{
bool retry = false;
try
{
//Create Import Piplline using mappedImport
//Setup BCP Object
//Run Import Using BCP & Pipeline
}
catch (Exception ex)
{
retry = true;
//Log error
}
if(nextPhase !=null && !retry)
{
nextPhase.ProcessImport(fileImport);
}
return retry;
}
}
class FileImport
{
public string Foo { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment