简体   繁体   English

命名空间和类名指南

[英]Namespace and class name guidelines

Im having problems naming my classes and services correctly when utils and other help classes are involved. 在涉及utils和其他帮助类时,我正确地命名我的类和服务时遇到问题。

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. 但这并不是100%感觉,因为其他服务中可能有更多的DateValidator,这很容易导致不必要的引用。

And also the Services.EventService.EventService.cs includes the class name in the namespace, which is no good either. 而且,Services.EventService.EventService.cs也包含命名空间中的类名,这也不好。 You could use Services.Event.EventService.cs, but there is of course already an entity with that name. 您可以使用Services.Event.EventService.cs,但当然已经有一个具有该名称的实体。

This is the domain model. 这是域模型。

You can put validators (and other classes) that are used in more than one service in seperate namespace named eg. 您可以将多个服务中使用的验证器 (和其他类)放在名为eg的单独命名空间中 CommonValidators. CommonValidators。
You can change name of EventService.cs instead of changing name of the namespace - maybe Main.cs or Default.cs ? 您可以更改EventService.cs的名称而不是更改名称空间的名称 - 可能是Main.csDefault.cs
I am supposing that your service has a contract in interface so this would indicate main/default implementation of service contract. 我假设您的服务在接口中有合同,因此这将指示服务合同的主要/默认实现。

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) 使用应用程序名称作为顶级(Microsoft建议根据其指南使用公司名称)
  • I wouldn't put coordinators in a separate namespace if there are just a few. 如果只有一些,我不会将协调员放在一个单独的命名空间中。

Microsoft guidelines can be found here: Framework Design Guidelines 可以在此处找到Microsoft指南: 框架设计指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM