【问题标题】:query to detect ISP violations查询以检测 ISP 违规
【发布时间】:2013-01-19 15:27:01
【问题描述】:

我正在尝试使用 NDepend 创建一个特殊查询,但无法弄清楚。

这是我想用更程序化的伪代码查询的内容:

var list
foreach type t
    foreach i = t.attribute that is an interface
        var nm = i.numberOfMethods
        var mu = numberOfMethods that t actually uses
        if mu / nm < 1
        list.Add(t)
    end foreach
end foreach
return list

应该列出不符合接口隔离原则的类型。

谢谢!

【问题讨论】:

  • 什么是“t.attribute that is an interface”一个接口不是一个属性类?
  • 在“t实际使用的numberOfMethods”中是指“t实际使用的i的numberOfMethods”请准确
  • 嘿,你说得对,它并不精确。 i = 在类 t 中的任何地方使用的任何接口(可以是类变量或可以在 t 的方法中本地使用)。 “t 实际使用的 numberOfMethods”意味着例如 i 有 3 个方法:A()、B() 和 C()。在 t 内仅调用 A() 和 B()。所以“mu”应该等于2。

标签: ndepend


【解决方案1】:

所以你问的查询可以这样写:

from t in JustMyCode.Types where !t.IsAbstract

from i in t.TypesUsed where i.IsInterface

// Here collect methods of i that are not used
let methodsOfInterfaceUnused = i.Methods.Where(m => !m.IsUsedBy(t))
where methodsOfInterfaceUnused.Count() > 0
select new { t, methodsOfInterfaceUnused }

这个查询的特点是多次匹配同一个类型,每次匹配一个methodsOfInterfaceUnused不为空。结果很好地呈现并且易于理解:

【讨论】:

  • 这正是我想要的。非常感谢!
  • 其实第二行应该是这样的:"from i in t.TypesUsed where i.IsInterface && (!i.TypesThatImplementMe.Contains(t))" 否则会识别出实现i的类型作为使用 i 的类型,这是不应该的。但除此之外它完美无缺
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 2013-03-23
  • 2018-04-23
  • 2022-09-23
相关资源
最近更新 更多