dotnet 中的命名空间命名约定
我通常使用此约定:
CompanyName.ApplicationName.Functionality
例如
Acme.EmailService
Acme.EmailService.Dal
Acme.EmailService.BusinessLogic
Acme.EmailService.BusinessLogic.ErrorHandling
但在我目前工作的地方,他们使用:
CompanyName.Functionality.ApplicationName
Acme.EmailService
Acme.Dal.EmailService
Acme.BusinessLogic.EmailService
Acme.BusinessLogic.EmailService.ErrorHandling
最后一个命名空间对我来说看起来有点奇怪。它是业务逻辑项目的子文件夹,因此默认情况下文件夹名称会附加到命名空间。
我看过很多命名约定标准,但似乎没有提到这个问题。
每种方法的优点和缺点是什么?
I typically use this convention:
CompanyName.ApplicationName.Functionality
e.g.
Acme.EmailService
Acme.EmailService.Dal
Acme.EmailService.BusinessLogic
Acme.EmailService.BusinessLogic.ErrorHandling
but where I currently work, they use:
CompanyName.Functionality.ApplicationName
Acme.EmailService
Acme.Dal.EmailService
Acme.BusinessLogic.EmailService
Acme.BusinessLogic.EmailService.ErrorHandling
The last namespace looks a bit weird to me. It is a subfolder of the businesslogic project so by default the foldername is appended to the namespace.
I have seen plenty of naming convention standards but none seem to mention this issue.
What are the Pros and Cons of each approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请遵循 Microsoft 创建的命名准则:
为命名空间选择的名称应指示命名空间中的类型提供的功能。例如,System.Net.Sockets 命名空间包含使开发人员能够使用套接字通过网络进行通信的类型。
命名空间名称的一般格式如下:
例如,Microsoft.WindowsMobile.DirectX。
请使用公司名称作为命名空间名称的前缀,以防止不同公司的命名空间具有相同的名称和前缀。
http://msdn.microsoft.com/ en-us/library/vstudio/ms229026(v=vs.100).aspx
在这种情况下,我认为 BusinessLogic 或 Dal 只是层抽象,实际上不属于命名空间。因此,我不会使用任何示例,只是
那些额外的名称空间只会让找到正确的接口/类变得更加困难。无论如何,您正在使用多个程序集,对吧?无论如何,您的不同层都会被分离(在组件的帮助下)。
问问自己层命名空间到底给了你什么?每个命名空间中有多少个类? 10岁以下?它会让您的代码更难还是更容易?
Follow the naming guidelines that Microsoft has created:
The name chosen for a namespace should indicate the functionality made available by types in the namespace. For example, the System.Net.Sockets namespace contains types that enable developers to use sockets to communicate over networks.
The general format for a namespace name is as follows:
For example, Microsoft.WindowsMobile.DirectX.
Do prefix namespace names with a company name to prevent namespaces from different companies from having the same name and prefix.
http://msdn.microsoft.com/en-us/library/vstudio/ms229026(v=vs.100).aspx
In this case I would argue that BusinessLogic or Dal is just layer abstractions and really do not belong in namespaces. Hence I would not use any of you examples, just
Those extra name spaces just makes it harder to find the proper interface/class. You are using multiple assemblies anyway, right? Your different layers will be separated anyway (with the help of the assemblies).
Ask yourself what the layer namespaces really gives you? How many classes are in each namespace? Under 10? Does it make it harder or easier to work with your code?
首先,如果我有任何库/通用代码,它会进入非客户特定的命名空间(这里,CompanyName 是我工作的地方):
等等。
其次,如果它是客户特定的,我使用 ClientName.Application。功能形式。这是因为,如果它不常见(因此会在上述库和命名空间中),我相信该功能属于应用程序,而不是相反。
显然,只是我的意见;)
编辑:澄清,我使用
ClientName
作为特定客户端的顶级命名空间。我们与一些客户有多个项目,因此您最终可能会得到:等等......
Firstly, if I have any library/common code, it goes in a non-customer specific namespace (here, CompanyName is where I work):
etc.
Secondly, if it is customer-specific I use the
ClientName.Application.Functionality
form. This is because if it is not common (and therefore would be in the aforementioned libraries & namespaces), I believe that the functionality belongs to an Application, not the other way around.Just my opinion, obviously ;)
EDIT: Clarification, I use
ClientName
as the Top-level namespace for a particular client. We have multiple projects with some clients, and so you can end up with:etc...
我相信,如果子层太多,使用第二个约定可能会把事情搞砸。例如,我们的一个项目包含 200 多个项目,我选择按项目而不是按功能对命名空间进行分组,因为除了常见(真正常见)功能之外,功能属于应用程序。
考虑一下框架,我们可能会使用像 System.Ui.Web 这样的命名空间,它包含所有与 ui 相关的子命名空间,而不是我们当前的体系结构 System.Web.Ui,它按平台/应用程序对命名空间进行分组。
我不相信有一个确切的事实,我相信习惯是存在的,很难说服人们改变惯例。
I believe that using the second convention may mess things up if you have too many sub layers. For instance one of our projects contains more than 200 projects and i choose to group namespaces by project, not by functionality because the functionality belongs to application except common (really common) functions.
Think about Framework, we might be using namespaces like System.Ui.Web which contains all ui related sub namespaces instead of our current architecture, System.Web.Ui which groups namespaces by platform / application.
I don't believe that there is a exact truth, i believe that there are habits and it is very hard to convince people to change the conventions.