Reducing duplicate error handling code in C#?

后端 未结 4 1314
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 19:08

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

4条回答
  •  长发绾君心
    2020-12-29 19:14

    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
    }
    

提交回复
热议问题