How to Implement SQL transaction commit without external pollution (2), SQL transaction

Source: Internet
Author: User

How to Implement SQL transaction commit without external pollution (2), SQL transaction

Next, we will record the transaction operations here,ImplementationMulti-entity functions

Add the following method to the SqlTran class:

1. Two transaction methods of different entity types:

1 // <summary> 2 // execute the transaction (different entities in the transaction) 3 /// </summary> 4 /// <typeparam name = "T"> entity </typeparam> 5 /// <param name = "method"> method (SqlTransaction is passed in as null by default) </param> 6 /// <param name = "obj1"> parameter value </param> 7 /// <returns> </returns> 8 public static Int32 ExecuteTran <M, n> (Func <M, SqlTransaction, Int32> method1, Func <N, SqlTransaction, Int32> method2, M obj1, N obj2) 9 where M: new () 10 where N: new () 11 {1 2 Int32 count = 0; 13 SqlConnection conn = null; 14 SqlTransaction tran = null; 15 try16 {17 conn = new SqlConnection (Repository. connStr); 18 conn. open (); 19 tran = conn. beginTransaction (); 20 21 count + = method1 (obj1, tran); 22 count + = method2 (obj2, tran); 23 24 tran. commit (); 25 return count; 26} 27 catch (Exception ex) 28 {29 tran. rollback (); 30 return-1; 31} 32 finally33 {34 if (tran! = Null) 35 tran. Dispose (); 36 if (conn! = Null) 37 {38 conn. close (); 39 conn. dispose (); 40} 41} 42 43} 44 45 // <summary> 46 // execute the transaction (different entities in the transaction) 47 // </summary> 48 // <typeparam name = "T"> entity </typeparam> 49 // <param name = "method"> method (SqlTransaction is passed in as null by default) </param> 50 // <param name = "obj1"> parameter value </param> 51 // <returns> </returns> 52 public static Int32 ExecuteTran <M, n> (IList <Func <M, SqlTransaction, Int32> methods1, IList <Func <N, SqlTransac Tion, Int32> methods2, IList <M> objs1, List <N> objs2) 53 where M: new () 54 where N: new () 55 {56 Int32 count = 0; 57 SqlConnection conn = null; 58 SqlTransaction tran = null; 59 try60 {61 conn = new SqlConnection (Repository. connStr); 62 conn. open (); 63 tran = conn. beginTransaction (); 64 65 if (methods1.Count ()! = Objs1.Count () 66 return-1; 67 if (methods2.Count ()! = Objs2.Count () 68 return-1; 69 70 for (int I = 0; I <objs1.Count (); I ++) 71 count + = methods1 [I] (objs1 [I], tran); 72 for (int I = 0; I <objs2.Count (); I ++) 73 count + = methods2 [I] (objs2 [I], tran); 74 75 tran. commit (); 76 return count; 77} 78 catch (Exception ex) 79 {80 tran. rollback (); 81 return-1; 82} 83 finally84 {85 if (tran! = Null) 86 tran. Dispose (); 87 if (conn! = Null) 88 {89 conn. Close (); 90 conn. Dispose (); 91} 92} 93 94}View Code

When the parameter is List, pay attention to the corresponding relationship

Methods1 --> objs1

Methods2 --> objs2

2. Test method:

1 public void Test () 2 {3 Repository repository = new Repository (); 4 5 Orders order11 = new Orders () {Id = 11, Name = "name11 "}; 6 Orders order21 = new Orders () {Id = 12, Name = "name12"}; 7 Orders order31 = new Orders () {Id = 13, Name = "name13 "}; 8 OrderDetail orderDetail11 = new OrderDetail () {Id = 11, OrderId = 1, Name = "namedetail11"}; 9 OrderDetail orderDetail12 = new OrderDetail () {Id = 12, orderId = 1, Name = "namedetail12"}; 10 11 var count1 = SqlTran. executeTran <Orders, OrderDetail> (repository. addOrder, repository. addOrderDetail, order11, orderDetail11); // different methods, different object types 12 13 List <Func <Orders, SqlTransaction, Int32> listFuncOrders = new List <Func <Orders, SqlTransaction, int32> (); 14 List <Func <OrderDetail, SqlTransaction, Int32> listFuncOrdersDetail = new List <Func <OrderDetail, SqlTransaction, Int32> (); 15 List <Orders> listOrder = new List <Orders> (); 16 List <OrderDetail> listOrderDatail = new List <OrderDetail> (); 17 18 listFuncOrders. add (repository. addOrder); 19 listFuncOrders. add (repository. addOrder); 20 listOrder. add (order21); 21 listOrder. add (order31); 22 23 listFuncOrdersDetail. add (repository. addOrderDetail); 24 listFuncOrdersDetail. add (repository. updateOrderDetail); 25 listOrderDatail. add (orderDetail12); 26 orderDetail11.Name = "namedetail11Update"; 27 listOrderDatail. add (orderDetail11); 28 29 var count2 = SqlTran. executeTran <Orders, OrderDetail> (listFuncOrders, listFuncOrdersDetail, listOrder, listOrderDatail); 30}View Code

 

3. Three transaction methods of different entity types: Definitions

Public static Int32 ExecuteTran <M, N, T> (Func <M, SqlTransaction, Int32> method1, Func <N, SqlTransaction, Int32> method2, Func <T, SqlTransaction, int32> method3, M obj1, N obj2, T obj3)
Where M: new ()
Where N: new ()
Where T: new ()

So it is OK to define based on the number of objects, and then it can be reused.

 

One of the disadvantages is that most of the Code in each transaction method is the same. Only the delegate execution method has minor changes (for example, count + = method (obj1, tran );), at present, I have no better way to extract the same piece of code for sharing. I hope to see my peers here. If there is a good solution, I hope I can share it with each other! For more information, see.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.