Naming Conventions For Partial Class Files

浪子不回头ぞ 提交于 2019-11-26 15:17:35

问题


I'm generating the bulk of my ASP.NET MVC scaffolding code. All generated files are partial classes which use standard naming conventions. For example, my employee controller file is named EmployeeController.cs. If I wish to extend the EmployeeController with custom, non-generated logic, I create a second partial class file named EmployeeControllerCustom.cs. I separate the custom and generated logic into two different files so the next time I generate the EmployeeController my custom changes aren't overwritten. Adding the "Custom" suffix to the file name seems reasonable to me, but is there a more established partial class file naming convention which I should be following?


回答1:


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.cs
      • Program.Foo.cs



回答2:


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 :)



来源:https://stackoverflow.com/questions/1478610/naming-conventions-for-partial-class-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!