本文是小编为大家收集整理的关于在.NET中,我们可以看到,在文件之间划分命名空间的最佳做法/准则。在文件之间划分命名空间的最佳做法/准则?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
将应用程序代码(app_code)分为单独的文件应该是什么?
我发现,随着时间的流逝,原始文件与名称空间层次结构的演变方式不太匹配.如何随着时间的推移将应用程序代码容器保持直觉的组织?
文件划分应针对什么目的?代码可移植性?关注点分离?一般功能上下文?变化的频率?他们应该努力与班级的1-1关系吗?
将代码分为许多较小的文件与将代码分解为几个文件有什么含义?
我经常考虑过这一点,但从未真正得出任何适用于所有情况的一般结论.
推荐答案
这个问题的答案不是绝对的,因为它通常取决于您手头的任务.如果您正在创建某种SDK来重用其他人,那么名称空间非常重要.但是,如果您仅使用几个类创建一个内部工具,则名称空间几乎并不重要.
类
一般而言,课程应该具有自己的文件,因为这简化了人们如何在代码解决方案中导航,帮助开发和维护(例如,当每个人都更改相同的文件时,很难合并更改).在某些情况下,例如:
-
当有嵌套类时,每个嵌套类都可以拥有自己的文件.
-
当课程中有自动生成部分时,例如设计师代码.
-
当类上有固定部分时,例如一组公共的隐藏属性或接口的常见实现.
在我们的一个项目中,我们有一个常见的界面实现,许多类都可以公开.由于我们没有多个继承,因此我们采用了一种混合方法,从而为每个班级自动化一个额外的文件.这可以手动完成,而不是自动(并且最初是).
还有其他情况,但这在某种程度上是主观的,并且取决于您自己的项目的要求.
名称空间
名称空间通常应集中在您类型的合理分组上.名称空间应允许开发人员直观地找到他们想要的东西.对于许多小型项目,一个单个名称空间(例如MyAwesomeTool)就足够了,但是对于一个较大的项目,许多类别需要更合乎逻辑的分组.这样的大型项目(例如SDK或.NET BCL)依靠命名空间来击穿原本绝大多数类型的类型.每个名称空间级别提供了可能在那里发现的内容的其他信息,例如System.Windows.Forms或System.Drawing或Microsoft.VisualBasic.
.创建名称空间时,必须按照该名称空间和关联项目的目的进行所有考虑.如果该项目是内部且较小的,请称呼命名空间您喜欢的内容,因为它仅需要分组您的类型;如果该项目在外部可见或包含大量类型,请仔细考虑逻辑和有意义的分组,这些分组将使其他人能够直观地找到他们所寻找的类型.
结论
在每种情况下都没有努力的规则.将代码排列到文件中的方式与您自己的开发过程有关,从而影响您和您的团队;一个文件中的所有课程都将是地狱,但是编译的产品不会采取任何不同(前提是一个文件方法没有导致错误),而您的命名空间的安排与未来的开发和消费者有关这些名称空间,因此弄错了它的后果可能会更加严重.
- 旨在以简化当前发展和未来维护的方式组织您的课程.
- 旨在以简化所有开发以及适当的最终用户体验的方式组织您的命名空间.
其他推荐答案
类和源文件之间应该有一对一的映射.
命名空间应被认为是一个包含一个或多个类的软件包.在可能的情况下,将它们表示为Visual Studio Project窗口中的文件夹/过滤器可能是有用的.
如果您找到了一个相当大的课程,则认为将受益于单独的文件,然后考虑重构和分配课程.
(可接受的)例外是生成可视化工作室将UI放置在单独文件中的UI的代码.我建议将其放在自己的文件中,并尽可能将其视为透明的编辑文件.
其他推荐答案
我看到的大多数建议都说每种公共类型都应在自己的文件中,名称空间应代表应用程序的文件夹结构.
问题描述
What should be the general guidelines/gotchas for dividing application code (App_Code) into separate files?
I've found that over time, the original files do not match up well with how the namespace hierarchy evolves. How do I keep application code containers organized intuitively over time?
What PURPOSE should the file divisions aim towards? Code portability? Separation of concerns? General functional context? Frequency of change? Should they strive for 1-1 relationship with classes?
What are the implications of splitting the code into MANY smaller files vs consolidated into few files?
I've often thought about this but never really reached any general conclusions that apply to all situations.
推荐答案
The answer to this question is not absolute as it often depends on the task you have at hand. If you're creating some kind of SDK for reuse by others, then namespaces are very important; however, if you're creating an in-house tool with just a few classes, the namespaces are pretty much unimportant.
Classes
Generally speaking, classes should have their own file as this simplifies how people navigate around the code solution, helping with development and maintenance (it's much harder to merge changes when everyone is changing the same files, for example). It can be acceptable to split one class across multiple files in some situations such as:
When there are nested classes, each nested class could have its own file.
When there are auto-generated portions to the class such as designer code.
When there are fixed portions to the class such as a common set of hidden properties or a common implementation of an interface.
In one of our projects, we have a common implementation of an interface that many classes expose. As we don't have multiple inheritance, we take a mix-in approach whereby we autogenerate an additional file for each class. This could be done manually, instead of automatically (and was, originally).
There are other situations, but this is somewhat subjective and dependent on your own project's requirements.
Namespaces
Namespaces should generally focus on sensible groupings of your types. A namespace should allow a developer to intuitively locate what they are looking for. For many small projects, a single namespace such as MyAwesomeTool is sufficient, but for a larger project with many classes will need a more logical grouping. Such large projects, like SDKs or the .NET BCL rely on the namespaces to breakdown the otherwise overwhelmingly large collection of types. Each namespace level provides additional information of what might be found there, such as System.Windows.Forms or System.Drawing or Microsoft.VisualBasic.
When creating namespaces, every consideration must be given to the purpose of that namespace and the associated project. If the project is in-house and small, call the namespace what you like as it is merely a necessity for grouping your types; if the project is externally visible or contains a large amount of types, think carefully about logical and meaningful groupings that will enable others to intuitively find the types they are looking for.
Conclusion
There are no hard and fast rules that work in every situation. How you arrange your code into files relates to your own development processes, impacting you and your team; all your classes in one file will be hell to develop with but the compiled product won't act any different (provided the one file approach didn't lead to errors), whereas the arrangement of your namespaces relates to future development and the consumers of those namespaces, so the consequences of getting it wrong can be more serious.
- Aim to organise your classes in a way that simplifies current development and future maintenance.
- Aim to organise your namespaces in a way that simplifies all development and, where appropriate, the experience of your end users.
其他推荐答案
There should be a one-to-one mapping between classes and source files.
Namespaces should be thought of as a package that may encompass one or more classes. Where possible it can be useful to represent these as folders/filters in the Visual Studio project window.
If you find a sizeable class you feel would benefit from being split into separate files then instead consider refactoring and splitting the class itself.
The (acceptable) exception to this is the code that generates the UI which Visual Studio places in a separate file. I would recommend leaving this in its own file and treating it as a transparent editor-owned file as much as possible.
其他推荐答案
Most recommendations I saw say that every public type should be in its own file and namespaces should represent the folder structure of the application.