在 C# 中哪里定义特定于服务的枚举?
假设我有一个接口 ICustomerService
并且该接口需要一个枚举,例如 ProcessingMode
:
namespace MyServices {
public enum ProcessingMode { Immediate, Normal, Slow }
public interface ICustomerService {
void Process(ProcessingMode mode);
}
}
枚举不能在 C# 中的接口内部定义(但显然是在 VB 中?)。但是将其放在界面之外会使包含名称空间 MyServices
变得混乱。不好,因为其他接口可能还需要一个名为 ProcessingMode
的不同枚举。
那么有什么“最佳实践”来解决这个问题吗?
我可以看到一些选项,但对我来说似乎没有一个是完全正确的:
将接口和枚举都放在特定于服务的命名空间中:
命名空间 MyServices.CustomerService { 公共枚举处理模式 { 立即、正常、慢速 } 公共接口 ICustomerService { void Process(ProcessingMode 模式); } }
虽然它可以工作并避免包含命名空间中的混乱,但它确实会导致命名空间扩散,并且我猜想服务实现可能会与命名空间发生冲突或重复它:
MyNamespace.CustomerService.CustomerService
。或者将服务实现放在哪里以及如何称呼它?仅将枚举放入单独的命名空间(或静态类)中:
命名空间 MyServices { 命名空间 CustomerServiceTypes { 公共枚举处理模式 { 立即、正常、慢速 } } 公共接口 ICustomerService { void Process(CustomerServiceTypes.ProcessingMode 模式); } }
与选项 1 的问题相同,但还需要“在任何地方”限定枚举名称。
添加枚举名称前缀,例如
CustomerServiceProcessingMode
。避免名称空间扩散以及与服务实现发生名称冲突的风险,但会导致枚举名称过长且丑陋。
那么你会怎么做呢?
Let's say I have an interface ICustomerService
and this interface requires an enum, e.g. ProcessingMode
:
namespace MyServices {
public enum ProcessingMode { Immediate, Normal, Slow }
public interface ICustomerService {
void Process(ProcessingMode mode);
}
}
The enum can't be defined inside the interface in C# (but apparently in VB?). But having it outside the interface clutters the containing namespace MyServices
. Not good, since other interfaces might also require a different enum called ProcessingMode
.
So are there any "best practices" how to solve this?
I can see a few options, but none of them seems quite right to me:
Put both the interface and the enum in a service-specific namespace:
namespace MyServices.CustomerService { public enum ProcessingMode { Immediate, Normal, Slow } public interface ICustomerService { void Process(ProcessingMode mode); } }
While it works and avoids clutter in the containing namespace, it does cause namespace proliferation, and I guess a service implementation might clash with the namespace or duplicate it:
MyNamespace.CustomerService.CustomerService
. Or where would you put the service implementation and what would you call it?Put just the enum in a separate namespace (or a static class):
namespace MyServices { namespace CustomerServiceTypes { public enum ProcessingMode { Immediate, Normal, Slow } } public interface ICustomerService { void Process(CustomerServiceTypes.ProcessingMode mode); } }
Same problems as option 1 but also requires a enum name to be qualified "everywhere".
Prefix the enum name, e.g.
CustomerServiceProcessingMode
. Avoids namespace proliferation and risk of name clashes with service implementations, but causes long ugly enum names.
So how would you do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需将接口和枚举放在一个命名空间下即可。无论如何,您都必须将
enum
设为public
,因为您在服务内部使用它。因此,通过将接口和枚举放在一个命名空间下,可以使接口的实现者只包含一个命名空间。就像
System.Drawing
有Color
枚举一样。Simply put interface and enum under one namespace. In any case you will have to make the
enum
public
, because you are using it inside service. So by putting interface and enum under one namespace, makes the implementer of interface to include only one namespace.Like
System.Drawing
hasColor
enum.仅仅因为是客户服务进行处理并不意味着您必须将枚举命名为“CustomerService.....” - 如果枚举是针对客户的,则只需将其命名为 CustomerProcessingMode;特别是如果枚举将成为商务舱的属性。
如果有多种处理客户的方式需要不同的处理模式,那么您将不得不使用服务的名称,但您不喜欢该名称的外观的原因是因为您的服务名称很垃圾: ) “客户服务” - 这到底是做什么的?
MailingService(在接口的实现者上运行)将是一个很好的服务示例,因为名称告诉您该服务的用途,而不是它的工作原理。甚至“CustomerRepository”也是一个好名字,因为该名称告诉您它的用途。
命名服务“CustomerService”没有任何指示服务的任务,通常最终会执行与客户有关的任何事情 - 一种“瑞士军刀”服务,它应该真正有一个单一的重点任务,而不是一个可以用一个人可以做的所有事情的目录。 顾客。
Just because it is the customer service that does the processing doesn't mean you have to name the enum "CustomerService....." - if the enum is for the Customer then just call it CustomerProcessingMode; especially if the enum is going to be a property on the business class.
If there will be many ways of processing the customer which will require different processing modes then you will have to use the name of the service instead, but the reason you don't like the look of the name is because your service name is rubbish :) "CustomerService" - what does that do exactly?
MailingService (which runs on implementers of an interface) would be a good example for a service, because the name tells you what the service does, NOT what it works on. Even "CustomerRepository" is a good name because the name tells you what it does.
Naming services "CustomerService" has no indication of the task of the service, and usually end up performing anything and everything to do with customers - kind of a "Swiss Army Knife" service which should really have a single focussed task rather than be a catalogue of everything one can do with a customer.
我可能会使用两者的组合:
我不会太担心名称冲突,您的枚举名称是服务合同的一部分,就像接口名称一样。您真的不必担心有人在同一名称空间中定义相同的接口,对吗?对于作为合约一部分的枚举和其他公共类型也是如此。
I would probably use the combination of two:
I would not be worried to much about clashing names, your enum name is a part of your service contract, just as the interface name. You don't really worry about someone defining the same interface in the same namespace, do you? Same for enum and other public types that are part of the contract.
我会选择#3。名称应该具有描述性,并且第一眼就能看出它们的用途。名字的长度对我来说不是标准。我会发现拥有多个具有不同含义的
ProcessingMode
枚举更令人恼火。只是我的2分钱...
I would opt for #3. Names should be descriptive and at first glance show what they're for. Length of the name is no criterium for me. I would find it much more irritating to have multiple
ProcessingMode
enums with different meanings.Just my 2cent...