Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Employee
- {
- public int EmployeeId { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public List<ContactPhone> ContactPhoneNumbers { get; set; }
- public List<ContactEmail> ContactEmailAddress { get; set; }
- }
- public class ContactPhone
- {
- public int ContactId { get; set; }
- public string Type { get; set; }
- public string Number { get; set; }
- }
- public class ContactEmail
- {
- public int ContactId { get; set; }
- public string Type { get; set; }
- public string Number { get; set; }
- }
- EmployeeId FirstName LastName
- _________________________________
- 1 Bala Manigandan
- ContactId EmployeeId Type Number
- __________________________________________
- 1 1 Fax 9123456789
- 2 1 Mobile 9123456789
- ContactId EmployeeId Type EmailAddress
- ______________________________________________
- 1 1 Private bala@gmail.com
- 2 1 Public bala@ymail.com
- DTO.Employee emp = new DTO.Employee()
- {
- EmployeeId = 1,
- FirstName = "Bala",
- LastName = "Manigandan",
- ContactPhoneNumbers = new List<DTO.ContactPhone>
- {
- new DTO.ContactPhone()
- {
- Type = "Mobile",
- Number = "9000012345"
- }
- },
- ContactEmailAddress = new List<DTO.ContactEmail>()
- {
- new DTO.ContactEmail()
- {
- Type = "Private",
- EmailAddress = "bala@gmail.com"
- },
- new DTO.ContactEmail()
- {
- Type = "Public",
- EmailAddress = "bala@ymail.com"
- }
- }
- };
- public void ProcessEmployee(DTO.Employee employee)
- {
- if(employee != null)
- {
- DevDBEntities dbContext = new DevDBEntities();
- DbContextTransaction dbTransaction = dbContext.Database.BeginTransaction();
- List<Task> taskList = new List<Task>();
- List<bool> transactionStatus = new List<bool>();
- try
- {
- Employee emp = dbContext.Employees.FirstOrDefault(m => m.EmployeeId == employee.EmployeeId);
- if (emp != null)
- {
- Task task1 = Task.Factory.StartNew(() =>
- {
- bool flag = UpdateContactPhone(emp.EmployeeId, employee.ContactPhoneNumbers.FirstOrDefault().Type, employee.ContactPhoneNumbers.FirstOrDefault().Number, dbContext).Result;
- transactionStatus.Add(flag);
- });
- taskList.Add(task1);
- Task task2 = Task.Factory.StartNew(() =>
- {
- bool flag = RemoveContactPhone(emp.EmployeeId, "Fax", dbContext).Result;
- transactionStatus.Add(flag);
- });
- taskList.Add(task2);
- }
- if(taskList.Any())
- {
- Task.WaitAll(taskList.ToArray());
- }
- }
- catch
- {
- dbTransaction.Rollback();
- }
- finally
- {
- if(transactionStatus.Any(m => !m))
- {
- dbTransaction.Rollback();
- }
- else
- {
- dbTransaction.Commit();
- }
- dbTransaction.Dispose();
- dbContext.Dispose();
- }
- }
- }
- public async Task<bool> UpdateContactPhone(int empId, string type, string newPhone, DevDBEntities dbContext)
- {
- bool flag = false;
- try
- {
- var empPhone = dbContext.ContactPhones.FirstOrDefault(m => (m.EmployeeId == empId) && (m.Type == type));
- if (empPhone != null)
- {
- empPhone.Number = newPhone;
- await dbContext.SaveChangesAsync();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return flag;
- }
- public async Task<bool> RemoveContactPhone(int empId, string type, DevDBEntities dbContext)
- {
- bool flag = false;
- try
- {
- var empPhone = dbContext.ContactPhones.FirstOrDefault(m => (m.EmployeeId == empId) && (m.Type == type));
- if (empPhone != null)
- {
- dbContext.ContactPhones.Remove(empPhone);
- await dbContext.SaveChangesAsync();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return flag;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement