简体   繁体   English

什么是枚举的好替代品?

[英]What's a good substitute for an enum?

I'm writing a web service that returns different types of errors. 我正在写一个返回不同类型错误的Web服务。
Each method can return one of the three basic types of errors: 每个方法都可以返回三种基本错误类型之一:
General , InvalidInput , or Non . GeneralInvalidInputNon
In addition to those three possible values, each method can have it's own errors (eg for SignIn method - InvalidPassword) - but each method can return only one error. 除了这三个可能的值之外,每个方法都可以有自己的错误(例如,SignIn方法-InvalidPassword)-但是每个方法只能返回一个错误。
So for example the SignIn method will be able to return one of the following error types: General, InvalidInput, Non, InvalidPassword. 因此,例如SignIn方法将能够返回以下错误类型之一:General,InvalidInput,Non,InvalidPassword。
At first I thought of using enums, but I now think that the error types should implement inheritance because there are the basic three types, and each new method's error types inherit from that.. But I can't really think how. 起初我考虑使用枚举,但是现在我认为错误类型应该实现继承,因为存在基本的三种类型,每种新方法的错误类型都继承自该类型。但是我真的无法考虑如何实现。 I thought of using a static class - but then it will only have one string static field - and inheritance is irrelevant again... 我想到了使用静态类-但是它将只有一个字符串静态字段-并且继承又不再相关...
Another problem with enums is that what the web service's client will get eventually is a meaningless int (through json) 枚举的另一个问题是,Web服务的客户端最终将获得的是无意义的int(通过json)

So my question is: What is a good way of conveying the idea that there are three basic possible values, and you can add to those to make a new type of errors? 所以我的问题是:传达三个基本可能值的想法的最佳方法是什么,您可以添加这些值来产生一种新的错误类型?

It would be best if you reconsidered your interfaces. 最好是重新考虑接口。

It is by far better to use exceptions over error codes not only because it is so easy to forget about checking for an error code, but also because it is difficult to maintain them, keep them unique and meaningful. 迄今为止,最好使用错误代码上的异常,这不仅是因为它很容易忘记检查错误代码,而且还因为难以维护它们,使它们保持唯一和有意义。

John Saunders states in a similar thread: 约翰·桑德斯指出类似的线程:

[...] you should throw a SoapException . [...]您应该抛出SoapException This will translate more or less directly into a SOAP Fault. 这或多或少将直接转化为SOAP Fault。 A SOAP Fault is the appropriate way to indicate an error with a web service operation for the same reason that Exceptions are better than return status in a normal method - you don't have to check the return status at the point of the call. SOAP Fault是指示Web服务操作错误的适当方法,其原因与Exception优于普通方法中的返回状态相同,您不必在调用时检查返回状态。

Besides throwing a SoapException you may also throw arbitrary exceptions. 除了抛出SoapException您还可以抛出任意异常。 ASP.NET will wrap these exceptions into a SoapException that gets passed to the client. ASP.NET会将这些异常包装到一个SoapException ,该异常将传递给客户端。 The client can access the details of the exception in the inner exception of the SoapException . 客户端可以在SoapException的内部异常中访问异常的详细信息。

For further explanations see also: 有关更多说明,请参见:

Handling and Throwing Exceptions in XML Web Services XML Web Services中的处理和引发异常

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

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