什么是MVP?在“MVP初探”里就有讲过了,就是一种UI的架构模式。

简单的描述一下Unity和Exception Handling Application Block:

Unity是一个轻量级的可扩展的依赖注入(DI)容器,支持构造函数,属性和方法调用注入。构建一个成功应用程序的关键是实现非常松散的耦合设计。松散耦合的应用程序更灵活,更易于维护 。

微软Enterprise Library EHAB(Exception Handling Application Block)提供了一种基于策略(Policy)的异常处理方式,在不同的环境中,比如多层架构中不同的层次中,我们可以定义不同的异常处理策略。

为使M/V/P之间更好的解耦,我们通过引入Enterprise Library的Exception Handling Application Block来实现异常处理,借助Policy Injection Application Block来实现AOP,即然从2.0开始Unity就有几个内置的Handler(Authorization/Exception Handling/Logging/Performance Counter/Validation),自然也就引入了Unity。

这样,我们就可以做到以AOP的方式处理异常(不仅仅只有异常,还可以是其它的业务无关性处理)。我们通过Unity的使得P对M的依赖得以解除,同时也大大增强了可扩展性和可配置性。将系统的耦合度也除到最低。

实例演练:

这里还是延用《MVP之V和P交互》的例子。

首先,我们来看看Unity如何配置的

  • 修改配置文件添加配置节
<configSections>   ...   <section name="unity"            type=" Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </configSections>
  • 添加Unity节元素
 <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">   </unity>

这个元素节的名称也就添加配置节时的名称,两者要相同;

  • 添加程序集和命名空间
<assembly name="Handwe.Demo.UnityInMVP" />
<namespace name="Handwe.Demo.UnityInMVP" />

使得可以在相应的程序集和命名空间里查到相对应的类;在这里也可以添加别名;

  • 添加容器的扩展节
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    

用于在Unity容器中扩展拦截;

  • 添加默认(未命名)容器container
<container>

</container>

接着,在这个container里注册类型或接口,这里我们注册的是接口,并用于拦载;

  • 注册接口/类型
<register type="ICalculatorView" mapTo="CalculatorView"/>
<register type="ICalculate" mapTo="Calculate">
<interceptor type="TransparentProxyInterceptor"/>
<interceptionBehavior type="PolicyInjectionBehavior"/>
</register>

注册了两个接口,一个是ICalculatorView并映射到CalculatorView;另外一个是ICalculate映射到Calculate;这里我们只对ICalculate使用了拦截,有三种拦截器,适用于接口或类型,它是接口类型,所以选择透明代理拦截器类型TransparentProxyInterceptor,并使用提供的策略拦载行为PolicyInjectionBehavior.

  • 添加拦截扩展元素
      <extension type="Interception" />
<interception>
<policy name="policy-exceptionHandler">
<matchingRule name="auther-rule2" type="MemberNameMatchingRule">
<constructor>
<param name="namesToMatch">
<array type="string[]">
<value value="Divide" />
<value value="Add" />
</array>
</param>
</constructor>
</matchingRule>
<callHandler name="exceptionHandler-handler1" type="Handwe.Demo.UnityInMVP.ExceptionCallHandler, Handwe.Demo.UnityInMVP">
<constructor>
<param name="exceptionPolicyName" value="UIExceptionPolicy"/>
<param name="order" value="1"/>
</constructor>
</callHandler>
</policy>
</interception>

这里的拦截策略policy-cexeptionHandler我们配置为:使用成员名称配置规则MemberNameMatchingRule,提供的构造函数接受一个名称为nameToMatch,的字符串组string[]的形参,这里提供了两个成员,分别是Divide、Add;而处理程序则是我们通过自定义的,因为我们要把Unity和EHAB集成;这里我们先做下介绍,在ExceptionCallHandler中提供了支持接受两个形参的构造函数,分别是异常处理策略名称exceptionPolicyName和处理程序在管道中的执行顺序order;那么这里的异常处理策略"UIExceptionPolicy"是从哪里来的呢?其实这个就是我们在EHAB中配置的异常处理策略的名称。

有了上面的认识,那么我们现在回过头了看看是如果配置“UIExceptionPloicy”。

 配置EHAB

因为Entlib5不支持使用配置工具来对Unity配置,须要手工配置;EHAB可以用配置工具来配置;我以借助配置工具我们将EHAB的异常处理策略配置为如下:

  • 添加支持的节
<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" />
  • 具体的配置
  <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>

那么,现在很清楚的可以看到我们通过在Unity中配置的拦截处理通过“UIExceptionPolicy”这个名称将它们关联起来。接下来再来看看代码是如何实现的。具体的可以看《Exception Handling引入MVP》这里;

代码的实现

主要的一些代码在《MVP之V和P交互》的例子中已经实现了,在这里我们来看看新增的两个类:分别是MessageBoxHandler、ExceptionCallHandler;

MessageBoxHandler代码:

  • 引用的程序集

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;

 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;
}
}

这个仅仅是在处理异常里弹出一个消息框;

[ConfigurationElementType(typeof(CustomHandlerData))]

注意这行代码的使用,它使得可以在配置文件中配置该处理程序;

public MessageBoxHandler(NameValueCollection igonre)
{ }

通过NameValueConllection可以取得在配置文件的的配置;

ExceptionCallHandler的实现代码:

 namespace Handwe.Demo.UnityInMVP
{
public class ExceptionCallHandler : ICallHandler
{
// Fields
private ExceptionPolicyImpl exceptionPolicy;
private int order; public ExceptionCallHandler(string exceptionPolicyName, int order)
{ this.exceptionPolicy = EnterpriseLibraryContainer.Current.GetInstance<ExceptionPolicyImpl>(exceptionPolicyName);
this.order = order;
} // Methods
public ExceptionCallHandler(ExceptionPolicyImpl exceptionPolicy)
{
this.exceptionPolicy = exceptionPolicy;
} public ExceptionCallHandler(ExceptionPolicyImpl exceptionPolicy, int order)
: this(exceptionPolicy)
{
this.order = order;
} public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
if (getNext == null)
{
throw new ArgumentNullException("getNext");
}
IMethodReturn return2 = getNext()(input, getNext);
if (return2.Exception != null)
{
try
{
if (!this.exceptionPolicy.HandleException(return2.Exception))
{
return2.ReturnValue = null;
return2.Exception = null;
if (input.MethodBase.MemberType == MemberTypes.Method)
{
MethodInfo methodBase = (MethodInfo)input.MethodBase;
if (methodBase.ReturnType != typeof(void))
{
return2.Exception = new InvalidOperationException("CantSwallowNonVoidReturnMessage");
}
}
}
}
catch (Exception exception)
{
return2.Exception = exception;
}
}
return return2;
} // Properties
public ExceptionPolicyImpl ExceptionPolicy
{
get
{
return this.exceptionPolicy;
}
} public int Order
{
get
{
return this.order;
}
set
{
this.order = value;
}
}
} }

ExceptionCallHandler须是ICallHandler的实现,下面这个构造函数重载

public ExceptionCallHandler(string exceptionPolicyName, int order)
{ this.exceptionPolicy = EnterpriseLibraryContainer.Current.GetInstance<ExceptionPolicyImpl>(exceptionPolicyName);
this.order = order;
}

就是使用到的构造函数数,它有两个形参exceptionPolicyName和order。通过:

this.exceptionPolicy = EnterpriseLibraryContainer.Current.GetInstance<ExceptionPolicyImpl>(exceptionPolicyName);

根据异常处理策略的名称获取所相关异常处理配置;

在调用方法或目标方法后确定是否引发了异常,并通过HandleException检查是否存在异常对象的类型由 System.Exception 参数指定相匹配的策略项,如果有,则调用与该条目关联的处理程序。

if (!this.exceptionPolicy.HandleException(return2.Exception))
{
return2.ReturnValue = null;
return2.Exception = null;
if (input.MethodBase.MemberType == MemberTypes.Method)
{
MethodInfo methodBase = (MethodInfo)input.MethodBase;
if (methodBase.ReturnType != typeof(void))
{
return2.Exception = new InvalidOperationException("CantSwallowNonVoidReturnMessage");
}
}
}

这里是通过被调用的方法是否有无返回值,如果有将方法异常重新引发一个一新的无效操作异常;

小结:

所以通过这样的设计采用Unity作为IoC框架,并且通过Unity的Interception Extension实现AOP,不仅仅是本文提到的Exception Handler。

我们通过Unity的使得P对M的依赖得以解除,同时也大大增强了可扩展性和可配置性。将系统的耦合度也除到最低。

Unity、Exception Handling引入MVP的更多相关文章

  1. Exception Handling引入MVP

    异常处理(Exception Handling)是所有系统的最基本的基础操作之一,其它的比如日志(Logging).审核(Auditing).缓存(Caching).事务处理(Transaction) ...

  2. Exception Handling Considered Harmful

    异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...

  3. Exception Handling in ASP.NET Web API webapi异常处理

    原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...

  4. CoreCLR on Mac:体验managed exception handling

    C#测试代码: using System; class Program { static void A() { try { Console.WriteLine("Throwing an ex ...

  5. Exception Handling Statements (C# Reference)

    Exception Handling Statements (C# Reference) C# provides built-in support for handling anomalous sit ...

  6. Exception Handling in ASP.NET Web API

    public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...

  7. [转]java-Three Rules for Effective Exception Handling

    主要讲java中处理异常的三个原则: 原文链接:https://today.java.net/pub/a/today/2003/12/04/exceptions.html Exceptions in ...

  8. How a C++ compiler implements exception handling

    Introduction One of the revolutionary features of C++ over traditional languages is its support for ...

  9. 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...

随机推荐

  1. 图片效果集合(js、jquery或html5)

    1.jQuery HTML5 幻灯片使用支持HTML5的浏览器会有特殊效果,即切换图片时颜色的改变文章:http://keleyi.com/a/bjac/b8i3xdui.htm效果:http://k ...

  2. javascript中DOM部分基础知识总结

    1.DOM介绍      1.1 DOM概念      文档对象模型(Document Object Model),它定义了访问和处理HTML文档的标准方法.现在我们主要接触到的是HTML DOM. ...

  3. VMware VirtualBox共存时桥接注意

    今天在VMware必须桥接的一个虚拟机上需要连接其他机器时,遇到总是连接不到的情况. 具体现象: HOST机器可以ping A机器 VMWare Guest机器无法ping A机器,也无法ping H ...

  4. QuickFlow UI 控件之 NamedFormAttachment

    在开发自定义表单时,如果碰到需要按类别上传附件的情况,(比如流程附件分为草稿版,正式版,发布版),比较难处理,需要开发自定义字段控件来实现. QF最新版本(Build131124)提供了NamedFo ...

  5. iOS __block 与 __weak

    关于__block 与__weak http://stackoverflow.com/questions/19227982/using-block-and-weak 一下是一些区别的介绍 MRC: _ ...

  6. Git 简易手册

    前言 这里就不介绍 git 了,表一看到命令行就怕怕,常用的命令也就这些,基本满足 99% 以上的项目需求,非常实用. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnbl ...

  7. 【原】iOS动态性(五)一种可复用且解耦的用户统计实现(运行时Runtime)

    声明:本文是本人 编程小翁 原创,转载请注明. 为了达到更好的阅读效果,强烈建议跳转到这里查看文章. iOS动态性是我的关于iOS运行时的系列文章,由浅入深,从理论到实践.本文是第5篇.有兴趣可以看看 ...

  8. iOS 9.3真机适配-Could not find Developer Disk Image问题

    Could not find Developer Disk Image 这是由于真机系统过高或者过低,Xcode中没有匹配的配置包文件,我们可以通过这个路径进入配置包的存放目录: /Applicati ...

  9. (三)Maven仓库介绍与本地仓库配置

    1.Maven本地仓库/远程仓库的基本介绍 示意图: 本地仓库是指存在于我们本机的仓库,在我们加入依赖时候,首先会跑到我们的本地仓库去找,如果找不到则会跑到远程仓库中去找.对于依赖的包大家可以从这个地 ...

  10. ORACLE数据库SQL语句的执行过程

    SQL语句在数据库中处理过程是怎样的呢?执行顺序呢?在回答这个问题前,我们先来回顾一下:在ORACLE数据库系统架构下,SQL语句由用户进程产生,然后传到相对应的服务端进程,之后由服务器进程执行该SQ ...