如何实现仅仅只使用的TransactionScope和sqlconnections工作单位如何实现、单位、工作、sqlconnections

2023-09-04 04:44:22 作者:6个字的王者荣耀名字,各种类型的都有:

我工作的一个现有的系统(asp.net 2,MS SQL服务器2005年),其中库模式等来实现:

I am working on a existing system(asp.net 2, ms sql server 2005) where repository pattern is implemented like:

IMyRepository
{
  void add(object o);
  int update(object obj);
  int delete(object obj);
  IList<T> getAll();
  IList<T> getByDate(DateTime date);
....
}

该系统有3种不同的产品。因此,我们有不同的版本库的每个产品。随着时间的推移需求的变化,我们需要实现的业务流程层面的事务工作模式单元。 我们没有任何的ORM。(其实我们没有足够的权限和时间,现在执行它) 谁能给我一个明确的指引如何实现用刚才刚才的TransactionScope和sqlconnections工作单位。

The system has 3 different products. So We have different repository for each product. As the requirement changes over time, we need to implement Unit Of Work pattern for business process level transaction. We don't have any ORM.(actually we don't have the permission or time to implement it now) Can anyone give me a clear guideline how to implement Unit Of Work just using just TransactionScope and sqlconnections.

PLZ提到如何快速,高效地完成Sqlconnections(因为它有大量的用户)。 工作模式的单位是新的东西给我。 在此先感谢...

Plz mention how to close Sqlconnections quickly and efficiently(as it has large number of users). The unit of work pattern is new thing to me. thanks in advance ...

推荐答案

那么,在你的工作模式单位必须调用commit()和/或回滚()。随着的TransactionScope,调用完成()或什么都没有,它不回滚。

Well, in your unit of work pattern you have a call to Commit() and/or Rollback(). With TransactionScope, you call Complete() or nothing at all, which does the rollback.

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,你提交之后,或回退。

Surround the whole thing with a try catch and you'll be all setup. If you get any exceptions, ts.Complete() is never called and your db changes are rolled back. Just remember to reset your connections before you start the transactionscope, and right after you commit or rollback.

 
精彩推荐
图片推荐