命名空间和类名指南

3

涉及到工具类和其他帮助类时,我在命名类和服务时遇到了问题。

你应该如何组织以下内容:

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

等等,每个命名空间当然是一个单独的文件夹。

但这并不完美,因为其他服务中可能有更多的DateValidators,这很容易导致不必要的引用。

而且Services.EventService.EventService.cs在命名空间中包含类名,这也不好。你可以使用Services.Event.EventService.cs,但显然已经有一个同名实体了。

这是领域模型。

2个回答

1
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的建议,每个公司都应该有一个公司名称)
  • 如果只有几个协调器,则不需要将其放在单独的命名空间中。

可以在这里找到Microsoft指南:Framework Design Guidelines


1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接