域模型命名空间命名约定

发布于 2024-10-11 12:29:26 字数 709 浏览 10 评论 0原文

问题:

对于 ASP.NET MVC 应用程序,我忽略域文件夹并将域模型写入单独的库项目中。

Visual Studio 具有这样的库项目,将逐个文件夹命名空间。例如,如果我的图书馆项目名为:

ES.eLearning.Domain

,并且我有一个名为 Services 的文件夹,它将自动将 Services 文件夹中的所有类命名为:

ES.eLearning.Domain.Services

这一切都很好,但罢工我太矫枉过正了。我的项目相对简单,因此不存在类名冲突的可能性。即使有,编译器也会选择它。

所以我只是手动将命名空间名称编辑为项目名称,即:

ES.eLearning.Domain

这样,在开发前端时,领域模型项目只是一个提供领域模型的黑盒子。我不认为将名称空间名称耦合到项目文件夹结构有什么意义,而不是将其视为一个功能块。

因此出现了两个问题:

  1. 这是不好的做法吗?如果是这样,为什么?请记住:这些都是中小型项目。

  2. 有没有办法告诉 VS 2010 如何命名我的命名空间?

谢谢

注意:在我的项目命名中,ES 是客户端,eLearning 是应用程序,Domain 是组件

PS:我在 SO 上发现的有关命名空间命名约定的其他问题解决了不同的问题。所以据我所知,不是重复的。

Issue:

For asp.net mvc apps, I ignore the domain folder and write the domain model in a separate library project.

Visual Studio, with such a library project, will namespace on a folder by folder basis. Eg, if my library project is called:

ES.eLearning.Domain

and I have a folder called Services, it will auto namespace all classes in the Services folder as:

ES.eLearning.Domain.Services

Which is all well and good, but strikes me as overkill. My projects are relatively simple and so there is no chance of class name clashes. Even if there were, the compiler would pick it up.

So I just manually edit the namespace names to the project name, ie:

ES.eLearning.Domain

That way, when developing the front end, the domain model project is just a black box that delivers, well, a domain model. And I don't see the point of coupling the namespace names to the projects folder structure, as opposed to treating it as one functional block.

So two questions arise:

  1. Is this bad practice? If so, why? Remember: these are small to medium projects.

  2. Is there a way to tell VS 2010 how to name my namespaces?

Thanks

Note: In my naming of the project, ES is the client, eLearning is the app, Domain is the component

PS: other questions on namespace naming conventions I have found on SO address different issues. So not a duplicate as far as I can determine.

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

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

发布评论

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

评论(4

來不及說愛妳 2024-10-18 12:29:26

要防止特定文件夹强制其内容使用文件夹名称作为命名空间部分,您可以右键单击该文件夹,选择“属性”,然后将“命名空间提供程序”更改为False。不过,需要对其适用的所有文件夹执行此操作,因此我不确定这是否真正完全回答了您的问题。

编辑:看起来ReSharper 添加了此功能。

To prevent a particular folder forcing its contents to use the folder name as a namespace part, you can right-click the folder, choose "Properties" and then change "Namespace Provider" to False. That would need to be done to all the folders to which it applies though, so I'm not sure if that's really answered your question fully.

Edit: looks like ReSharper adds this functionality.

哑剧 2024-10-18 12:29:26

引导您的 IDE 使用适合您的特定环境的命名约定是一个很好的做法。您可以使用项目属性的“应用程序”选项卡中的默认命名空间框来更改 VS 自动插入到类文件中的命名空间。

Coaxing your IDE to use a naming convention that suits your particular environment is good practice. You can use the default namespace box in the Application tab of the project properties to change the namespace that VS automatically inserts in your class files.

感情旳空白 2024-10-18 12:29:26

我不确定是否可以在 VS2010 中配置来禁用此功能..但我不会这样做。

如果您的代码文件放置在Domain文件夹中,那么它的命名空间应该是Company.Domain。如果域有子文件夹 Services,则命名空间应为 Company.Domain.Services。这是一个很好的(重要的是——常见的、实践的)。

你最好遵守规则,而不是违反规则:)

I'm not sure if it possible to configure in VS2010 to disable this.. but I would not do that.

If your code file placed in Domain folder, its namespace should be Company.Domain. If domain has sub folder Services, namespace should be Company.Domain.Services. It is a good (and whats important - common, practice).

You better stick to the rules, over violating them :)

烟雨凡馨 2024-10-18 12:29:26

我的建议是在命名名称空间、文件夹和类时使用单数结尾。例如,

namespace ES.eLearning.Domain.Services

将首选为

ES.eLearning.Domain.Service // <-- note missing "s"

源代码中的另一个用法示例:

ES.eLearning.Domain.Constants.FileTypes.Text

可能“更好”理解,因为

ES.eLearning.Domain.Constant.FileType.Text

它是所有个人喜好,而不是规则。只是一件小事,可能会使代码(命名空间)更短,同时更易于阅读。

附言。另一方面,ASP.NET MVC 并不遵循这个“规则”:

Content
Controllers
Models
Scripts
Views

但无论如何,没有什么可以阻止您在源代码中“重命名”它们。

My suggestions to use singular ending while naming namespaces, folders and classes. For example

namespace ES.eLearning.Domain.Services

would be preffered as

ES.eLearning.Domain.Service // <-- note missing "s"

And another example of usage in source code:

ES.eLearning.Domain.Constants.FileTypes.Text

could be "better" to understand as

ES.eLearning.Domain.Constant.FileType.Text

It is all of the personal preferences and it is not a rule. Just a small thing that might make the code (namespace) shorter and easier to read at the same time.

PS. On the other hand ASP.NET MVC does not follow this "rule":

Content
Controllers
Models
Scripts
Views

But nothing keeps you "renaming" them in the source code anyway.

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