简体   繁体   English

C# - 如何检查缺少scope.Complete()语句?

[英]C# - How do I check for missing scope.Complete() statements?

Programmers on my team sometimes open a transaction and forget to include the scope.Complete() statement (see code block below). 我团队中的程序员有时会打开一个事务而忘记包含scope.Complete()语句(参见下面的代码块)。 Any ideas on ways to either 任何有关方法的想法

(1) search our solution for missing scope.Complete() statements, or (1)搜索我们的解决方案是否缺少scope.Complete()语句,或

(2) have Visual Studio automatically highlight or raise a warning for missing scope.Complete() statements? (2)让Visual Studio自动突出显示或发出缺少scope.Complete()语句的警告?

Here's the line we miss: 这是我们想念的那条线:

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

What I have tried 我试过了什么

I have tried using a ReSharper Custom Pattern for this purpose, with no luck. 我尝试过使用ReSharper自定义模式,没有运气。 Ideally I would search for something like: 理想情况下,我会搜索类似的东西:

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

However, ReSharper only accepts regular expressions for identifiers, not for statements, so this does not appear to work ( http://www.jetbrains.com/resharper/webhelp/Reference__Search_with_Pattern.html ). 但是,ReSharper只接受标识符的正则表达式,而不接受语句,因此这似乎不起作用( http://www.jetbrains.com/resharper/webhelp/Reference__Search_with_Pattern.html )。

Any ideas? 有任何想法吗? I'm open to using other plugins or tools as well. 我也愿意使用其他插件或工具。

Thanks, 谢谢,
Ben

Could you force programmers to use a custom API instead of the low-level scope.Complete stuff? 您是否可以强制程序员使用自定义API而不是低级范围。完整的东西?

A closure will force usage of .Complete() : 闭包将强制使用.Complete()

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

Then you could do: 然后你可以这样做:

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

NDepend can certainly help, but cannot check 100% of what you are asking for. NDepend当然可以提供帮助,但不能100%检查你的要求。 NDepend doesn't know about the method body internals (order of method calls). NDepend不知道方法体内部(方法调用的顺序)。 So at best, you can write a code rule over LINQ (CQLinq) that will check that if a method is creating a TransactionScope , at least it must call TransactionScope.Complete() : 所以充其量,您可以在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

Note that if developers are disciplined enough to avoid creating multiple TransactionScope in one method, this rule should work for you. 请注意,如果开发人员足够严格以避免在一种方法中创建多个TransactionScope ,则此规则应该适合您。

I'm not aware of any existing R# plugin that checks for this, but you could certainly create one of your own. 我不知道有任何现有的R#插件可以检查这个,但你当然可以自己创建一个。 All you'd have to do is to detect a using statement with a variable declaration of the TransactionScope type, then iterate the contained statements looking for the Complete() call. 您所要做的就是检测带有TransactionScope类型的变量声明的using语句,然后迭代包含的语句,查找Complete()调用。

If you are interested in doing this, I recommend you download the ReSharper SDK and check out the Plugin Development Guide . 如果您对此感兴趣,我建议您下载ReSharper SDK并查看插件开发指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM