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?
-
100th like : )) – Chandraprakash Aug 08 '21 at 10:12
2 Answers
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

- 610
- 6
- 14

- 1,026,079
- 266
- 2,566
- 2,900
-
5The 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. – Ben Griswold Sep 25 '09 at 18:30
-
12Correct - 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` ;-p – Marc Gravell Sep 25 '09 at 19:02
-
1@Marc Gravell: do you by any chance know any VS extensions that provide the functionality of setting DependentUpon for files? – Dyppl Feb 09 '11 at 05:17
-
2@Dyppl The [FileNesting](https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting) extension can do this – g t May 24 '18 at 10:57
-
@MarcGravell: Is it required for both partial class files to use the same `namespace`? – JohnB Apr 20 '22 at 15:54
-
1
-
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`. – Fred Feb 16 '23 at 12:16
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 :)

- 7,847
- 7
- 46
- 67

- 1,615
- 2
- 23
- 39
-
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. – Joshua K Aug 29 '18 at 18:38