Skip to content

Instantly share code, notes, and snippets.

@ierhalim
Last active May 14, 2020 20:36
Show Gist options
  • Save ierhalim/0d020c8a935189adc6b3901997fd7bb1 to your computer and use it in GitHub Desktop.
Save ierhalim/0d020c8a935189adc6b3901997fd7bb1 to your computer and use it in GitHub Desktop.
public class ExampleImporter
{
private readonly IExampleService _exampleService;
public ExampleImporter(IExampleService exampleService)
{
_exampleService = exampleService;
}
public async Task Import()
{
var items = await _exampleService.Get();
foreach (var item in items)
{
string valMessage;
if (!IsValid(item, out valMessage))
{
// TODO: Log validation message.
continue;
}
var example = GetFromDb(item);
if (example == null)
{
example = new Example();
// TODO: Update example properties from item.
Add(example);
}
else
{
// TODO: Update data properties.
Update(example);
}
}
Commit();
}
private Example GetFromDb(ExternalExampleModel externalExample)
{
// TODO Get data from db if exist.
}
private void Add(Example example)
{
// TODO: Add item to db
}
private void Update(Example example)
{
// TODO: Update item
}
private bool IsValid(ExternalExampleModel data, out string validationMessage)
{
// TODO: Validate item
validationMessage = "";
return true;
}
private void Commit()
{
// TODO: Commit changes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment