API设计:修改返回附加错误信息
我有一个 API 定义如下
bool IsValid()
现在,我想修改该 API,以便如果返回值为 false,那么我需要额外的
验证失败的原因的信息。
我正在寻找一个优雅的方式来解决问题...以下是我的选项
选项 1:bool IsValid(ref string errorMessage)
仅当结果为 false 时才会更新 errorMessage
选项 2:
class Result<T>
{
bool Succeeded;
T Argument;
}
Result<string> IsValid()
选项 3:
void Validate();
//throw an exception if it invalid, just return if succeeded.
我对上面列出的任何选项都不满意。所以,想知道是否有任何我可能不知道的优雅解决方案
I have an API defined as follows
bool IsValid()
Now, I want to modify the API so that if the return value is false, then I need additional
information why the validation failed.
I'm looking for an elegant want to solve the problem... Here are the options I have
Option 1:bool IsValid(ref string errorMessage)
errorMessage is updated only if the result is false
option 2:
class Result<T>
{
bool Succeeded;
T Argument;
}
Result<string> IsValid()
Option 3:
void Validate();
//throw an exception if it invalid, just return if succeeded.
I'm not comfortable with any of the options listed above. So, wondering if there is any graceful solution that I might not be aware of
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能的错误消息是固定的集合吗?在这种情况下,我将在枚举中定义它们,然后创建一个返回枚举类型值的 Validate() 方法。这样做的另一个优点是,如果您需要向用户显示消息,则可以轻松使用本地化消息。
Are your possible error messages a fixed set? In that case, I would define them in an enumeration, then make a
Validate()
method that returns a value of the enum type. This has the added advantage that, if you need to show the message to the user, you can easily use localized messages.我会选择
或者如果可以的话,更好:
其中 ValidCode 将是一个枚举
I'd go for
or if you can, better yet:
where ValidCode would be an enum
如果必须,您可以使用“string isValid()”,其中该方法将在成功时返回 null 或空字符串,否则返回某种错误消息。
但我个人永远不会这样做。让 API 执行不止一件事会增加复杂性。
If you must you could use "string isValid()" where the method would return null or empty string on success and some kind of an error message otherwise.
I would never do that personally though. Having APIs do more than one thing increases the complexity.
如果您使用动态类型语言,您可以执行以下操作:
但是,我真的很喜欢异常方法。它是标准的,可以在失败代码之上的任何级别进行处理。
If you were using a dynamically typed language you could do something like:
However, I would really spring for the exception method. It's standard and can be handled at any level above the failing code.
以防万一您想坚持使用选项 3:当验证因输入错误而失败时,validate() 不应抛出异常。异常应该在异常情况下抛出,而不是在您可能期望输入错误的情况下抛出。因此,我将按以下方式设计它
,其中 ValidationResult 可能是
验证方法应该仅在无法验证时抛出异常,例如,当它必须在数据库中查找值但无法建立连接时。
编辑:哇。没看到帖子的日期……我猜我有点晚了……
Just in case you wants to stick with option 3: validate() should not throw an exception in cases the validation fails because of wrong input. Exceptions should be thrown in exceptional cases and not in cases where you might expect wrong input. So I would design it in the following way
where ValidationResult could be
The validation method should only throw an exception when it is not able to validate, e.g. when it has to lookup values in a database but a connection could not be established.
EDIT: Wow. Didn't see the date of the post.... Guess I am a bit late...