企業項目實戰 .Net Core + Vue/Angular 分庫分表日誌系統四 | 強化設計方案

教程

01 | 模塊化方案一html

02 | 模塊化方案二git

其餘教程預覽

分庫分表項目實戰教程

Git地址: https://github.com/MrChuJiu/EasyLogger

01 | 前言github

02 | 簡單的分庫分表設計sql

03 | 控制反轉搭配簡單業務數據庫

04 | 強化設計方案express

05 | 完善業務自動建立數據庫ide

06 | 最終篇-經過AOP自動鏈接數據庫-完成日誌業務模塊化

強化

先來記錄一下咱們如今的樣子,一會好作個對比
函數

1.在EasyLogger.DbStorage類庫新建 IDbEntity(主鍵約束)、IDbRepository接口(倉儲)

public interface IDbEntity<TPrimaryKey>
    {
        TPrimaryKey Id { get; set; }
    }
public interface IDbRepository<TEntity, TPrimaryKey> : IDisposable
    {
        /// <summary>
        /// 修改Provider
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        IDisposable ChangeProvider(string name);


        /// <summary>
        /// 插入數據
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        int Insert(TEntity entity);

        /// <summary>
        /// 查詢數據
        /// </summary>
        /// <returns></returns>
        List<TEntity> GetQuery(Expression<Func<TEntity, bool>> expression = null);

    }

2.在 EasyLogger.SqlSugarDbStorage 新建 ISqlSugarRepository 接口 繼承自IDbRepository

public interface ISqlSugarRepository<TEntity, TPrimaryKey> : IDbRepository<TEntity, TPrimaryKey>
            where TEntity : class, IDbEntity<TPrimaryKey>
    {
        /// <summary>
        /// 獲取sqlSugar對象
        /// </summary>
        /// <returns></returns>
        SqlSugarClient GetCurrentSqlSugar();
    }

3.新建 SqlSugarRepository 實現接口

補:在 EasyLogger.DbStorage 類庫下新建 DisposeAction

public class DisposeAction : IDisposable
    {
        public static readonly DisposeAction Empty = new DisposeAction(null);

        private Action _action;


        public DisposeAction(Action action)
        {
            _action = action;
        }

        public void Dispose()
        {
            var action = Interlocked.Exchange(ref _action, null);
            action?.Invoke();
        }
    }
public class SqlSugarRepository<TEntity, TPrimaryKey> : ISqlSugarRepository<TEntity, TPrimaryKey>
          where TEntity : class, IDbEntity<TPrimaryKey> , new()
    {
 
        public string ProviderName { get; private set; }
        public string OldProviderName { get; private set; }
        protected readonly ISqlSugarProviderStorage _sqlSugarProviderStorage;

        public SqlSugarRepository(ISqlSugarProviderStorage sqlSugarProviderStorage)
        {
            _sqlSugarProviderStorage = sqlSugarProviderStorage;
        }

        public IDisposable ChangeProvider(string name)
        {
            OldProviderName = ProviderName;
            ProviderName = name;
            return new DisposeAction(() =>
            {
                ProviderName = OldProviderName;
                OldProviderName = null;
            });
        }


        public SqlSugarClient GetCurrentSqlSugar()
        {
            return this._sqlSugarProviderStorage.GetByName(this.ProviderName, SqlSugarDbStorageConsts.DefaultProviderName).Sugar;
        }
      
        public int Insert(TEntity entity)
        {
            return this.GetCurrentSqlSugar().Insertable<TEntity>(entity).ExecuteCommand();
        }

        public List<TEntity> GetQuery(Expression<Func<TEntity, bool>> expression = null)
        {
            return this.GetCurrentSqlSugar().Queryable<TEntity>().Where(expression).ToList();
        }

        public void Dispose()
        {

        }


}

4.改造依賴注入部分

#region SqlSugar
            var defaultDbPath = Path.Combine(PathExtenstions.GetApplicationCurrentPath(), $"{Configuration["EasyLogger:DbName"]}.db");

            services.AddSingleton<ISqlSugarProvider>(new SqlSugarProvider(new SqlSugarSetting()
            {

                Name = SqlSugarDbStorageConsts.DefaultProviderName,
                ConnectionString = @$"Data Source={defaultDbPath}",
                DatabaseType = DbType.Sqlite,
                LogExecuting = (sql, pars) =>
                {
                    Console.WriteLine($"sql:{sql}");
                }

            }));
   
            services.AddTransient(typeof(ISqlSugarRepository<,>), typeof(SqlSugarRepository<,>));
            services.AddTransient(typeof(IDbRepository<,>), typeof(SqlSugarRepository<,>));
            services.AddSingleton<ISqlSugarProviderStorage, DefaultSqlSugarProviderStorage>();
            #endregion

5.改造接口部分

理論部分

到此改造完成 來看下咱們作了什麼this

1.新建公共倉儲來制定約束

2.在SqlSugar中 繼承公共倉儲接口 並 添加本身方法

3.從實現 SqlSugar本身的倉儲業務部分

4.將業務部分從獲取SugarClient 換成 操做倉儲層

分析

如今咱們若是切換到FreeSql是否是隻須要更換依賴注入的部分就能夠了!
而且若是咱們同時使用雙方我只只須要將ISqlSugarRepository做爲咱們的函數注入 就能夠獲取到SugarClient實例來進行操做。

問題

其實咱們是能夠不用注入 ISqlSugarRepository 可是由於2款ORM 都不支持 IQueryable的形式來操做,因此靈活性就變得很低,但願做者後面改進吧。

相關文章
相關標籤/搜索