重载最佳实践

发布于 2024-09-18 06:35:21 字数 781 浏览 4 评论 0原文

我想使用两个静态方法来处理错误。其中一个传递异常对象,另一个仅在需要报告错误时使用,该错误将是基于文本的消息(字符串 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南汐寒笙箫 2024-09-25 06:35:24
void report(Exception e)
{
  //convert exception to plain text
  string text = e.ToString();
  //re-use the other overload
  report(text);
}

void report(string text)
{
  ...etc...
}

更复杂的版本:

delegate string GetString();

public void report(Exception e)
{
  GetString getString = delegate() { return e.ToString(); }
  report(getText);
}

public void report(string text)
{
  GetString getString = delegate() { return text; }
  report(getText);
}

void report(GetString getString)
{
  ...etc...
  string text = getString();
  ...etc...
}
void report(Exception e)
{
  //convert exception to plain text
  string text = e.ToString();
  //re-use the other overload
  report(text);
}

void report(string text)
{
  ...etc...
}

A more complicated version:

delegate string GetString();

public void report(Exception e)
{
  GetString getString = delegate() { return e.ToString(); }
  report(getText);
}

public void report(string text)
{
  GetString getString = delegate() { return text; }
  report(getText);
}

void report(GetString getString)
{
  ...etc...
  string text = getString();
  ...etc...
}
極樂鬼 2024-09-25 06:35:24

其他答案都很好,但我想我应该添加一些东西来避免我遇到的维护问题,这

public void report(string text)
{
   ...
   report(text,defaultvalue);
}

public void report(string text, string email)
{
   ...
   report(text,email,null);
}

public void report(string text, string email, List<string> errors)
{
   ...
}
...
etc.

往往是非常烦人的,当你必须重构所有这些方法只是因为你想要更改其中一种方法中的参数。

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 ...

public void report(string text)
{
   ...
   report(text,defaultvalue);
}

public void report(string text, string email)
{
   ...
   report(text,email,null);
}

public void report(string text, string email, List<string> errors)
{
   ...
}
...
etc.

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.

高速公鹿 2024-09-25 06:35:23

一个简单的解决方案是将重复的代码提取到它自己的方法中,如下所示:

public static void ReportError(Exception exceptionRaised, string reference, string customMessage, bool sendEmail)
{
    // build up message from exception, reference & custom message using string builder
    ProcessError(Message, SendEmail)
}

public static void ReportError(string errorMessage, string reference, bool sendEmail)
{
    // build up message from errorMessage & reference string builder
    ProcessError(Message, SendEmail)
}

private static void ProcessError(string message, bool sendEmail)
{
    // get filename
    // check if logfile exists, blah, blah
    // save message
    // email error (if set)
}

这当然不是最优雅的方法,但它是一个开始;-)

One simple solution would be to extract the duplicate code into it's own method, like this:

public static void ReportError(Exception exceptionRaised, string reference, string customMessage, bool sendEmail)
{
    // build up message from exception, reference & custom message using string builder
    ProcessError(Message, SendEmail)
}

public static void ReportError(string errorMessage, string reference, bool sendEmail)
{
    // build up message from errorMessage & reference string builder
    ProcessError(Message, SendEmail)
}

private static void ProcessError(string message, bool sendEmail)
{
    // get filename
    // check if logfile exists, blah, blah
    // save message
    // email error (if set)
}

This is certainly not the most elegant way to do it, but it is a start ;-)

一杯敬自由 2024-09-25 06:35:23

重载方法时,请确保第一个参数始终相同。否则有点混乱。在您的情况下,将异常作为最后一个参数。

public static void ReportError(string customMessage, string reference, bool sendEmail, Exception exceptionRaised) 

至于代码重复。让这两个方法都调用第三个受保护的方法来进行实际处理。
异常方法可以在调用第三个方法之前格式化异常并将其添加到消息参数中。

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.

public static void ReportError(string customMessage, string reference, bool sendEmail, Exception exceptionRaised) 

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.

旧城烟雨 2024-09-25 06:35:22

鉴于您所做的所有不同都是在第一个方法中构建自定义消息,因此更改您的第一个方法以通过纯文本错误消息方法传递自定义异常:

public static void ReportError(Exception exceptionRaised, string reference, 
    string customMessage, bool sendEmail)
{
    string errorMessage = BuildMessage(exceptionRaised, customMessage);
    ReportError(errorMessage, reference, sendEmail);
}

免责声明:不完全确定这是否有效。这取决于您如何构建错误消息。

编辑:

或者您可以添加第三个重载:

private static void ReportError(string completeException, bool sendEmail)
{
     // Do what needs to be done.
}

然后您的方法可以构建异常消息并传递该字符串和 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:

public static void ReportError(Exception exceptionRaised, string reference, 
    string customMessage, bool sendEmail)
{
    string errorMessage = BuildMessage(exceptionRaised, customMessage);
    ReportError(errorMessage, reference, sendEmail);
}

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:

private static void ReportError(string completeException, bool sendEmail)
{
     // Do what needs to be done.
}

And then your methods could just both build up the exception message and pass that string and sendEmail boolean to the third overload.

╰沐子 2024-09-25 06:35:22

您能否仅使用较少的参数来调用具有较多参数的重载,并传入 null 或“”作为自定义消息?

public static void ReportError(string errorMessage,
                               string reference,
                               bool sendEmail)
{
    ReportError(errorMessage, reference, null, sendEmail);
}

请注意,如果您使用的是 C# 4,则可以使用可选参数来执行此操作,而无需重载。

Can you just make the overload with fewer parameters call the one with more, passing in null or "" as the custom message?

public static void ReportError(string errorMessage,
                               string reference,
                               bool sendEmail)
{
    ReportError(errorMessage, reference, null, sendEmail);
}

Note that if you're using C# 4, you could do this without overloading, using an optional parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文