【问题标题】:Sort classes in dependency order (NDepend)按依赖顺序对类进行排序 (NDepend)
【发布时间】:2018-06-18 16:20:33
【问题描述】:

我是 NDepend 的新手,刚刚开始涉足。我可以使用以下简单查询创建类依赖图:

from t in Application.Types
// remove compiler generated classes
where !t.FullName.Contains(">")
// sort by dependency count
orderby t.TypesUsed.Count()
select new { t, t.TypesUsed }

我正在尝试构建一个有序的类列表,以便列表中的第一项不具有系统类型以外的依赖项。随着我们在列表中前进,每种类型都应该只具有在列表中出现在它之前的依赖项。我意识到,在具有周期性依赖关系的情况下,这是不可能的,但我希望至少将它们排列成更正确的顺序,而不是简单地按依赖计数排序。

【问题讨论】:

    标签: c# ndepend


    【解决方案1】:

    这是一个但棘手但可行的方法。此 CQLinq 代码查询使用了 NDepend.API 方法 FillIterative() 的魔力。

    // The call FillIterative() will fill hashSet with types already processed
    let hashSet = new HashSet<IType>()
    
    let fillHashSetWithTypesFunc = new Func<IEnumerable<IType>, bool>(types => types.All(t => hashSet.Add(t)))
    
    let metric = ThirdParty.Types.FillIterative(
       types => 
          // Use a ternary operator to invoke fillHashSetWithTypesFunc() that always return true,
          fillHashSetWithTypesFunc(types) ?
             // Select t when hashSet contains all t.TypesUsed
             Application.Types.Where(t => t.TypesUsed.Intersect(hashSet).Count() == t.TypesUsed.Count()) :
             // new IType[0] is never invoqued coz fillHashSetWithTypesFunc() always returns true
             new IType[0])
    
    from val in metric 
    where val.Value > 0  // Don't show third-party types
    orderby val.Value ascending
    select new { val.CodeElement, 
       (val.CodeElement as IType).Level, 
       val.Value, 
       (val.CodeElement as IType).TypesUsed }
    

    循环中涉及的类型不匹配。

    结果为每个匹配的类型分配一个值:

    • (1) 是仅使用第三方类型的类型
    • (2) 是使用第三方的类型和 (1) 中的类型
    • (3) ...

    有趣的是,指标IType.Level 提供了准确的衡量标准并回答了您的问题。但是用代码查询重写它更有趣。命名空间(也有 Level 度量)、程序集和方法也可以这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 2013-08-30
      • 2017-11-30
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      相关资源
      最近更新 更多