2

I'm trying to write a CQL query in Visual NDepend to find all types and methods that don't directly depend on any type from a list of namespaces.

The Query I've built so far is this one:

SELECT METHODS
WHERE 
   !IsDirectlyUsing "NAMESPACE:Microsoft.*"
   AND !IsDirectlyUsing "NAMESPACE:System.Web.UI.*"
   AND !FullNameLike ".Test"
   AND !HasAttribute "System.CodeDom.Compiler.GeneratedCodeAttribute"
   AND FullNameLike "OurOwnNameSpaceHere"

But this still returns methods that accept a SPWeb as a parameter, so I must be missing something.

So I want to:

  • exclude any method that depends on any type inside any referenced Assembly which is inside a Microsoft.* namespace.

  • exclude any method that depends on any type inside any referenced Assembly which is inside a System.Web.Ui.* namespace.

  • exclude any generated method/type

  • exclude any method that is part of a project that has Test in the namespace.

Sample methods that fall through are:

public void SomeMethod(SPWeb web)
{
    ... // other code here
    SomeOtherMethod(web);
    ...
}
jessehouwing
  • 106,458
  • 22
  • 256
  • 341

1 Answers1

1

You can try the following code query over LINQ (CQLinq query):

let dontUseTypes = Namespaces
                   .WithNameWildcardMatchIn("Microsoft.*", "System.Web.UI.*")
                   .ChildTypes()

from m in JustMyCode.Methods.Except(Methods.UsingAny(dontUseTypes))
where !m.ParentAssembly.Name.ToLower().Contains("test")
select m

The condition exclude any generated method/type is handled by the fact that we use JustMyCode.

Then, which methods or fields of SPWeb are used by the method still matched? NDepend can detect that a method uses a type only if it is using a member of the type.

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • the method looks like this: public void SomeMethod(SPWeb web){ .... SomeOtherMethod(web) .... } – jessehouwing Dec 06 '11 at 21:43
  • Does "SELECT METHODS FROM NAMESPACES "OurOwnNameSpaceHere" WHERE" work with wildcards like "SomeMain.NameSpace.*"? If so, that'll do :) – jessehouwing Dec 06 '11 at 21:47
  • Ok, so it looks like no member of SPWeb is used, hence NDepend won't see usage of methods to SPWeb. However NDepend can see usage of parent type of the method to SPWeb, so maybe you can write a query on type level? – Patrick from NDepend team Dec 07 '11 at 12:34
  • Not an option. I'm looking for all methods that are 'easy' to test as apposed from the ones that need TypeMock or Moles or a huge refactoring. I'm wondering *why* NDepend can't see this... it's in the methods signature! – jessehouwing Dec 08 '11 at 12:59
  • 1
    In the method sig it is just a string, the namespace nor generic expression if any is not precised. However this is a feature we are taking account for the future. – Patrick from NDepend team Dec 09 '11 at 13:08
  • Ok. Hope it is available soon :) – jessehouwing Dec 10 '11 at 15:54