Advertisement
Guest User

Untitled

a guest
Jan 6th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. public class Employee
  2. {
  3. public int EmployeeId { get; set; }
  4. public string FirstName { get; set; }
  5. public string LastName { get; set; }
  6. public List<ContactPhone> ContactPhoneNumbers { get; set; }
  7. public List<ContactEmail> ContactEmailAddress { get; set; }
  8. }
  9.  
  10. public class ContactPhone
  11. {
  12. public int ContactId { get; set; }
  13. public string Type { get; set; }
  14. public string Number { get; set; }
  15. }
  16.  
  17. public class ContactEmail
  18. {
  19. public int ContactId { get; set; }
  20. public string Type { get; set; }
  21. public string Number { get; set; }
  22. }
  23.  
  24. EmployeeId FirstName LastName
  25. _________________________________
  26. 1 Bala Manigandan
  27.  
  28. ContactId EmployeeId Type Number
  29. __________________________________________
  30. 1 1 Fax 9123456789
  31. 2 1 Mobile 9123456789
  32.  
  33. ContactId EmployeeId Type EmailAddress
  34. ______________________________________________
  35. 1 1 Private bala@gmail.com
  36. 2 1 Public bala@ymail.com
  37.  
  38. DTO.Employee emp = new DTO.Employee()
  39. {
  40. EmployeeId = 1,
  41. FirstName = "Bala",
  42. LastName = "Manigandan",
  43. ContactPhoneNumbers = new List<DTO.ContactPhone>
  44. {
  45. new DTO.ContactPhone()
  46. {
  47. Type = "Mobile",
  48. Number = "9000012345"
  49. }
  50. },
  51. ContactEmailAddress = new List<DTO.ContactEmail>()
  52. {
  53. new DTO.ContactEmail()
  54. {
  55. Type = "Private",
  56. EmailAddress = "bala@gmail.com"
  57. },
  58. new DTO.ContactEmail()
  59. {
  60. Type = "Public",
  61. EmailAddress = "bala@ymail.com"
  62. }
  63. }
  64. };
  65.  
  66. public void ProcessEmployee(DTO.Employee employee)
  67. {
  68. if(employee != null)
  69. {
  70. DevDBEntities dbContext = new DevDBEntities();
  71.  
  72. DbContextTransaction dbTransaction = dbContext.Database.BeginTransaction();
  73.  
  74. List<Task> taskList = new List<Task>();
  75. List<bool> transactionStatus = new List<bool>();
  76.  
  77. try
  78. {
  79. Employee emp = dbContext.Employees.FirstOrDefault(m => m.EmployeeId == employee.EmployeeId);
  80.  
  81. if (emp != null)
  82. {
  83. Task task1 = Task.Factory.StartNew(() =>
  84. {
  85. bool flag = UpdateContactPhone(emp.EmployeeId, employee.ContactPhoneNumbers.FirstOrDefault().Type, employee.ContactPhoneNumbers.FirstOrDefault().Number, dbContext).Result;
  86. transactionStatus.Add(flag);
  87. });
  88.  
  89. taskList.Add(task1);
  90.  
  91. Task task2 = Task.Factory.StartNew(() =>
  92. {
  93. bool flag = RemoveContactPhone(emp.EmployeeId, "Fax", dbContext).Result;
  94. transactionStatus.Add(flag);
  95. });
  96.  
  97. taskList.Add(task2);
  98. }
  99.  
  100. if(taskList.Any())
  101. {
  102. Task.WaitAll(taskList.ToArray());
  103. }
  104. }
  105. catch
  106. {
  107. dbTransaction.Rollback();
  108. }
  109. finally
  110. {
  111. if(transactionStatus.Any(m => !m))
  112. {
  113. dbTransaction.Rollback();
  114. }
  115. else
  116. {
  117. dbTransaction.Commit();
  118. }
  119.  
  120. dbTransaction.Dispose();
  121. dbContext.Dispose();
  122. }
  123. }
  124. }
  125.  
  126. public async Task<bool> UpdateContactPhone(int empId, string type, string newPhone, DevDBEntities dbContext)
  127. {
  128. bool flag = false;
  129.  
  130. try
  131. {
  132. var empPhone = dbContext.ContactPhones.FirstOrDefault(m => (m.EmployeeId == empId) && (m.Type == type));
  133. if (empPhone != null)
  134. {
  135. empPhone.Number = newPhone;
  136. await dbContext.SaveChangesAsync();
  137. }
  138. }
  139. catch (Exception ex)
  140. {
  141. throw ex;
  142. }
  143.  
  144. return flag;
  145. }
  146.  
  147. public async Task<bool> RemoveContactPhone(int empId, string type, DevDBEntities dbContext)
  148. {
  149. bool flag = false;
  150.  
  151. try
  152. {
  153. var empPhone = dbContext.ContactPhones.FirstOrDefault(m => (m.EmployeeId == empId) && (m.Type == type));
  154. if (empPhone != null)
  155. {
  156. dbContext.ContactPhones.Remove(empPhone);
  157. await dbContext.SaveChangesAsync();
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. throw ex;
  163. }
  164.  
  165. return flag;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement