C # - How to check for missing scope.Complete () statements?

The programmers of my team sometimes open a transaction and forget to include the scope.Complete () operator (see the code block below). Any ideas on how or

(1) find our solution for missing areas scope.Complete () or

(2) Does Visual Studio automatically raise or raise a warning about missing scope.Complete () parameters?

Here is the line we are skipping:

 using(TransactionScope scope = new TransactionScope())
 {
      /* Perform transactional work here */
      scope.Complete(); <-- we forget this line
      /* Optionally, include a return statement */
 }

What I tried

For this purpose I tried to use a custom ReSharper template with no luck. Ideally, I would look for something like:

using(TransactionScope scope = new TransactionScope())
{
    $statements1$
    [^(scope.Complete();)]
    $statements2$
}

However, ReSharper only accepts regular expressions for identifiers, not for operators, so this does not work ( http://www.jetbrains.com/resharper/webhelp/Reference__Search_with_Pattern.html ).

? .

,

+5
3

NDepend, , , 100% , . NDepend ( ). LINQ (CQLinq), , TransactionScope, TransactionScope.Complete():

warnif count > 0
from m in Application.Methods
where m.CreateA("System.Transactions.TransactionScope") &&
     !m.IsUsing("System.Transactions.TransactionScope.Complete()")
select m

, , TransactionScope , .

+3

API . ?

.Complete():

public static void Do(this TransactionScope scope, Action action) {
  using (scope) {
    action();
    scope.Complete();
  }
}

:

new TransactionScope().Do(() => /* Transactional stuff */);
+4

I don't know about the existing R # plugin that tests this, but you can certainly create your own. All you have to do is find the using statement with a type variable declaration TransactionScope, and then iterate through the instructions in it looking for a call Complete().

If you are interested in this, I recommend that you download the ReSharper SDK and check out the Plugin Development Guide .

0
source

All Articles