减少重复的错误处理代码在C#中? [英] Reducing duplicate error handling code in C#?

查看:172
本文介绍了减少重复的错误处理代码在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有用这样的异常处理工作完全满意,有很多例外和try / catch带来的表(堆栈展开等),但它似乎打破了很多的面向对象模型过程

I've never been completely happy with the way exception handling works, there's a lot exceptions and try/catch brings to the table (stack unwinding, etc.), but it seems to break a lot of the OO model in the process.

无论如何,这里有一个问题:

Anyway, here's the problem:

假设你有一些类包装或包括网络文件IO操作(如阅读,并在某些特定的UNC路径某处写一些文件)。由于各种原因,你不希望这些IO操作失败,所以如果你发现他们失败的再次尝试他们,你不断重试,直到他们成功或到达超时。我已经有一个方便的RetryTimer类,我可以实例化并使用睡眠试之间的当前线程,并确定超时期限已过,等等。

Let's say you have some class which wraps or includes networked file IO operations (e.g. reading and writing to some file at some particular UNC path somewhere). For various reasons you don't want those IO operations to fail, so if you detect that they fail you retry them and you keep retrying them until they succeed or you reach a timeout. I already have a convenient RetryTimer class which I can instantiate and use to sleep the current thread between retries and determine when the timeout period has elapsed, etc.

的问题是,你有一堆的IO操作这个类的几种方法,以及你需要用他们每个人在的try-catch /重试逻辑

The problem is that you have a bunch of IO operations in several methods of this class, and you need to wrap each of them in try-catch / retry logic.

下面是一个示例代码片段:

Here's an example code snippet:

RetryTimer fileIORetryTimer = new RetryTimer(TimeSpan.FromHours(10));
bool success = false;
while (!success)
{
try
{
// do some file IO which may succeed or fail
success = true;
}
catch (IOException e)
{
if (fileIORetryTimer.HasExceededRetryTimeout)
{
throw e;
}
fileIORetryTimer.SleepUntilNextRetry();
}
}

那么,你如何避免重复极本在整个班级的每个文件IO操作的代码?我的解决方案是使用匿名委托块和在类的单一方法,执行传递给它的委托块。这让我做这样的事情在其他的方法:

So, how do you avoid duplicating most of this code for every file IO operation throughout the class? My solution was to use anonymous delegate blocks and a single method in the class which executed the delegate block passed to it. This allowed me to do things like this in other methods:

this.RetryFileIO( delegate()
{
// some code block
} );

我喜欢这个有点,但它留下来了很多有待改进。我想听听人们将如何解决等这类问题。

I like this somewhat, but it leaves a lot to be desired. I'd like to hear how other people would solve this sort of problem.

推荐答案

这看起来像一个极好的机会来看看面向方面编程。这里是在.NET AOP的好文章。总的想法是,你会提取跨职能的关注(即重试对于x小时)到一个单独的类,然后你注释需要修改以这种方式他们的行为的任何方法。下面是它看起来(上的Int32一个不错的扩展方法)

This looks like an excellent opportunity to have a look at Aspect Oriented Programming. Here is a good article on AOP in .NET. The general idea is that you'd extract the cross-functional concern (i.e. Retry for x hours) into a separate class and then you'd annotate any methods that need to modify their behaviour in that way. Here's how it might look (with a nice extension method on Int32)

[RetryFor( 10.Hours() )]
public void DeleteArchive()
{
  //.. code to just delete the archive
}

这篇关于减少重复的错误处理代码在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆