Naming Conventions For Partial Class Files
Asked Answered
W

2

116

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?

Witty answered 25/9, 2009 at 17:30 Comment(1)
100th like : ))Mascia
M
182

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
Meanly answered 25/9, 2009 at 17:34 Comment(7)
The DependentUpon suggestion is really cool and works great. Thanks for noting. If I'm reading correctly, you don't simply use a standard suffix like "Custom." Your suffix always expresses the intent of the partial class file's functionality. Also, is there a reason why you use the . separation opposed to casing? Does the . provide anything more than improved readability? Thanks.Witty
Correct - the file name indicates the intent of the code in that portion. So if I am implementing an exotic interface (and keeping the code separate), it might be SomeType.ICustomTypeDescriptor.cs. The . (IMO) separates the two things: the actual type (SomeType) and the intent ICustomTypeDescriptor - both are already fully cased; besides, it matches neatly with things like SomeForm.Designer.cs ;-pMeanly
@Marc Gravell: do you by any chance know any VS extensions that provide the functionality of setting DependentUpon for files?Thompson
@Thompson The FileNesting extension can do thisGorrono
@MarcGravell: Is it required for both partial class files to use the same namespace?Altis
@Altis yes, otherwise they're different typesMeanly
I use .NET 6 in VS 2022 and I didn't need any of this XML in my csproj, my Visual Studio just automatically folded them files in the Solution Explorer when naming them Invoice.cs and Invoice.Impl.cs.Bandolier
C
22

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

Consubstantiation answered 8/12, 2014 at 10:15 Comment(1)
me too. it happened because I started the project in database-first and when it created the model it put them inside the model diagram. VS2015 if it makes a difference to anyone.Eversole

© 2022 - 2024 — McMap. All rights reserved.