如何覆盖或创建新的 try/catch/finally 块 .net c#
how to overwrite or create a new try/catch/finally block .net c#
我想覆盖 try/catch/finally 块或创建一个新的 myTry/myCatch/myFinally 块以在内部具有特定和 "automatic" 行为。
例如,要记录异常,开发人员必须显式调用 Log(ex) 方法。 => try{...}catch(Ex ex){ **Log(ex)**; doSomething(); throw; }
但如果开发人员忘记调用 Log(ex),则什么也不会发生。
我想创建一个能够在其中执行某些操作的结构,例如模板模式。类似于:
MyTryWithAutomaticInternalLog {
doSomething();
}
MyCatch(Exception ex){
**//Internally this stores in someplace the exception**
doSomething2(ex);
throw;
}
MyFinally {
//internally this saves how much time took the execution for example.
doSomething3();
}
还有其他选项,例如:
1)创建一个包装器方法,如
MyTryWithAutomaticInternalLog(()=>{ doSomething(); }, (ex)=>{ doSomethingOnError(); }, ()=>{ doSomethingInfinally(); });
并在 MyTryWithAutomaticInternalLog 中使用模板模式策略管理流程,但同样,开发人员必须记住使用方法 MyTryWithAutomaticInternalLog 而不是非常标准的 try/catch/finally.
2) 避免在代码中使用 try/catch 并使用依赖注入模式管理异常。但是我想避免它。
还有其他选择吗?我怎样才能覆盖 try/catch/finally 块?如果没有,请提供一些技术信息和链接,我将不胜感激,因为我搜索的运气不佳。
Tkx
一般来说,cross cutting concerns(验证、日志记录、授权、审计等)应该从应用程序的 "normal" 部分中分离出来,而不是试图 "override try catch"。
关于 Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class 有一篇很好的 MSDN 文章。
同样,您可以在 MSDN 上使用 Interception with the same aim. There is an article about interception with Unity,但此功能也适用于其他一些流行的 DI 容器。
一些应用程序框架还具有针对横切关注点的内置扩展。例如,ASP.NET MVC 有 filters 可以全局注册,然后使用 .NET 属性为应用程序的特定部分打开或关闭。
我会用拦截器包装一个服务来实现服务调用的日志记录。无论程序员如何编写捕获代码,它都会提供一种获取异常的方法。
示例在我的服务层中,我使用 logginginterceptor 绑定分辨率。
此 url 会在充满不确定性的方向上助您一臂之力,但会让您抽象出如何处理异常。
https://msdn.microsoft.com/en-us/library/dn178466(v=pandp.30).aspx
此外,您可以通过验证对象从另一层代码抛出异常。如果你 (Throw New CustomException("Error 121") 你的 try catch 代码将知道该做什么。如果你使用拦截,你可以处理所有类型的自定义异常(派生自 System.Exception)。
我想覆盖 try/catch/finally 块或创建一个新的 myTry/myCatch/myFinally 块以在内部具有特定和 "automatic" 行为。
例如,要记录异常,开发人员必须显式调用 Log(ex) 方法。 => try{...}catch(Ex ex){ **Log(ex)**; doSomething(); throw; }
但如果开发人员忘记调用 Log(ex),则什么也不会发生。
我想创建一个能够在其中执行某些操作的结构,例如模板模式。类似于:
MyTryWithAutomaticInternalLog {
doSomething();
}
MyCatch(Exception ex){
**//Internally this stores in someplace the exception**
doSomething2(ex);
throw;
}
MyFinally {
//internally this saves how much time took the execution for example.
doSomething3();
}
还有其他选项,例如:
1)创建一个包装器方法,如
MyTryWithAutomaticInternalLog(()=>{ doSomething(); }, (ex)=>{ doSomethingOnError(); }, ()=>{ doSomethingInfinally(); });
并在 MyTryWithAutomaticInternalLog 中使用模板模式策略管理流程,但同样,开发人员必须记住使用方法 MyTryWithAutomaticInternalLog 而不是非常标准的 try/catch/finally.
2) 避免在代码中使用 try/catch 并使用依赖注入模式管理异常。但是我想避免它。
还有其他选择吗?我怎样才能覆盖 try/catch/finally 块?如果没有,请提供一些技术信息和链接,我将不胜感激,因为我搜索的运气不佳。
Tkx
一般来说,cross cutting concerns(验证、日志记录、授权、审计等)应该从应用程序的 "normal" 部分中分离出来,而不是试图 "override try catch"。
关于 Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class 有一篇很好的 MSDN 文章。
同样,您可以在 MSDN 上使用 Interception with the same aim. There is an article about interception with Unity,但此功能也适用于其他一些流行的 DI 容器。
一些应用程序框架还具有针对横切关注点的内置扩展。例如,ASP.NET MVC 有 filters 可以全局注册,然后使用 .NET 属性为应用程序的特定部分打开或关闭。
我会用拦截器包装一个服务来实现服务调用的日志记录。无论程序员如何编写捕获代码,它都会提供一种获取异常的方法。
示例在我的服务层中,我使用 logginginterceptor 绑定分辨率。
此 url 会在充满不确定性的方向上助您一臂之力,但会让您抽象出如何处理异常。
https://msdn.microsoft.com/en-us/library/dn178466(v=pandp.30).aspx
此外,您可以通过验证对象从另一层代码抛出异常。如果你 (Throw New CustomException("Error 121") 你的 try catch 代码将知道该做什么。如果你使用拦截,你可以处理所有类型的自定义异常(派生自 System.Exception)。