zoukankan      html  css  js  c++  java
  • spring.net之aop加单例模式编写无try catch程序

      为应付软件小白的各种无脑操作(不过话说回来,软件就是给小白使用的),常常需要在调用方法时增加众多重复的try/catch,后来拜读artech的《如何编写没有Try/Catch的程序》,了解到可以将try/catch统一处理,大大简化了程序编写。

      将类中的方法统一处理则需要在所有的方法外增加一层安全的防护,此方法产生的exception由防护层进行处理,牵扯到方法了,自然就想到了aop,使用aop可以拦截方法,spring.net的aop中恰好有AroundAdvice可以做环绕处理(对aop框架了解较少,只知道spring.net可以做到),说到这大家就都明白了,呵呵,直接上代码。

    环绕统一处理try/catch部分

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using AopAlliance.Intercept;
     6 
     7 namespace ISEN.MSH.APP.Service.Mail.Filter
     8 {
     9     public class ImapFilterAroundAdice : IMethodInterceptor
    10     {
    11 
    12         public object Invoke(IMethodInvocation invocation)
    13         {
    14             object obj = new object();
    15             try
    16             {
    17                 obj = invocation.Proceed();
    18             }
    19             catch (System.Exception ex)
    20             {
    21 
    22             }
    23             return obj;
    24         }
    25     }
    26 }

    应用接口部分

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace ISEN.MSH.APP.Service.Mail.Util
     7 {
     8     public interface IIMAPUtil
     9     {
    10         void Test();
    11     }
    12 }

    应用部分

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using ISEN.MSH.APP.Service.Mail.Filter;
     6 using LumiSoft.Net;
     7 using LumiSoft.Net.IMAP;
     8 using LumiSoft.Net.IMAP.Client;
     9 using Spring.Aop.Framework;
    10 
    11 namespace ISEN.MSH.APP.Service.Mail.Util
    12 {
    13     public class IMAPUtil : IIMAPUtil
    14     {
    15         IMAP_Client client = new IMAP_Client();
    16 
    17         private static IIMAPUtil entity;
    18         public static IIMAPUtil GetEntity()
    19         {
    20             if (entity == null)
    21             {
    22                 ProxyFactory factory = new ProxyFactory(new IMAPUtil());
    23                 factory.AddAdvice(new ImapFilterAroundAdice());
    24                 entity = (IIMAPUtil)factory.GetProxy();
    25             }
    26             return entity;
    27         }
    28 
    29         public void Test()
    30         {
    31             throw new System.Exception();
    32         }
    33 
    34     }
    35 }

    程序调用部分

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.IO;
     6 using LumiSoft.Net.POP3.Client;
     7 using LumiSoft.Net.Mime;
     8 using LumiSoft.Net.IMAP.Client;
     9 using LumiSoft.Net.IMAP;
    10 using LumiSoft.Net;
    11 
    12 namespace ISEN.MSH.APP.Service.Mail
    13 {
    14     static class Programe
    15     {
    16         [STAThread]
    17         static void Main(string[] args)
    18         {
    19             ISEN.MSH.APP.Service.Mail.Util.IMAPUtil.GetEntity().Test();
    20  
    21             System.Console.ReadLine();
    22         }
    23 }

    在使用时,我使用了单例模式,当然也可以是用spring.net配置文件来进行处理,刘东的博客中有关于spring.net的详细使用讲解。

  • 相关阅读:
    luogu P1064|| 01背包||金明的预算
    NOIp蒟蒻的爆零记——HA-0132
    模板输入计划
    1112测试教你做人
    NOIP注意事项
    强连通分量的一二三 | | JZOJ【P1232】 | | 我也不知道我写的什么
    图的割点 | | jzoj【P1230】 | | gdoi | |备用交换机
    【游戏作品】Sunset Game 制作组出品游戏一览
    【说明】我们计划从博客园迁移到知乎啦
    【总结】操作系统的重点
  • 原文地址:https://www.cnblogs.com/isenhome/p/2794397.html
Copyright © 2011-2022 走看看