Rupesh Kahane

Rupesh Kahane

  • 102
  • 19.1k
  • 4.2m

How to use Begin Tran,Commit Rollback in Repository pattern

Dec 21 2016 11:58 PM
I have created a project using MVC Unit Of Work Repository Pattern.
 
I have implemented CRUD operation using repository pattern.
 
I have total 4 tables like 1) Master Table, 2) to 4) Detail Table.
  
 
Now I have a bellow situation I filled all text box on above form & click on save button. By using repository I am going to Insert data one by one i.e will store data into Master Details then Details 1 to Details 4.
 
But after inserting data into first 2 tables, because of some wrong information tables 3 & 4 are not inserted. So in this situation I would like to revert data from Master details and  Details Table 1.
 
How can I use Begin Transaction, commit, rollback using repository pattern.
 
Bellow is my code
  1.  // Insert Data for Master Details table 1
  2. MasterDetails objMaster= new MasterDetails();   
  3. objMaster.RFQ = UserTextValue;  
  4. objMaster.CompanyCode =  UserTextValue;  
  5. //other fields   
  6. UnitoffWork.MasterDetailsRepository.Insert(objMaster);  
  7. UnitoffWork.MasterDetailsRepository.Save();  
  8.   
  9.  // Insert Data for Details 2  
  10. Details1 objD1= new Details1();   
  11. objD1.RFQDate = UserTextValue;  
  12. objD1.Item =  UserTextValue; 
  13. //other fields  
  14. UnitoffWork.DetailsOneRepository.Insert(objD1);  
  15. UnitoffWork.DetailsOneRepository.Save();  
  16.   
  17.  // Insert Data for Details 3  
  18. Details2 objD2= new Details2();   
  19. objD2.TargVal = UserTextValue;  
  20. UnitoffWork.DetailsTwoRepository.Insert(objD2);  
  21. UnitoffWork.DetailsTwoRepository.Save();  
  22.   
  23.  // Insert Data for Details 4  
  24. Details3 objD3= new Details3();   
  25. objD3.YourReference =  UserTextValue;  
  26. //other fields
  27. UnitoffWork.DetailsThreeRepository.Insert(objD3);  
  28. UnitoffWork.DetailsThreeRepository.Save();  
 Thanks
 
 

Answers (1)