重载最佳实践
我想使用两个静态方法来处理错误。其中一个传递异常对象,另一个仅在需要报告错误时使用,该错误将是基于文本的消息(字符串 errorMessage)。
这两种方法中的代码几乎相同,只是消息的构建方式和发送到日志文件的方式不同。我怎样才能重构这个,这样我就不会重复代码?
public static void ReportError(Exception exceptionRaised, string reference, string customMessage, bool sendEmail)
{
// get filename
// check if logfile exists, blah, blah
// build up message from exception, reference & custom message using string builder
// save message
// email error (if set)
}
public static void ReportError(string errorMessage, string reference, bool sendEmail)
{
// get filename
// check if logfile exists, blah, blah
// build up message from errorMessage & reference string builder
// save message
// email error (if set)
}
谢谢。
I have two static methods that I want to use for error handling. One of which passes the exception object and the other is just used if needing to report an error which would be a text based message (string errorMessage).
The code within the two methods is pretty much the same with the exception of how the message is build up and sent to a log file. How can I refactor this so that I'm not duplicating code?
public static void ReportError(Exception exceptionRaised, string reference, string customMessage, bool sendEmail)
{
// get filename
// check if logfile exists, blah, blah
// build up message from exception, reference & custom message using string builder
// save message
// email error (if set)
}
public static void ReportError(string errorMessage, string reference, bool sendEmail)
{
// get filename
// check if logfile exists, blah, blah
// build up message from errorMessage & reference string builder
// save message
// email error (if set)
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更复杂的版本:
A more complicated version:
其他答案都很好,但我想我应该添加一些东西来避免我遇到的维护问题,这
往往是非常烦人的,当你必须重构所有这些方法只是因为你想要更改其中一种方法中的参数。
The other answers are good but I thought I would add something to avoid that I have come across that tends to be a maintenance pain in the ...
This tends to be very anoying when you have to refactor all these methods just cause you want to change a parameter in one of the methods.
一个简单的解决方案是将重复的代码提取到它自己的方法中,如下所示:
这当然不是最优雅的方法,但它是一个开始;-)
One simple solution would be to extract the duplicate code into it's own method, like this:
This is certainly not the most elegant way to do it, but it is a start ;-)
重载方法时,请确保第一个参数始终相同。否则有点混乱。在您的情况下,将异常作为最后一个参数。
至于代码重复。让这两个方法都调用第三个受保护的方法来进行实际处理。
异常方法可以在调用第三个方法之前格式化异常并将其添加到消息参数中。
When overloading methods, make sure that the first parameters always are the same. It's a bit confusing otherwise. In your case, make the exception the last parameter.
As for code duplication. Make both methods call a third protected method which does the actual handling.
The exception method can format the exception and add it to the message parameter before calling the third method.
鉴于您所做的所有不同都是在第一个方法中构建自定义消息,因此更改您的第一个方法以通过纯文本错误消息方法传递自定义异常:
免责声明:不完全确定这是否有效。这取决于您如何构建错误消息。
编辑:
或者您可以添加第三个重载:
然后您的方法可以构建异常消息并传递该字符串和
sendEmail
布尔值到第三个重载。Seeing as all you're doing differently is building up a custom message in the first method, change your first method to pass the custom exception through the plain text error message method instead:
Disclaimer: Not entirely sure if this will work. It depends how you build up the error message.
EDIT:
Or you could add a third overload:
And then your methods could just both build up the exception message and pass that string and
sendEmail
boolean to the third overload.您能否仅使用较少的参数来调用具有较多参数的重载,并传入
null
或“”作为自定义消息?请注意,如果您使用的是 C# 4,则可以使用可选参数来执行此操作,而无需重载。
Can you just make the overload with fewer parameters call the one with more, passing in
null
or "" as the custom message?Note that if you're using C# 4, you could do this without overloading, using an optional parameter.