2013-01-19 83 views
1

我想用NDepend創建一個特殊查詢,但無法弄清楚。查詢檢測ISP違規

這是我想在多個程序的僞代碼來查詢的內容:

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 

它應該列出不符合該接口分離原則類型。

謝謝!

+0

這是什麼意思「t.attribute是一個接口」的接口不是屬性類? –

+0

在「實際使用的numberOfMethods」中是否表示「實際使用的i的numberOfMethods」請確切地說 –

+0

嘿,你說的沒錯。 i =在類t中任何地方使用的任何接口(可以是類變量,或者可以在t的方法中使用)。 「實際使用的numberOfMethods」表示例如我有3種方法:A(),B()和C()。在t中只調用A()和B()。所以「畝」應該等於2. – leifbattermann

回答

2

所以你問的查詢可以這樣寫:

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爲不爲空。然後將結果呈現很好的和可以理解的:

Segregation Principle with NDepend

+0

這正是我想要的。非常感謝! – leifbattermann

+0

不客氣:) –

+0

其實第二行應該是這樣的:「從我在t.TypesUsed哪裏i.IsInterface &&(!i.TypesThatImplementMe.Contains(t))」否則它會識別實現我的類型作爲使用我的類型,這不應該是。但除此之外它是完美的 – leifbattermann