Exception Handling引入MVP
异常处理(Exception Handling)是所有系统的最基本的基础操作之一,其它的比如日志(Logging)、审核(Auditing)、缓存(Caching)、事务处理(Transaction)等;
今天,来把异常处理引入到我们在《MVP之V和P的交互》中Calculator的实例中,简单的实现AOP。实例运行如图:

那么,开始我们开简单的介绍下Enterprise Library EHAB(Exception Handling Application Block)提供了一种基于策略(Policy)的异常处理方式。具体的可以参考这里。
如何配置
具体的配置如下:
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="UIExceptionPolicy">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None">
<exceptionHandlers>
<add type="Handwe.Demo.UnityInMVP.MessageBoxHandler, Handwe.Demo.UnityInMVP"
name="Custome Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
这些是可以通过配置工具来配置的;现在我们来说说具体的内容:
<exceptionPolicies>
<add name="UIExceptionPolicy">
添加一个名为UIExceptionPolicy的异常策略;
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None">
配置要处理的异常类型,这里是所有异常;postHandlingAction="None"是无后续处理;
<exceptionHandlers>
<add type="Handwe.Demo.UnityInMVP.MessageBoxHandler, Handwe.Demo.UnityInMVP"
name="Custome Handler" />
exceptionHandlers添加的是一个我们自定义的处理程序,名为MessageBoxHandler,就是简单的一个弹出式消息框;
代码的实现
- 引用程序集
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
- 异常处理程序MessageBoxHandler
namespace Handwe.Demo.UnityInMVP
{
[ConfigurationElementType(typeof(CustomHandlerData))]
public class MessageBoxHandler : IExceptionHandler
{
public MessageBoxHandler(NameValueCollection igonre)
{ }
public Exception HandleException(Exception exception, Guid handlingInstanceId)
{
MessageBox.Show(exception.Message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return exception;
}
}
}
这里很简单也没有用到相应的参数配置;
try
{
this.OnCalculating(op1, op2);
}
catch (Exception ex)
{
if (ExceptionPolicy.HandleException(ex, "UIExceptionPolicy"))
{
throw;
}
}
修改并应用以上代码;指定异常处理策略;
小结
通过Entlib的EHAB,我们可以专注于具体的业务逻辑上,类似异常之类的非业务处理可以通过后期的配置来实现。
Exception Handling引入MVP的更多相关文章
- Unity、Exception Handling引入MVP
什么是MVP?在“MVP初探”里就有讲过了,就是一种UI的架构模式. 简单的描述一下Unity和Exception Handling Application Block: Unity是一个轻量级的可扩 ...
- Exception Handling Considered Harmful
异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...
- Exception Handling in ASP.NET Web API webapi异常处理
原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...
- CoreCLR on Mac:体验managed exception handling
C#测试代码: using System; class Program { static void A() { try { Console.WriteLine("Throwing an ex ...
- Exception Handling Statements (C# Reference)
Exception Handling Statements (C# Reference) C# provides built-in support for handling anomalous sit ...
- Exception Handling in ASP.NET Web API
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...
- [转]java-Three Rules for Effective Exception Handling
主要讲java中处理异常的三个原则: 原文链接:https://today.java.net/pub/a/today/2003/12/04/exceptions.html Exceptions in ...
- How a C++ compiler implements exception handling
Introduction One of the revolutionary features of C++ over traditional languages is its support for ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...
随机推荐
- jQuery Validation Engine 表单验证
功能强大的 jQuery 表单验证插件,适用于日常的 E-mail.电话号码.网址等验证及 Ajax 验证,除自身拥有丰富的验证规则外,还可以添加自定义的验证规则. 兼容 IE 6+, Chrome, ...
- 微信WeixinJSBridge API(屏蔽右上角按钮等)
[声明]:我这份纯属于备份,为了自己将来用起来方便: [相关链接]:http://www.2cto.com/weixin/201511/451592.html(好不好用完全看这个文档的作者了) [感谢 ...
- IOS开发基础知识--碎片36
1:tabBarController跳转到另一个一级页面 当我们用tabBarController时,若已经到其中一个TabBar的子页,又要跳转到某一个一级的页面时,可以这样写 //这样就可以避免跳 ...
- React Native知识5-Touchable类组件
React Native 没有像web那样可以给元素绑定click事件,前面我们已经知道Text组件有onPress事件,为了给其他组件 也绑定点击事件,React Native提供了3个组件来做这件 ...
- Android打造属于自己的数据库操作类。
1.概述 开发Android的同学都知道sdk已经为我们提供了一个SQLiteOpenHelper类来创建和管理SQLite数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...
- 初识JavaScript 变量, 操作符, 数组
这里讲的不会太多, 因为所有的语言都是一样的, 一些基本的东西, 所以就随便写写. 变量 变量就是可变的量, 编程角度理解就是用于存储某种/某些数值的存储器. 我们可以把变量具象理解为一个盒子, 而我 ...
- C/C++浮点数在内存中的存储方式
一.内存表示 任何数据在内存中都是以二进制的形式存储的,浮点数的表示是把一个数的有效数字和数的范围在计算机的一个存储单元中分别予以表示,数的小数点位置随比例因子的不同而在一定范围内自由浮动.如下图是3 ...
- php时间
date_default_timezone_set('PRC'); //默认时区 //当前的时间增加5天 $date1 = "2014-11-11"; echo date('Y-m ...
- Linux下通配符总结
* - 通配符,代表任意字符(0到多个)? - 通配符,代表一个字符# - 注释/ - 跳转符号,将特殊字符或通配符还原成一般符号| - 分隔两个管线命令的界定; - 连续性命令的界定~ - 用户的根 ...
- 解决Android Graphical Layout 界面效果不显示
解决Android Graphical Layout 界面效果不显示 qq463431476