紧接着上文,这里主要记录事务操作,实现多实体的功能
在SqlTran类中添加方法如下:
1、两个不同实体类型的事务方法:
/// <summary>
/// 执行事务(事务中不同实体)
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="method">要执行的方法(SqlTransaction 默认传入为null)</param>
/// <param name="obj1">参数值</param>
/// <returns></returns>
public static Int32 ExecuteTran<M, N>(Func<M, SqlTransaction, Int32> method1, Func<N, SqlTransaction, Int32> method2, M obj1, N obj2)
where M : new()
where N : new()
{
Int32 count = ;
SqlConnection conn = null;
SqlTransaction tran = null;
try
{
conn = new SqlConnection(Repository.connStr);
conn.Open();
tran = conn.BeginTransaction(); count += method1(obj1, tran);
count += method2(obj2, tran); tran.Commit();
return count;
}
catch (Exception ex)
{
tran.Rollback();
return -;
}
finally
{
if (tran != null)
tran.Dispose();
if (conn != null)
{
conn.Close();
conn.Dispose();
}
} } /// <summary>
/// 执行事务(事务中不同实体)
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="method">要执行的方法(SqlTransaction 默认传入为null)</param>
/// <param name="obj1">参数值</param>
/// <returns></returns>
public static Int32 ExecuteTran<M, N>(IList<Func<M, SqlTransaction, Int32>> methods1, IList<Func<N, SqlTransaction, Int32>> methods2, IList<M> objs1, List<N> objs2)
where M : new()
where N : new()
{
Int32 count = ;
SqlConnection conn = null;
SqlTransaction tran = null;
try
{
conn = new SqlConnection(Repository.connStr);
conn.Open();
tran = conn.BeginTransaction(); if (methods1.Count() != objs1.Count())
return -;
if (methods2.Count() != objs2.Count())
return -; for (int i = ; i < objs1.Count(); i++)
count += methods1[i](objs1[i], tran);
for (int i = ; i < objs2.Count(); i++)
count += methods2[i](objs2[i], tran); tran.Commit();
return count;
}
catch (Exception ex)
{
tran.Rollback();
return -;
}
finally
{
if (tran != null)
tran.Dispose();
if (conn != null)
{
conn.Close();
conn.Dispose();
}
} }
参数为List的时候,注意对应关系
methods1-->objs1
methods2-->objs2
2、测试方法:
public void Test()
{
Repository repository = new Repository(); Orders order11 = new Orders() { Id = , Name = "name11" };
Orders order21 = new Orders() { Id = , Name = "name12" };
Orders order31 = new Orders() { Id = , Name = "name13" };
OrderDetail orderDetail11 = new OrderDetail() { Id = , OrderId = , Name = "namedetail11" };
OrderDetail orderDetail12 = new OrderDetail() { Id = , OrderId = , Name = "namedetail12" }; var count1 = SqlTran.ExecuteTran<Orders, OrderDetail>(repository.AddOrder, repository.AddOrderDetail, order11, orderDetail11); //不同方法,不同实体类型 List<Func<Orders, SqlTransaction, Int32>> listFuncOrders = new List<Func<Orders, SqlTransaction, Int32>>();
List<Func<OrderDetail, SqlTransaction, Int32>> listFuncOrdersDetail = new List<Func<OrderDetail, SqlTransaction, Int32>>();
List<Orders> listOrder = new List<Orders>();
List<OrderDetail> listOrderDatail = new List<OrderDetail>(); listFuncOrders.Add(repository.AddOrder);
listFuncOrders.Add(repository.AddOrder);
listOrder.Add(order21);
listOrder.Add(order31); listFuncOrdersDetail.Add(repository.AddOrderDetail);
listFuncOrdersDetail.Add(repository.UpdateOrderDetail);
listOrderDatail.Add(orderDetail12);
orderDetail11.Name = "namedetail11Update";
listOrderDatail.Add(orderDetail11); var count2 = SqlTran.ExecuteTran<Orders, OrderDetail>(listFuncOrders, listFuncOrdersDetail, listOrder, listOrderDatail);
}
3、三个不同实体类型的事务方法:定义
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()
所以根据实体的个数进行定义就ok,之后就可以重用了
缺点之一:每个事务方法中大部分的代码是一样的,只有委托执行方法部分有微小的变化(如:count += method(obj1, tran); ),本人目前没有更好的办法把这块相同的代码提取出来进行共用,希望看到这里的同行,如果有好的解决方案,希望能拿出来互相交流!指点迷津。