C# EF CodeFirst简单工厂模式,接口多继承实例(二)

此处承接一:http://blog.csdn.net/u011127019/article/details/51384478

补全抽象接口 IBaseAbstract<T>

    /// <summary>
    /// 基础访问接口,使用泛型
    /// </summary>
    public interface IBaseAbstract<T> where T : class
    {
        #region 基础访问
        /// <summary>
        /// 获取表达式树
        /// </summary>
        /// <returns></returns>
        DbSet<T> GetDbSet();
        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="ID">主键ID</param>
        /// <returns></returns>
        T GetModel(int ID);
        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns></returns>
        bool Add(T model);
        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns></returns>
        bool Modify(T model);
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns></returns>
        bool Delete(T model);
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="ID">主键ID</param>
        /// <returns></returns>
        bool Delete(int ID);
        #endregion
    }


补全基础类操作定义BaseOperate

    /// <summary>
    /// 操作类基础封装
    /// </summary>
    internal class BaseOperate
    {
        /// <summary>
        /// 当前操作上下文
        /// </summary>
        public LiuXueContext Context { get; set; }
        /// <summary>
        /// 获取当前上下文
        /// </summary>
        /// <returns></returns>
        private LiuXueContext CreateContext()
        {
            string _Key = "SentenceContext";
            if (CallContext.GetData(_Key) == null)
            {
                CallContext.SetData(_Key, new LiuXueContext());
            }
            return CallContext.GetData(_Key) as LiuXueContext;
        }
        public BaseOperate()
        {
            this.Context = CreateContext();
        }
        /// <summary>
        /// 模型验证
        /// </summary>
        public bool IsValided<T>(T model) where T : class
        {
            DbEntityValidationResult vResult = Context.Entry<T>(model).GetValidationResult();
            if (vResult == null)
                return true;
            if (vResult.IsValid)
                return true;
            StringBuilder builder = new StringBuilder();
            foreach (DbValidationError item in vResult.ValidationErrors)
            {
                builder.Append("出错字段:" + item.PropertyName);
                builder.Append("<br />");
                builder.Append("错误描述:" + item.ErrorMessage);
                builder.Append("<br />");
            }
            throw new Exception("数据模型验证失败," + builder.ToString());
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        public int Save()
        {
            return Context.SaveChanges();
        }
        /// <summary>
        /// 标间对象为修改状态,并保存
        /// </summary>
        public bool SetModify<T>(T model) where T : class
        {
            Context.Entry<T>(model).State = EntityState.Modified;
            return Save() > 0;
        }
        /// <summary>
        /// 逗号分割ID处理
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int[] GetIntArrayFromString(string list)
        {
            if (string.IsNullOrEmpty(list))
                return new int[] { };
            string[] temp = list.Split(new[] { ',',',' }, StringSplitOptions.RemoveEmptyEntries);
            if (temp == null || temp.Length <= 0)
                return new int[] { };
            return temp.Select(q => Convert.ToInt32(q)).ToArray();
        }
        /// <summary>
        /// 获取逗号隔开的字符串
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public string[] GetStringArray(string list)
        {
            if (string.IsNullOrEmpty(list))
                return new string[] { };
            string[] temp = list.Split(new[] { ',',',' }, StringSplitOptions.RemoveEmptyEntries);
            return temp;
        }

        #region 异常处理
        /// <summary>
        /// 添加数据异常
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public bool Error_Add(Exception ex)
        {
            DbEntityValidationException efEx = ex as DbEntityValidationException;
            if (efEx != null)
            {
                throw new Exception("添加数据失败," + efEx.Message);
            }
            string Message = ex.Message;
            if (ex.InnerException != null)
            {
                Message += "\n";
                Message += "内部原因:" + ex.InnerException.Message;
                Exception inner = ex.InnerException.InnerException;
                if (inner != null)
                {
                    Message += "\n";
                    Message += "内部原因:" + ex.Message;
                }
            }
            throw new Exception("添加数据失败," + Message);
        }
        /// <summary>
        /// 修改数据异常
        /// </summary>
        /// <returns></returns>
        public bool Error_Modify(Exception ex)
        {
            DbEntityValidationException efEx = ex as DbEntityValidationException;
            if (efEx != null)
            {
                throw new Exception("修改数据失败," + efEx.Message);
            }
            string Message = ex.Message;
            if (ex.InnerException != null)
            {
                Message += "\n";
                Message += ex.InnerException.Message;
            }
            throw new Exception("修改数据失败," + Message);
        }
        /// <summary>
        /// 删除数据异常
        /// </summary>
        public bool Error_Delete(Exception ex)
        {
            DbEntityValidationException efEx = ex as DbEntityValidationException;
            if (efEx != null)
            {
                throw new Exception("删除数据失败," + efEx.Message);
            }
            string Message = ex.Message;
            if (ex.InnerException != null)
            {
                Message += "\n";
                Message += ex.InnerException.Message;
            }
            throw new Exception("删除数据失败," + Message);
        }
        #endregion
    }

更多:

C# EF Core 简单工厂模式,接口多继承实例(三)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值