【发布时间】:2021-05-04 01:35:16
【问题描述】:
我在一个 .NET 项目中使用 Rider,该项目有许多注入大量服务并在文件顶部占用大量空间的类,如下所示:
public class MyCoolService: IMyCoolService
{
private readonly IMyInjectedService1: myInjectedService1;
private readonly IMyInjectedService2: myInjectedService2;
private readonly IMyInjectedService3: myInjectedService3;
private readonly IMyInjectedService4: myInjectedService4;
private readonly IMyInjectedService5: myInjectedService5;
private readonly IMyInjectedService6: myInjectedService6;
private readonly IMyInjectedService7: myInjectedService7;
private readonly IMyInjectedService8: myInjectedService8;
private readonly IMyInjectedService9: myInjectedService9;
private readonly IMyInjectedService10: myInjectedService10;
private readonly IMyInjectedService11: myInjectedService11;
private readonly IMyInjectedService12: myInjectedService12;
private readonly IMyInjectedService13: myInjectedService13;
private readonly IMyInjectedService14: myInjectedService14;
private readonly IMyInjectedService15: myInjectedService15;
private readonly IMyInjectedService16: myInjectedService16;
private readonly IMyInjectedService17: myInjectedService17;
private readonly IMyInjectedService18: myInjectedService18;
private readonly IMyInjectedService19: myInjectedService19;
private readonly IMyInjectedService20: myInjectedService20;
public MyCoolService(IMyInjectedService1 myInjectedService1,
IMyInjectedService2 myInjectedService2, IMyInjectedService3 myInjectedService3,
IMyInjectedService4 myInjectedService4, IMyInjectedService5 myInjectedService5,
IMyInjectedService6 myInjectedService6, IMyInjectedService7 myInjectedService7,
IMyInjectedService8 myInjectedService8, IMyInjectedService9 myInjectedService9,
IMyInjectedService10 myInjectedService10, IMyInjectedService11 myInjectedService11,
IMyInjectedService12 myInjectedService12, IMyInjectedService13 myInjectedService13,
IMyInjectedService14 myInjectedService14, IMyInjectedService15 myInjectedService15,
IMyInjectedService16 myInjectedService16, IMyInjectedService17 myInjectedService17,
IMyInjectedService18 myInjectedService18, IMyInjectedService19 myInjectedService19,
IMyInjectedService20 myInjectedService20)
{
this.myInjectedService1 = myInjectedService1;
this.myInjectedService2 = myInjectedService2;
this.myInjectedService3 = myInjectedService3;
this.myInjectedService4 = myInjectedService4;
this.myInjectedService5 = myInjectedService5;
this.myInjectedService6 = myInjectedService6;
this.myInjectedService7 = myInjectedService7;
this.myInjectedService8 = myInjectedService8;
this.myInjectedService9 = myInjectedService9;
this.myInjectedService10 = myInjectedService10;
this.myInjectedService11 = myInjectedService11;
this.myInjectedService12 = myInjectedService12;
this.myInjectedService13 = myInjectedService13;
this.myInjectedService14 = myInjectedService14;
this.myInjectedService15 = myInjectedService15;
this.myInjectedService16 = myInjectedService16;
this.myInjectedService17 = myInjectedService17;
this.myInjectedService18 = myInjectedService18;
this.myInjectedService19 = myInjectedService19;
this.myInjectedService20 = myInjectedService20;
}
}
当我浏览代码时,我喜欢使用代码折叠来获得一个类的概览,然后再展开并深入到我想要处理的特定部分。目前 Rider 可以自动折叠隐藏了一些细节的构造函数体:
public class MyCoolService: IMyCoolService
{
private readonly IMyInjectedService1: myInjectedService1;
private readonly IMyInjectedService2: myInjectedService2;
private readonly IMyInjectedService3: myInjectedService3;
private readonly IMyInjectedService4: myInjectedService4;
private readonly IMyInjectedService5: myInjectedService5;
private readonly IMyInjectedService6: myInjectedService6;
private readonly IMyInjectedService7: myInjectedService7;
private readonly IMyInjectedService8: myInjectedService8;
private readonly IMyInjectedService9: myInjectedService9;
private readonly IMyInjectedService10: myInjectedService10;
private readonly IMyInjectedService11: myInjectedService11;
private readonly IMyInjectedService12: myInjectedService12;
private readonly IMyInjectedService13: myInjectedService13;
private readonly IMyInjectedService14: myInjectedService14;
private readonly IMyInjectedService15: myInjectedService15;
private readonly IMyInjectedService16: myInjectedService16;
private readonly IMyInjectedService17: myInjectedService17;
private readonly IMyInjectedService18: myInjectedService18;
private readonly IMyInjectedService19: myInjectedService19;
private readonly IMyInjectedService20: myInjectedService20;
public MyCoolService(IMyInjectedService1 myInjectedService1,
IMyInjectedService2 myInjectedService2, IMyInjectedService3 myInjectedService3,
IMyInjectedService4 myInjectedService4, IMyInjectedService5 myInjectedService5,
IMyInjectedService6 myInjectedService6, IMyInjectedService7 myInjectedService7,
IMyInjectedService8 myInjectedService8, IMyInjectedService9 myInjectedService9,
IMyInjectedService10 myInjectedService10, IMyInjectedService11 myInjectedService11,
IMyInjectedService12 myInjectedService12, IMyInjectedService13 myInjectedService13,
IMyInjectedService14 myInjectedService14, IMyInjectedService15 myInjectedService15,
IMyInjectedService16 myInjectedService16, IMyInjectedService17 myInjectedService17,
IMyInjectedService18 myInjectedService18, IMyInjectedService19 myInjectedService19,
IMyInjectedService20 myInjectedService20)
{...}
}
但是,这留下了私有字段和构造函数参数。我最终要做的是使用Ctrl+. 自定义代码折叠来隐藏这些区域(然后我可以在需要时展开它们)。我的问题是每次都是手动过程。有没有办法让 Rider 将这些区域识别为可折叠并自动完成?
public class MyCoolService: IMyCoolService
{
...
public MyCoolService(...)
{...}
}
【问题讨论】:
-
如果您需要将 20 个服务注入到一个类中,这表明您需要将该类重构为多个类,因为它可能会尝试做太多事情。那个或所有这些服务都过于简单化了,可以合并在一起。
-
同意。尽管它是一个大型遗留代码库,并且像这样的重构将是相当大的变化。目前我只是想办法让它更好地工作
-
我发现Rider lets you define custom foldable regions 和
//<editor-fold desc="description">cmets。这不是一个理想的解决方案 - 我希望 IDE 自动识别这些区域 - 但它是一个解决方案
标签: c# .net jetbrains-ide rider