1. 程式人生 > >C# EF Core 簡單工廠模式,介面多繼承例項(三)

C# EF Core 簡單工廠模式,介面多繼承例項(三)

一、說明

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();
        }
    }
}
更多: