部分类文件的命名约定

我正在生成大量的 ASP.NET MVC 脚手架代码。所有生成的文件都是使用标准命名约定的部分类。例如,我的雇员控制器文件名为 employeecontroller.cs。如果我希望使用自定义的、非生成的逻辑来扩展 EmployeeController,我会创建第二个名为 employeecontrollercustom.cs 的分部类文件。我将自定义逻辑和生成的逻辑分离到两个不同的文件中,以便下次生成 EmployeeController 时不会覆盖自定义更改。在文件名后加上“ Custom”后缀对我来说似乎是合理的,但是否有一个更加成熟的部分类文件变数命名原则,我应该遵循?

24067 次浏览

I use . separation - for example EmployeeController.SomeSpecialBehaviour.cs. I also link it into the project tree via "dependentUpon" or whatever it is in the csproj, so that it nests under the file (in solution explorer) neatly. You have to do that by hand (edit the csproj) or with an addin, though; for example:

<Compile Include="Subfolder/Program.cs" />
<Compile Include="Subfolder/Program.Foo.cs">
<DependentUpon>Program.cs</DependentUpon> <!-- Note that I do not reference the subfolder here -->
</Compile>

appears as:

  • Subfolder
      • Program.Foo.cs

UPDATE / DISCLAIMER: On 2018 someone edited Marc Gravell♦'s answer (the one accepted above) to include a subfolder in his example. And how to handle the case of having a subfolder is the main point of this answer.

Without that disclaimer you probably wouldn't understand why this answer exists and why it has so many votes.


To add to Marc Gravell♦'s answer, I had a situation with files in a subfolder and the DependentUpon node being ignored. The short of it is that in such a case it my xml had to be:

<Compile Include="foo\bar.cs" />
<Compile Include="foo\bar.baz.cs">
<DependentUpon>bar.cs</DependentUpon>  <!-- Note that I do not reference the subfolder here -->
</Compile>

I hope this helps someone :)