zoukankan      html  css  js  c++  java
  • 企业项目实战 .Net Core + Vue/Angular 分库分表日志系统四 | 强化设计方案

    教程

    01 | 模块化方案一

    02 | 模块化方案二

    其他教程预览

    分库分表项目实战教程

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

    01 | 前言

    02 | 简单的分库分表设计

    03 | 控制反转搭配简单业务

    04 | 强化设计方案

    05 | 完善业务自动创建数据库

    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.改造接口部分

    理论部分

    到此改造完成 来看下我们做了什么

    1.新建公共仓储来制定约束

    2.在SqlSugar中 继承公共仓储接口 并 添加自己方法

    3.从实现 SqlSugar自己的仓储业务部分

    4.将业务部分从获取SugarClient 换成 操作仓储层

    分析

    现在我们如果切换到FreeSql是不是只需要更换依赖注入的部分就可以了!
    并且如果我们同时使用双方我只只需要将ISqlSugarRepository作为我们的函数注入 就可以获取到SugarClient实例来进行操作。

    问题

    其实我们是可以不用注入 ISqlSugarRepository 但是因为2款ORM 都不支持 IQueryable的形式来操作,所以灵活性就变得很低,希望作者后面改进吧。

  • 相关阅读:
    数据库表与视图的区别
    maven中snapshot版本和正式版本的区别
    @Retention注解
    java泛型以及通配符
    git 删除了本不应该删除的commit 如何恢复
    ES group分组聚合的坑
    solution for 1006 中国剩余定理
    solution for POJ 1001
    ondraw() 和dispatchdraw()的区别
    android几种定时器机制及区别(转载整理)
  • 原文地址:https://www.cnblogs.com/MrChuJiu/p/13539186.html
Copyright © 2011-2022 走看看