.net 如何仅使用TransactionScope和sqlconnections实现工作单元

.net 如何仅使用TransactionScope和sqlconnections实现工作单元,.net,repository,design-patterns,transactionscope,unit-of-work,.net,Repository,Design Patterns,Transactionscope,Unit Of Work,我正在使用一个现有系统(asp.net 2,ms sql server 2005),其中存储库模式的实现方式如下: IMyRepository { void add(object o); int update(object obj); int delete(object obj); IList<T> getAll(); IList<T> getByDate(DateTime date); .... } IMyRepository { 无效添加(对象o

我正在使用一个现有系统(asp.net 2,ms sql server 2005),其中存储库模式的实现方式如下:

IMyRepository
{
  void add(object o);
  int update(object obj);
  int delete(object obj);
  IList<T> getAll();
  IList<T> getByDate(DateTime date);
....
}
IMyRepository
{
无效添加(对象o);
int更新(objectobj);
int-delete(objectobj);
IList getAll();
IList getByDate(日期时间日期);
....
}
该系统有3种不同的产品。因此,我们对每种产品都有不同的存储库。随着需求的不断变化,我们需要为业务流程级事务实现工作单元模式。
我们没有任何ORM。(实际上,我们现在没有权限或时间实施它) 有谁能给我一个明确的指南,告诉我如何仅仅使用TransactionScope和sqlconnections来实现工作单元

请说明如何快速高效地关闭Sqlconnections(因为它有大量用户)。
工作单元模式对我来说是新事物。

先谢谢你

在工作单元模式中,您需要调用Commit()和/或Rollback()。使用TransactionScope,可以调用Complete()或nothing,这将执行回滚

using(TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
  // do your gets inside of supressed TransactionScopes:
  using (new TransactionScope(TransactionScopeOption.Suppress))
  {
    myRep.GetAll();
  }

  // do your updates
  myRep.Update();

  // no errors, so commit
  ts.Complete();

  // Close db connections
  myConn.Close();
}

用试一试的方式将整个过程包围起来,你就完全准备好了。如果出现任何异常,则永远不会调用ts.Complete(),并且会回滚对数据库的更改。请记住在启动transactionscope之前以及提交或回滚之后重置连接。

那么我应该如何初始化具体的MyRepository类?我的意思是MyRepository repo=新建MyRepository(SqlConnection??)?;
using(TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
  // do your gets inside of supressed TransactionScopes:
  using (new TransactionScope(TransactionScopeOption.Suppress))
  {
    myRep.GetAll();
  }

  // do your updates
  myRep.Update();

  // no errors, so commit
  ts.Complete();

  // Close db connections
  myConn.Close();
}