Skip to content

Instantly share code, notes, and snippets.

@ierhalim
Last active May 14, 2020 20:31
Show Gist options
  • Save ierhalim/3225e21658a511ad31f3cb8b71f42496 to your computer and use it in GitHub Desktop.
Save ierhalim/3225e21658a511ad31f3cb8b71f42496 to your computer and use it in GitHub Desktop.
public interface IImportEngine<TSource, TTarget> where TSource : ExternalSourceModel
where TTarget : IIdentityEntity
{
Task Import();
}
public class ImportEngine<TSource, TTarget> : IImportEngine<TSource, TTarget> where TSource : ExternalSourceModel
where TTarget : IIdentityEntity
{
private readonly IImporter<TSource, TTarget> _importer;
public ImportEngine(IImporter<TSource, TTarget> importer)
{
_importer = importer;
}
public async Task Import()
{
var source = await _importer.GetExternalSource();
foreach (var item in source)
{
if (_importer is IValidator<TSource>)
{
var validator = _importer as IValidator<TSource>;
string validationMessage;
if (!validator.Validate(item, out validationMessage))
{
// TODO: Log validation message.
continue;
}
}
var target = _importer.GetTarget(item);
if (target == null)
{
if (_importer is IInserter<TSource>)
{
var inserter = _importer as IInserter<TSource>;
inserter.Insert(item);
}
}
else if (_importer is IUpdater<TSource>)
{
var updater = _importer as IUpdater<TSource>;
updater.Update(item);
}
}
// TODO: Commit changes.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment