Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface ISyncer
- {
- // Read input and synchronize destination, return error message if any
- string Sync();
- }
- public class StudentSyncer : ISyncer
- {
- // simple XML reader, reads XML file into a list of List<XMLStudent>
- private XMLStudentReader _reader;
- public StudentSyncer(string file)
- {
- _reader = new XMLStudentReader(file);
- }
- public string Sync()
- {
- // read the input
- List<XMLStudent> source = _reader.ReadAll();
- // using automapper map to domain object
- List<Student> dbStudent = Mapper.Map<List<XMLStudent>, List<Student>>(source);
- dbStudent.ForEach(s => s.IsCurrent = true); // set IsCurrent
- using (var context = new DbContext())
- {
- UnitOfWork uow = new UnitOfWork(context);
- // set the existing records to not current
- var curSet = uow.Student.FindByTrackingChanges(s => s.IsCurrent == true).ToList();
- curSet.ForEach(s => s.IsCurrent = false);
- uow.Student.AddRange(dbStudent);
- uow.SaveChanges();
- }
- return ""; // no error
- }
- }
- public void DoSync()
- {
- foreach (string file in Directory.EnumerateFiles(@"C:\Source", "*.xml"))
- {
- // use syncer factory to initialize the correct instance of syncer based on input file name
- ISyncer syncer = _syncerFactory.CreateInstance(file);
- // do the synchronization task
- syncer.Sync();
- // Move file to processed folder
- MoveFile(@"C:Destination");
- }
- }
- abstract class SyncerBase : ISyncer
- {
- sealed List<T> Map(List source, List target)
- {
- // Mapping magic here
- }
- sealed SaveToDB(List dataToSave, params ...)
- {
- // Your database access here
- }
- }
- class StundentSyncer : SyncerBase
- {
- // Constructor as in your code
- void Sync(string file)
- {
- var XMLstudents = _reader.ReadAll();
- var students = Map(XMLstudents); // calling base class here!
- // Manipulate student list here as needed
- SaveToDB (students, database params here) // base class here, too
- }
- }
Add Comment
Please, Sign In to add comment