Exception Handling introduced MVP

Source: Internet
Author: User
Tags mscorlib

Exception handling (Exception handling) is one of the most basic operations of all systems, other such as log (Logging), Audit (Auditing), Cache (Caching), transaction processing (Transaction), etc.

Today, to introduce exception handling into our example of calculator in the "MVP V and P interactions", we simply implement AOP. Instance runs

Well, let's start with a brief introduction of the Enterprise Library Ehab(Exception handling application Block) Provides a policy-based approach to exception handling. Specific can be referred to here .

How to configure

The specific configuration is as follows:

<Configuration>  <configsections>    < Sectionname= "ExceptionHandling"type= "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, version=5.0.414.0, Culture=neutral, publickeytoken= 31bf3856ad364e35 "requirepermission= "true" />    </configsections>

1   <exceptionhandling>2     <exceptionpolicies>3       <Addname= "Uiexceptionpolicy">4         <Exceptiontypes>5           <Addname= "All Exceptions"type= "System.Exception, mscorlib, version=4.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089"6 posthandlingaction= "None">7             <exceptionhandlers>8               <Addtype= "Handwe.Demo.UnityInMVP.MessageBoxHandler, Handwe.Demo.UnityInMVP"9 name= "Custome Handler" />Ten             </exceptionhandlers> One           </Add> A         </Exceptiontypes> -       </Add> -     </exceptionpolicies> the   </exceptionhandling>

These can be configured by means of configuration tools; Now let's talk about specific things:

< exceptionpolicies >      <  name= "Uiexceptionpolicy">

Add an exception policy named Uiexceptionpolicy;

1 <  name= "All Exceptions"  type= "System.Exception, mscorlib, version=4.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "2            posthandlingaction=" None "  >

Configure the type of exception to be handled, here are all exceptions; posthandlingaction= "None" is no follow-up treatment;

1 < exceptionhandlers > 2               <  type= "Handwe.Demo.UnityInMVP.MessageBoxHandler, Handwe.Demo.UnityInMVP"3                 name= "Custome Handler"/>

Exceptionhandlers added is a custom handler, called Messageboxhandler, is simply a pop-up message box;

implementation of the Code

    • referencing assemblies

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

    • Exception handler Messageboxhandler
1 namespaceHandwe.Demo.UnityInMVP2 {3[ConfigurationElementType (typeof(Customhandlerdata))]4      Public classMessageboxhandler:iexceptionhandler5     {6          PublicMessageboxhandler (NameValueCollection igonre)7         {8 9         }Ten          PublicException handleexception (Exception Exception, Guid Handlinginstanceid) One         { AMessageBox.Show (Exception. Message,"Application Error", MessageBoxButtons.OK, messageboxicon.error); -             returnexception; -         } the     } -}

Here is very simple and does not use the corresponding parameter configuration;

Try             {                this. Oncalculating (OP1, OP2);            }             Catch (Exception ex)            {                if"uiexceptionpolicy"))                {                    throw;                }            }

Modify and apply the above code, and specify exception handling policy;

Summary

With EntLib's Ehab, we can focus on specific business logic, and non-business processes such as exceptions can be implemented through a later configuration.

Exception Handling introduced MVP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.