部分类文件的命名约定

发布于 2024-08-05 08:13:19 字数 291 浏览 7 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2024-08-12 08:13:19

我使用 . 分隔 - 例如 EmployeeController.SomeSpecialBehaviour.cs。我还通过“dependentUpon”或 csproj 中的任何内容将其链接到项目树中,以便它整齐地嵌套在文件下(在解决方案资源管理器中)。不过,您必须手动(编辑 csproj)或使用插件来完成此操作;例如:

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

显示为:

  • 子文件夹
    • Program.cs
      • Program.Foo.cs

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
差↓一点笑了 2024-08-12 08:13:19

更新/免责声明:
2018 年,有人编辑了 Marc Gravell 的答案(上面接受的答案),在他的示例中包含一个子文件夹。如何处理有子文件夹的情况是这个答案的要点。

如果没有该免责声明,您可能不会理解为什么这个答案存在以及为什么它有如此多的选票。


为了添加 Marc Gravell 的答案,我遇到了子文件夹中的文件和 < code>DependentUpon 节点被忽略。缺点是在这种情况下我的 xml 必须是:

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

我希望这对某人有帮助:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文