01 | 模塊化方案一html
02 | 模塊化方案二git
01 | 前言github
04 | 強化設計方案express
06 | 最終篇-經過AOP自動鏈接數據庫-完成日誌業務模塊化
先來記錄一下咱們如今的樣子,一會好作個對比
函數
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); }
public interface ISqlSugarRepository<TEntity, TPrimaryKey> : IDbRepository<TEntity, TPrimaryKey> where TEntity : class, IDbEntity<TPrimaryKey> { /// <summary> /// 獲取sqlSugar對象 /// </summary> /// <returns></returns> SqlSugarClient GetCurrentSqlSugar(); }
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() { } }
#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
到此改造完成 來看下咱們作了什麼this
如今咱們若是切換到FreeSql是否是隻須要更換依賴注入的部分就能夠了!
而且若是咱們同時使用雙方我只只須要將ISqlSugarRepository做爲咱們的函數注入 就能夠獲取到SugarClient實例來進行操做。
其實咱們是能夠不用注入 ISqlSugarRepository 可是由於2款ORM 都不支持 IQueryable的形式來操做,因此靈活性就變得很低,但願做者後面改進吧。