C# 单一项目组织
由于各种原因,我正在将源文件重新组织为单个项目的单个解决方案:
这给我留下了许多命名空间,它们被分割在多个文件中。 到目前为止,我正在使用此约定:给定命名空间 Company.Project.A
,文件名为 A.f1.cs
、A.f2.cs
等等,并且 Company.Project.B
命名空间分为 B.f1.cs
、B.f2.cs
> 等。
考虑到单个项目的限制,是否有更好的方法来组织多个命名空间中的多个文件?
I am reorganizing my source files into a single solution with a single project, due to various reasons:
- a paranoic configured antivirus software;
- Advices on partitioning code through .NET assemblies
- Control component dependencies to gain clean architecture
- Benefit from the C# and VB.NET compilers perf
This leaves me with many namespaces, which are splitted across multiple files. So far, I am using this convention: given the namespace Company.Project.A
, the files are named A.f1.cs
, A.f2.cs
and so on, and the Company.Project.B
namespace is splitted across B.f1.cs
, B.f2.cs
, etc.
Given the single project restriction, are there any better ways to organize multiple files in multiple namespaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的 - 使用文件夹。
如果您在项目中创建文件夹,则该文件夹中的新类将自动使用该文件夹名称作为命名空间的基础。
例如,如果您有一个默认命名空间为“Company.Project”的项目和包含“Bar.cs”的文件夹“Foo”,您最终会得到:
Yes - use folders.
If you create a folder within a project, new classes within that folder will automatically use the folder name as the basis for the namespace.
For instance, if you have a project with a default namespace of "Company.Project" and a folder "Foo" containing "Bar.cs" you'll end up with:
所以解决方案就在这里。 这是文件夹。 但这有时很棘手。 首先,每个类有一个文件是个好主意。 如果您将多个类打包到一个文件中 - 随着时间的推移,您将很难找到它们。
关于文件夹的第二件事 - 如果您单击文件夹并选择例如“添加 -> 新项目”,则该项目将被放入所选文件夹中。 但要小心! 如果您要在文件夹之间移动文件,命名空间不会更新。
这是混乱项目的常见来源。 不久之后,您就可以得到一个项目,其中有组织整齐的文件夹和文件,但不反映名称空间。 例如,如果您在文件夹 MyFolder 中有类 MyClass,请确保该类的命名空间类似于 MyApp.MyFolder,而不是一些旧的垃圾。
因此,如果您不将类打包到一个文件中并观察类名称空间是否反映文件夹层次结构,那么您就走在了使您的项目非常易于阅读和导航的良好道路上。
So the solution is right here. It's Folders. But it's sometimes tricky. First of all it's kind of a good idea to have one file per class. If you will pack several classes into one file - you'll have problems with finding them with time.
Second thing about folders - if you will click on a folder and choose for example "Add -> New Item", this item will be put into selected folder. But watch out! If you will move files between folders, namespaces are not updated.
It's common source of messing project. Just after a while you can end up with a project where you have neat organized folder and files, but not reflecting namespaces. So for example, if you have class MyClass in folder MyFolder make sure, your namespace for this class is something like MyApp.MyFolder and not some old rubbish.
So If you will not pack classes into one file and watch if classes namespaces reflect folder hierarchy - you're on the good road to make you project very easy to read and navigate.
100%同意乔恩·斯基特的观点。
为了获得文件夹级别的更多概述,我们通过在文件夹前面添加下划线前缀来创建打破命名空间结构的文件夹。
100% agree with Jon Skeet.
To gain more overview at the folder level we're creating folders breaking the namespace structure by prefixing them with an underscore.