C# 项目文件夹命名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1389458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:57:18  来源:igfitidea点击:

C# Project folder naming conventions

c#.netnaming-conventions

提问by alexn

I have a project called Data which is a data layer. In this project, all files are just lying in the top folder. I have enumerations, POCOs, repositories, partial classes and so on.

我有一个名为 Data 的项目,它是一个数据层。在这个项目中,所有文件都位于顶层文件夹中。我有枚举、POCO、存储库、部分类等等。

If i want to move those files into subfolders, what would be the preffered folder name for each folder? Is there any convention?

如果我想将这些文件移动到子文件夹中,每个文件夹的首选文件夹名称是什么?有什么约定吗?

The "Repositories" folder is pretty obvious, but where should i keep POCOs and enumerations?

“存储库”文件夹非常明显,但我应该在哪里保存 POCO 和枚举?

Thanks

谢谢

采纳答案by Timothy Carter

I tend to use project folders as a way of separating out sub namespaces. So in your case, perhaps a folder called Repositories, which has class in the Data.Repositories namespace. Note, for partial classes, each file needs to be in the same namespace.

我倾向于使用项目文件夹作为分离子命名空间的一种方式。所以在你的情况下,也许是一个名为 Repositories 的文件夹,它在 Data.Repositories 命名空间中有类。注意,对于分部类,每个文件都需要在同一个命名空间中。

回答by Vitaliy Ulantikov

Best prectice is to divide entities in folders by object model meaning, not by type.

最好的方法是按对象模型含义而不是按类型划分文件夹中的实体。

回答by Alfred Myers

A good practice is to name the folder after the name of the project.

一个好的做法是在项目名称之后命名文件夹。

Design Guidelines for Developing Class Librarieshas a set of Guidelines for Names

开发类库的设计指南有一组名称指南

The last item should be of paticular interest for you:

最后一项应该是您特别感兴趣的:

回答by Ian Ringrose

If it is not clear how to group the classes by usage or object model meaning, just leave them all in one folder. Using subfolders don't give values if they don't organise the classes in a meaningful way.

如果不清楚如何按用途或对象模型含义对类进行分组,请将它们全部放在一个文件夹中。如果子文件夹没有以有意义的方式组织类,则使用子文件夹不会给出值。

Dividing folders by type, e.g. enumerations, POCOs, repositories, partial classes etc is not likely to be useful.

按类型划分文件夹,例如枚举、POCO、存储库、部分类等不太可能有用。

You may wish to use a subfolder for generated code that should not be edited.

您可能希望对不应编辑的生成代码使用子文件夹。

Also remember you can have folders within the solution explorer that are not part of the file system. Given how costly (in time) it is in some source code control systems to move files between directories, I would consider starting of just using msdev folders until you are clear on the structure you want.

还请记住,您可以在解决方案资源管理器中拥有不属于文件系统的文件夹。鉴于在某些源代码控制系统中在目录之间移动文件的成本(及时),我会考虑开始只使用 msdev 文件夹,直到您清楚所需的结构为止。

There is no needto put each enumeration in its own file, if an enumeration is only used by one class, it is valid to put it in the same file as the class. E.g the PersonSex enumeration can be put in the person.cs file. Likewise if you have a lot of smalland closely relatedclasses, consider putting them in the same file.

没有必要把每个枚举放在自己的文件中,如果一个枚举只被一个类使用,那么把它和类放在同一个文件中是有效的。例如,PersonSex 枚举可以放在 person.cs 文件中。同样,如果您有许多密切相关的类,请考虑将它们放在同一个文件中。

回答by Andrew Siemer

I (currently - changes based on project) tend to use this approach when naming assemblies/projects/namespaces in a SAAS/Web style project)

我(目前 - 基于项目的更改)在 SAAS/Web 样式项目中命名程序集/项目/命名空间时倾向于使用这种方法)

  • CompanyName.
    • ProductName.
      • Data.
      • Business. (references data)
      • Model. (POCO and interfaces - referenced by all)
      • Services. (WCF service layer)
      • ServiceClient. (referenced by web clients)
      • Web. (web client business layer)
        • ViewModel. (view specific model)
        • {client facing product segment} [Commerce, CMS, CRM, Reporting, etc.]
  • 公司名。
    • 产品名称。
      • 数据。
      • 商业。(参考资料)
      • 模型。(POCO 和接口 - 所有人都引用)
      • 服务。(WCF服务层)
      • 服务客户端。(由网络客户端引用)
      • 网。(web客户端业务层)
        • 视图模型。(查看具体型号)
        • {面向客户的产品细分} [商务、CMS、CRM、报告等]

To explain the Services/Service Client...I use an IoC (currently StructureMap) that allows my WebClient to either speak directly to the Business layer or to be redirected to speak through the ServiceClient through Services to the Business layer. This gives me the flexibility to either deploy my app layer to my web applications or to distribute pieces of my business layer (app layer) to different servers by way of WCF/SOA principles.

为了解释服务/服务客户端……我使用了一个 IoC(当前是 StructureMap),它允许我的 WebClient 直接与业务层对话,或者被重定向以通过服务通过 ServiceClient 与业务层对话。这使我可以灵活地将我的应用程序层部署到我的 Web 应用程序,或者通过 WCF/SOA 原则将我的业务层(应用程序层)的各个部分分发到不同的服务器。