Last active
May 14, 2020 20:31
-
-
Save ierhalim/3225e21658a511ad31f3cb8b71f42496 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IImportEngine<TSource, TTarget> where TSource : ExternalSourceModel | |
where TTarget : IIdentityEntity | |
{ | |
Task Import(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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