Guest User

Untitled

a guest
Jun 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. public interface ISyncer
  2. {
  3. // Read input and synchronize destination, return error message if any
  4. string Sync();
  5. }
  6.  
  7. public class StudentSyncer : ISyncer
  8. {
  9. // simple XML reader, reads XML file into a list of List<XMLStudent>
  10. private XMLStudentReader _reader;
  11.  
  12. public StudentSyncer(string file)
  13. {
  14. _reader = new XMLStudentReader(file);
  15. }
  16.  
  17. public string Sync()
  18. {
  19. // read the input
  20. List<XMLStudent> source = _reader.ReadAll();
  21.  
  22. // using automapper map to domain object
  23. List<Student> dbStudent = Mapper.Map<List<XMLStudent>, List<Student>>(source);
  24. dbStudent.ForEach(s => s.IsCurrent = true); // set IsCurrent
  25.  
  26. using (var context = new DbContext())
  27. {
  28. UnitOfWork uow = new UnitOfWork(context);
  29. // set the existing records to not current
  30. var curSet = uow.Student.FindByTrackingChanges(s => s.IsCurrent == true).ToList();
  31. curSet.ForEach(s => s.IsCurrent = false);
  32.  
  33. uow.Student.AddRange(dbStudent);
  34. uow.SaveChanges();
  35. }
  36. return ""; // no error
  37. }
  38. }
  39.  
  40. public void DoSync()
  41. {
  42. foreach (string file in Directory.EnumerateFiles(@"C:\Source", "*.xml"))
  43. {
  44. // use syncer factory to initialize the correct instance of syncer based on input file name
  45. ISyncer syncer = _syncerFactory.CreateInstance(file);
  46.  
  47. // do the synchronization task
  48. syncer.Sync();
  49.  
  50. // Move file to processed folder
  51. MoveFile(@"C:Destination");
  52. }
  53. }
  54.  
  55. abstract class SyncerBase : ISyncer
  56. {
  57. sealed List<T> Map(List source, List target)
  58. {
  59. // Mapping magic here
  60. }
  61.  
  62. sealed SaveToDB(List dataToSave, params ...)
  63. {
  64. // Your database access here
  65. }
  66. }
  67.  
  68. class StundentSyncer : SyncerBase
  69. {
  70. // Constructor as in your code
  71. void Sync(string file)
  72. {
  73. var XMLstudents = _reader.ReadAll();
  74. var students = Map(XMLstudents); // calling base class here!
  75. // Manipulate student list here as needed
  76. SaveToDB (students, database params here) // base class here, too
  77. }
  78. }
Add Comment
Please, Sign In to add comment