重构异常处理(Refactoring exception handling)

编程入门 行业动态 更新时间:2024-10-27 10:26:42
重构异常处理(Refactoring exception handling)

在我的一个类中,我调用了一个存储库,该存储库上有一些错误处理。 我想重构错误处理代码,因为它是非常重复的,唯一真正改变的是消息。

我的代码目前看起来像这样:

public IList<User> GetUser() { try { return _repository.GetUsers(); } catch (WebException ex) { ErrorMessages.Add("..."); _logger.ErrorException("...", ex); } catch (SoapException ex) { ErrorMessages.Add("..."); _logger.ErrorException("...", ex); } ... etc }

我可以通过调用另一个获取错误消息值和记录器消息值的方法来替换catch块中的那些行。 但是我想我也可以使用Action <>参数执行此操作,但我对使用Func <>和Action <>非常缺乏经验,并且我没有真正看到使用其中一个方法对我有什么好处。

我的问题是什么是重构此代码的最佳方法,为什么一种方式比另一种方式更有利(根据我上面的例子)。

谢谢你的帮助。

In one of my classes I have a call to a repository which has some error handling on it. I would like to refactor the error handling code because it is quite repetitive and the only thing that really changes is the message.

My code currently looks something like this:

public IList<User> GetUser() { try { return _repository.GetUsers(); } catch (WebException ex) { ErrorMessages.Add("..."); _logger.ErrorException("...", ex); } catch (SoapException ex) { ErrorMessages.Add("..."); _logger.ErrorException("...", ex); } ... etc }

I could replace those lines in my catch block with a call to another method which takes an error message value and a logger message value. However I suppose I could also do this using an Action<> parameter but I am very inexperienced at using Func<> and Action<> and don't really see what benefit I would have using one of those over a method.

My question is really what is the best way to refactor this code and why does one way benefit over the other (as per my example above).

Thanks for any help.

最满意答案

假设异常类型总是相同但消息不同,您可以这样做:

static public T Try<T>(string webMessage, string soapMessage, Func<T> func) { try { return func(); } catch (WebException ex) { ErrorMessages.Add(webMessage); _logger.ErrorException(webMessage, ex); } catch (SoapException ex) { ErrorMessages.Add(soapMessage); _logger.ErrorException(soapMessage, ex); } }

此Try-method将使用类型为Func<T>的委托来调用函数并返回其值。 该函数将位于同一个try-catch块中。 消息通过参数提供。 现在,在代码中的其他位置,您可以将其称为:

var users = Try("My web message.", "My soap message.", () => _repository.GetUsers());

或者,在您的情况下甚至更短(不使用参数时):

var users = Try("My web message.", "My soap message.", _repository.GetUsers);

当然,您可以根据自己的喜好修改和排列参数。

如果你是使用和不使用返回类型的混合方法,最好不使用Func而是使用Action 。 这将符合所有情况:

static public void Try(string webMessage, string soapMessage, Action action) { try { action(); } catch (WebException ex) { ErrorMessages.Add(webMessage); _logger.ErrorException(webMessage, ex); } catch (SoapException ex) { ErrorMessages.Add(soapMessage); _logger.ErrorException(soapMessage, ex); } }

但是这个解决方案使得代码更难以阅读/维护:

IList<User> users; Try("My web message.", "My soap message.", () => users = _repository.GetUsers());

Assuming the exception types are always the same but the messages are different, you can do this:

static public T Try<T>(string webMessage, string soapMessage, Func<T> func) { try { return func(); } catch (WebException ex) { ErrorMessages.Add(webMessage); _logger.ErrorException(webMessage, ex); } catch (SoapException ex) { ErrorMessages.Add(soapMessage); _logger.ErrorException(soapMessage, ex); } }

This Try-method will use a delegate of type Func<T> to call a function and return its value. The function will be inside the same try-catch block. The messages are provides via parameters. Now, somewhere else in your code, you could call this like:

var users = Try("My web message.", "My soap message.", () => _repository.GetUsers());

Or, in your case even shorter (when not using parameters):

var users = Try("My web message.", "My soap message.", _repository.GetUsers);

Of course you can modify and arrange the parameters of Try to your own liking.

In case you are mixing method with and without return types, it is better not to use the Func but the Action. This will be able to comply to all situations:

static public void Try(string webMessage, string soapMessage, Action action) { try { action(); } catch (WebException ex) { ErrorMessages.Add(webMessage); _logger.ErrorException(webMessage, ex); } catch (SoapException ex) { ErrorMessages.Add(soapMessage); _logger.ErrorException(soapMessage, ex); } }

But this solution makes the code a tiny bit more difficult to read / maintain:

IList<User> users; Try("My web message.", "My soap message.", () => users = _repository.GetUsers());

更多推荐

本文发布于:2023-04-29 12:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336431.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重构   异常   Refactoring   exception   handling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!