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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011127019/article/details/72904436

一、说明

1.本实例是基于.Net Core 1.1,EF Core 1.1的代码实例,仅供参考

2.当前版本的EF Core好像还没有提供模型验证,异常跟踪等

二、实体层,自动 生成

三 、接口层

1.基础 接口 

/// <summary>
/// 基础访问接口,使用泛型
/// </summary>
public interface IBaseAbstract<T> where T : class
{
    #region 基础访问
    /// <summary>
    /// 获取表达树
    /// </summary>
    /// <returns></returns>
    DbSet<T> GetDbSet();
    /// <summary>
    /// 根据主键 获取对象
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    T GetModel(object 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(object id);
    #endregion
}
2.业务接口

namespace NotifyBird.Abstract
{
    /// <summary>
    /// 消息内容,接口
    /// </summary>
    public interface IMessage : IBaseAbstract<Message>
    {
    }
}

四、接口实现,实例工厂层

1.基础操作

/// <summary>
/// 基础类型操作
/// 重点实现基础接口中的操作
/// </summary>
internal class BaseOperate<T> where T : class
{
    public NotifyBirdContext Context = null;
    public BaseOperate()
    {
        Context = new NotifyBirdContext();
    }

    #region 其他操作 
    /// <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;
    }
    #endregion

    #region 基础访问
    /// <summary>
    /// 获取表达树
    /// </summary>
    /// <returns></returns>
    public DbSet<T> GetDbSet()
    {
        return Context.Set<T>();
    }
    /// <summary>
    /// 根据主键 获取对象
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public T GetModel(object id)
    {
        return GetDbSet().Find(id);
    }
    /// <summary>
    /// 添加对象
    /// </summary>
    /// <param name="model">实例对象</param>
    /// <returns></returns>
    public bool Add(T model)
    {
        try
        {
            GetDbSet().Add(model);
            return Save() > 0;
        }
        catch (Exception ex)
        {
            return Error_Add(ex);
        }
    }
    /// <summary>
    /// 修改对象
    /// </summary>
    /// <param name="model">实例对象</param>
    /// <returns></returns>
    public bool Modify(T model)
    {
        try
        {
            return SetModify(model);
        }
        catch (Exception ex)
        {
            return Error_Modify(ex);
        }
    }
    /// <summary>
    /// 删除对象
    /// </summary>
    /// <param name="model">实例对象</param>
    /// <returns></returns>
    public bool Delete(T model)
    {
        try
        {
            GetDbSet().Remove(model);
            return Save() > 0;
        }
        catch (Exception ex)
        {
            return Error_Delete(ex);
        }
    }
    /// <summary>
    /// 删除 对象
    /// </summary>
    /// <param name="id">主键ID</param>
    /// <returns></returns>
    public bool Delete(object id)
    {
        try
        {
            T model = GetModel(id);
            if (model == null)
                throw new Exception($"获取id={id}的对象失败,当前类型:{model.GetType().Name}");
            return Delete(model);
        }
        catch (Exception ex)
        {
            return Error_Delete(ex);
        }
    }
    #endregion

    #region 异常处理  
    private string GetErrorMsg(Exception ex)
    {
        StringBuilder builder = new StringBuilder();
        Exception inner = ex;
        int i = 0;
        while (inner != null)
        {
            i++;
            builder.AppendFormat("内部原因{0}:{1}", i, ex.Message);
            builder.AppendLine();
            inner = inner.InnerException;
        }
        return builder.ToString();
    }
    /// <summary>  
    /// 添加数据异常  
    /// </summary>  
    /// <param name="ex"></param>  
    /// <returns></returns>  
    public bool Error_Add(Exception ex)
    {
        throw new Exception("添加数据失败," + GetErrorMsg(ex));
    }
    /// <summary>  
    /// 修改数据异常  
    /// </summary>  
    /// <returns></returns>  
    public bool Error_Modify(Exception ex)
    {
        throw new Exception("修改数据失败," + GetErrorMsg(ex));
    }
    /// <summary>  
    /// 删除数据异常  
    /// </summary>  
    public bool Error_Delete(Exception ex)
    {
        throw new Exception("删除数据失败," + GetErrorMsg(ex));
    }
    #endregion
}

2.业务操作

    /// <summary>
    /// 消息内容,数据访问操作
    /// </summary>
    internal class MessageOperate : BaseOperate<Message>, IMessage
    {
    }

3.实例工厂

注:此处命名空间和接口 相同 ,则在使用 的时候不需要单独引用 Factory

namespace NotifyBird.Abstract
{
    /// <summary>
    /// 工厂类 ,用于生产接口实例
    /// </summary>
    public class ConcreteFactory
    {
        public static IProject GetProject()
        {
            return new ProjectOpeate();
        }

        public static IMessage GetMessage()
        {
            return new MessageOperate();
        }
    }
}
更多:

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

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

展开阅读全文

没有更多推荐了,返回首页