注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

飞天心宏的博客

依稀旧梦似曾见,相逢只恨缘太迟

 
 
 

日志

 
 
关于我

出身数学,爱好文学,从事软件开发工作。一个阳光、幽默、热爱生活的男孩子,在追求理想的路上风雨无阻,勇往直前,崇尚“梦想有多远,我们就走多远”!

网易考拉推荐

统一返回类型TwiReturn  

2011-02-13 17:17:37|  分类: C# 扩展 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

    /*

     * CopyRight:Twilight (C)2010

     * Author:xuzhihong

     * Description:统一返回类型

     * 改进了DataTableIListList等单独序列化的问题

     * 整合全服务器端权限验证

     * 整合了Session过期重新登录问题

     */

    public class TwiReturn

    {

        /// <summary>

        /// 返回id

        /// </summary>

        public object id

        {

            get;

            set;

        }

        private bool _success = false; //默认false

        /// <summary>

        /// 操作是否顺利完成

        /// </summary>

        public bool success

        {

            get

            {

                return _success;

            }

            set

            {

                _success = value;

            }

        }

        private bool _hasRight = false; //默认false

        /// <summary>

        /// 是否有权限进行相应操作

        /// </summary>

        public bool hasRight

        {

            get

            {

                return _hasRight;

            }

            set

            {

                _hasRight = value;

            }

        }

 

        /// <summary>

        /// 操作权限名称

        /// </summary>

        public string rightName

        {

            get;

            set;

        }

 

        /// <summary>

        /// 错误

        /// </summary>

        public string errors

        {

            get;

            set;

        }

 

        /// <summary>

        /// 消息

        /// </summary>

        public string message

        { 

            get

            {

                string msg = string.Empty;

                if (!this.success)

                {

                    if (this.hasRight)

                    {

                        msg = string.Format("系统发生异常:{0}", this.errors);

                    }

                    else

                    {

                        msg = string.Format("您没有操作权限,权限名称为:{0}", this.rightName);

                    }

                }

                else

                {

                    msg = string.Format("操作成功!");

                }

                return msg;

            }

        }

 

        /// <summary>

        /// 返回数据(序列化数据)

        /// </summary>

        public object data

        {

            get;

            set;

        }

 

        private int _totalCount = 0;

        /// <summary>

        /// 总记录数,默认取tSoucre.Count()

        /// </summary>

        public int totalCount

        {

            get

            {

                if (_totalCount == 0)

                {

                    var source = data as IEnumerable<object>;

                    if (source !=null)

                    {

                        _totalCount = source.Count();

                    }                   

                }

                return _totalCount;

            }

            set

            {

                _totalCount = value;

            }

        }

    } 

 

 

 

使用示例:

 

public class _TecSch_Cer_Item

    {

        /// <summary>

        /// 获取计量依据

        /// </summary>

        /// <param name="paging"></param>

        /// <param name="condition"></param>

        /// <returns></returns>

        public TwiReturn GetTechnicalSchedule(PagingCondition paging, ConditionModel condition)

        {

            TwiReturn twi = new TwiReturn();

            try

            {

                twi.hasRight = UserContext.HasRight("pass");

                if (twi.hasRight)

                {

                    twi = UsingDAL.tecSch_Cer_Item.GetTechnicalSchedule(paging, condition);

                    twi.success = true;

                }

               

            }

            catch (System.Exception ex)

            {

                twi.errors = ex.Message;

            }

            return twi;

        }

 

        /// <summary>

        /// 根据计量依据ID获取计量标准_可检项目_计量依据

        /// </summary>

        /// <param name="techSchID"></param>

        /// <returns></returns>

        public TwiReturn GetByTechSchID(string techSchID, PagingCondition paging, ConditionModel condition)

        {

            TwiReturn twi = new TwiReturn();

            try

            {

                twi.hasRight = UserContext.HasRight("pass");

                if (twi.hasRight)

                {

                    twi = UsingDAL.tecSch_Cer_Item.GetByTechSchID(techSchID, paging, condition);

                    twi.success = true;

                }

 

            }

            catch (System.Exception ex)

            {

                twi.errors = ex.Message;

            }

            return twi;

        }

 

        /// <summary>

        /// 删除TTecSch_Cer_Item表数据

        /// </summary>

        /// <param name="ids"></param>

        /// <returns></returns>

        public TwiReturn Delete(string[] ids)

        {

            TwiReturn twi = new TwiReturn();

            try

            {

                twi.hasRight = UserContext.HasRight("pass");

                if (twi.hasRight)

                {

                    DAL.Common.Delete("TTecSch_Cer_Item", ids);

                    twi.success = true;

                }

 

            }

            catch (System.Exception ex)

            {

                twi.errors = ex.Message;

            }

            return twi;

        }

 

        /// <summary>

        /// 新增

        /// </summary>

        /// <param name="techSchID"></param>

        /// <param name="certiID"></param>

        /// <param name="vcItemIDs"></param>

        public TwiReturn New(string techSchID, string certiID, string[] vcItemIDs)

        {

            TwiReturn twi = new TwiReturn();

            try

            {

                twi.hasRight = UserContext.HasRight("pass");               

                if (twi.hasRight)

                {

                    UsingDAL.tecSch_Cer_Item.New(techSchID, certiID, vcItemIDs);

                    twi.success = true;                   

                }

 

            }

            catch (System.Exception ex)

            {

                twi.errors = ex.Message;

            }

            return twi;

        }

 

 }

 

 

为什么这么做?使用此种统一返回类型处理,BLL代码格式看起来非常统一,写起来非常顺手,即使是新手也能迅速负责写该层的代码,同时最大的好处在于JS异步请求(Ajax方式响应)得到的序列化格式(JSON形式)完全一样,非常好处理。

 

                                         飞天心宏  2011-02-13

 

  评论这张
 
阅读(1398)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017