命名空间和类名指南

发布于 2024-12-03 11:03:29 字数 787 浏览 0 评论 0原文

当涉及 utils 和其他帮助类时,我在正确命名我的类和服务时遇到问题。

您将如何构建以下内容:

EventService.cs
EventServiceUtils.cs
EventServiceValidators.cs
EventServiceCoordinator.cs

等等...

我有多个服务与上述服务具有相同的需求。 一种想法是将所有这些分离到一个合适的命名空间中,使其看起来像这样:

Services.EventService.EventService.cs //(the actual service)
Services.EventService.Validators.DateValidator.cs
Services.EventService.Validators.ParticipantValidator.cs
Services.EventService.Coordinators.ParticipantCoordinator.cs
Services.EventService.ExtensionMethods.Extensions.cs

等等。每个命名空间当然都是一个单独的文件夹。 但这感觉并不是 100%,因为其他服务中可能有更多的 DateValidator,这很容易导致不需要的引用。

而且 Services.EventService.EventService.cs 在命名空间中包含类名,这也不好。您可以使用 Services.Event.EventService.cs,但当然已经有一个具有该名称的实体。

这就是领域模型。

Im having problems naming my classes and services correctly when utils and other help classes are involved.

How would you structure the following:

EventService.cs
EventServiceUtils.cs
EventServiceValidators.cs
EventServiceCoordinator.cs

etc...

I have multiple services with the same needs as the above service.
One thought is to separate all of this into a suitable namespace, making it look something like this:

Services.EventService.EventService.cs //(the actual service)
Services.EventService.Validators.DateValidator.cs
Services.EventService.Validators.ParticipantValidator.cs
Services.EventService.Coordinators.ParticipantCoordinator.cs
Services.EventService.ExtensionMethods.Extensions.cs

and so on. Every namespace is of course a separate folder.
But this doesnt feel 100%, since there are probably more DateValidators in the other services, which can easily lead to an unwanted reference.

And also the Services.EventService.EventService.cs includes the class name in the namespace, which is no good either. You could use Services.Event.EventService.cs, but there is of course already an entity with that name.

This is the domain model.

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

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

发布评论

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

评论(2

我还不会笑 2024-12-10 11:03:29
AppName.Services.Events.EventService.cs //(the actual service)
AppName.Services.Events.ParticipantValidator.cs
AppName.Services.Events.ParticipantCoordinator.cs
AppName.Validators.DateValidator.cs
AppName.Text.Extensions.cs

要点:

  • 将扩展添加到命名空间,描述它们正在扩展的内容
  • 添加通用验证器执行通用命名空间
  • 使用应用程序名称作为顶层(微软根据其指南建议使用公司名称)
  • 如果有的话,我不会将协调器放在单独的命名空间中只是几个。

可以在此处找到 Microsoft 指南:框架设计指南

AppName.Services.Events.EventService.cs //(the actual service)
AppName.Services.Events.ParticipantValidator.cs
AppName.Services.Events.ParticipantCoordinator.cs
AppName.Validators.DateValidator.cs
AppName.Text.Extensions.cs

Points:

  • Add extensions to a namespace describing what they are extending
  • Add common validators do a general namespace
  • Use an app name as the top level (Microsoft recommends a company name per their guidelines)
  • I wouldn't put coordinators in a separate namespace if there are just a few.

Microsoft guidelines can be found here: Framework Design Guidelines

甜宝宝 2024-12-10 11:03:29

您可以将在多个服务中使用的验证器(和其他类)放置在名为例如的单独的命名空间中。通用验证器。
您可以更改 EventService.cs 的名称,而不是更改命名空间的名称 - 可能是 Main.csDefault.cs
我假设您的服务在接口中有一个合同,因此这将指示服务合同的主要/默认实现。

You can put validators (and other classes) that are used in more than one service in seperate namespace named eg. CommonValidators.
You can change name of EventService.cs instead of changing name of the namespace - maybe Main.cs or Default.cs?
I am supposing that your service has a contract in interface so this would indicate main/default implementation of service contract.

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