.netndependcqlinq

Can CQLinq code be reused in multiple queries?


Let's say I'm analyzing a solution which contains a lot of Controls, e.g.

public class FooControl : IControlBase
{
    public void EvilMethod1()
    {
        // does some warning-level evil here
    }

    public void EvilMethod2()
    {
        // does some critical-level evil here
    }
}

I want to write two CQLinq queries to report all classes which have evil code (such as EvilMethod1) and such which use really evil code (such as EvilMethod2) in two separate queries.

To find all the types which should be analyzed by this query, I will write code such as

let Controls = from t in Types
where t.NameLike("Control")
&& t.Implement(@"myNamespace.IControlBase")
select t

from c in Controls
... // actual query goes here

This code would obviously used by both queries. Is there a way to reference this code in both queries or am I forced to replicate it?


Solution

  • For now you need to replicate the logic, however this feature is in our TODO list, you can vote for it here:

    https://ndepend.uservoice.com/forums/226344-ndepend-user-voice/suggestions/9752604-let-queries-pull-data-from-other-queries

    We will update this answer once available, hopefully some time in 2017.

    Btw, this kind of idea is already available through the notmycode JustMyCode feature, but you can only define and reuse the JustMyCode set.