zoukankan      html  css  js  c++  java
  • 代码重构问题

    下面是我所开发的一个UI里面的C#代码,主要就是几个按钮点击事件,按照我们公司的要求在事件里面必须加try-catch-finally做异常处理,不过我感觉每个事件里面都要加相同的try-catch-finally代码,感觉没有达到代码重用,但又不是很清楚改如何重用这部分代码,故特此咨询博客园的大牛们,望大牛不吝赐教!不胜感激!
    代码
    private void AddButton_Click()
    {
      try
      {
        this.Cursor=Cursors.Wait;
        this._present.Add();//调用另一个Class里面的Add方法
      }
      catch(Exception e)
      {
        throw e;
      }
      finally
      {
        this.Cursor=Cursore
      }
    }

    private void DeleteButton_Click()
    {
      try
      {
        this.Cursor=Cursors.Wait;
        this._present.Delete();//调用另一个Class里面的Delete方法
      }
      catch(Exception e)
      {
        throw e;
      }
      finally
      {
        this.Cursor=Cursore
      }
    }

    private void SaveButton_Click()
    {
      try
      {
        this.Cursor=Cursors.Wait;
        this._present.Save();();//调用另一个Class里面的Save方法
      }
      catch(Exception e)
      {
        throw e;
      }
      finally
      {
        this.Cursor=Cursore
      }
    }

    http://www.cnblogs.com/artech/archive/2010/03/26/1697298.html

    private void PresentCall(Fun<T, void> func) //把T换成你那个present的类型

    窃以为这种重构没必要,如果真不想多写code的话,就试试AOP吧,比如PostSharp,针对每个方法,只需加一个attrbute即可:

    可以看看PPT里的demo
    http://www.cnblogs.com/blodfox777/archive/2010/01/14/AOP-With-PostSharp.html


    02{
    03  try
    04  {
    05    this.Cursor=Cursors.Wait;
    06    func;
    07  }
    08  catch(Exception e)
    09  {
    10    throw e;
    11  }
    12  finally
    13  {
    14    this.Cursor=Cursore
    15  }
    16}


    Aop

    果多的话,理想是这样. 不过这个AOP...
    view source

    print
    ?1[TryCatch(OnError=....,Finally=...)]
    2private void AddButton_Click()
    3{
    4}

  • 相关阅读:
    JS完整获取IE浏览器信息
    C# DataSet和DataTable详解
    linux下mysql导入数据
    Linux上安装mysql
    JDK6+tomcat7+mysql官网下载地址
    Linux常用命令
    mysql 授权
    tomcat7.0学习笔记
    struts2 <s:property/>标签的使用输出时间格式转换
    Linux如何查找软件安装路径?
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1788710.html
Copyright © 2011-2022 走看看