一个方法调用返回值的包含类

来源:互联网 发布:淘宝直播账号怎么申请 编辑:程序博客网 时间:2024/05/19 04:04

在我们开发的系统中,经常有这样一种情况:

  用户点击“保存”按钮之后,要将数据保存到数据库中,如果保存成功,则弹出提示框或不弹出提示框而直接更新数据;否则,如果失败,则弹出错误提示框。提示用户“保存失败,... (后面是相关错误信息) ”。为了使这些方法的调用过程更清晰,我自己定义了一个方法返回的包装类: ResultEx ,类图如下:

  

在这个类中,有几个ResultEx的构造函数,可以根据不同的情况,返回不同构造的ResultEx对象。ResultEx的全部代码如下:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace eHead.Common
{
    
public enum ResultType : int
    {
        
/// <summary>
        
/// 成功
        
/// </summary>
        Success = 1,
        
/// <summary>
        
/// 失败
        
/// </summary>
        Fail = 0
    }
    [Serializable]
    
public class ResultEx
    {
        
private ResultType _ResultType;
        
/// <summary>
        
/// 
        
/// </summary>
        public ResultType ResultType
        {
            
get { return _ResultType; }
            
set { _ResultType = value; }
        }

        
private Exception _Ex = new Exception();
        
public Exception Ex
        {
            
get { return _Ex; }
            
set { _Ex = value; }
        }

        
private string _Message;
        
public string Message
        {
            
get { return _Message; }
            
set { _Message = value; }
        }
        
private object _Tag;
        
public object Tag
        {
            
get { return _Tag; }
            
set { _Tag = value; }
        }

        
public ResultEx(ResultType ResultType, string message, Exception ex)
        {
            _ResultType 
= ResultType;
            
this._Message = message;
            
this._Ex = ex;
        }

        
public ResultEx(ResultType ResultType, string message)
        {
            _ResultType 
= ResultType;
            
this._Message = message;
        }

        
public ResultEx(ResultType ResultType, Exception ex)
        {
            _ResultType 
= ResultType;
            
this._Ex = ex;
            
this._Message = _Ex.Message;
        }

        
public ResultEx(ResultType ResultType)
        {
            
this._ResultType = ResultType;
        }

        
public ResultEx(ResultType ResultType, object tag)
        {
            
this._ResultType = ResultType;
            
this._Tag = tag;
        }

    }
}

 这个类的结构很简单,那么,如何使用它呢? 现在,我想插入新加一个部门信息到数据库中,后台插入的方法这样写:

        public ResultEx InsertDept(DeptObj dept)
        {
            
try
            {
                
//Insert Dept code here....

                
return new ResultEx(ResultType.Success);
            }
            
catch (Exception ex)
            {
                
return new ResultEx(ResultType.Fail, ex); //既可以在这里直接把错误发送出去,也可以在这里直接返回经过包装后的消息,如:
                //return new ResultEx(ResultType.Fail, "插入到数据库失败," + ex.Message);
            }
        }

客户端的插入方法这样写:

        private ResultEx InsertDept()
        {
            _CurrentDept 
= new DeptObj();
            
//.. 给DeptObj的各个属性赋值
            ResultEx result = Service.InsertDept(_CurrentDept);
            
if (result.ResultType == ResultType.Fail)
            {
                
return new ResultEx(ResultType.Fail, "增加部门失败," + result.Message);
            }
            
return result;
        }

这样,客户端每调用器端的方法时,都可以使用ResultEx这个类来进行方法返回的包装。只需要声明一个新的ResultEx对象来接收方法的返回值。
   方法如果执行成功,则ResultEx的ResultType == ResultType.Success ,你可以在这里弹出一个对话框,提示方法保存成功,也可以直接更新页面数据。反之,方法如果执行失败,则可以从ResultEx对象中去得到失败的信息,并且可以对这个失败信息进行一个包装。