SqlSugar的几种连接方式

1、最简单的使用

public class DatabaseService
{
    private static readonly Lazy<SqlSugarClient> _db = new Lazy<SqlSugarClient>(() =>
    {
        var db = new SqlSugarClient(new ConnectionConfig
        {
            ConnectionString = ConnectionDbConfig.ConnectionString,
            DbType = DbType.SqlServer,
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute
        });
        return db;
    });
}

2、使用自带的SimpleClient

 public class DatabaseService<T> : SimpleClient<T> where T : class, new()
 {
     private static readonly Lazy<SqlSugarClient> _db = new Lazy<SqlSugarClient>(() =>
     {
         var db = new SqlSugarClient(new ConnectionConfig
         {
             ConnectionString = ConnectionDbConfig.ConnectionString,
             DbType = DbType.SqlServer,
             IsAutoCloseConnection = true,
             InitKeyType = InitKeyType.Attribute
         });
         return db;
     });
}

3、在第一种基础上自定义部分方法

 public class DataBaseProvider<TEntity> where TEntity : class, new()
 {
     private SqlSugarClient _db;

     public DataBaseProvider()
     {
         _db = new SqlSugarClient(new ConnectionConfig()
         {

             ConnectionString = ConnectionDbConfig.ConnectionString,
             DbType = DbType.SqlServer,
             IsAutoCloseConnection = true,
             InitKeyType = InitKeyType.Attribute

         });
     }

     public ISugarQueryable<TEntity> Queryable
     {
         get { return _db.Queryable<TEntity>(); }
     }

     public ISugarQueryable<TEntity> QueryableList()
     {
         return _db.Queryable<TEntity>();
     }

     public void Insert(TEntity entity)
     {
         _db.Insertable(entity).ExecuteCommand();
     }

     public void Update(TEntity entity)
     {
         _db.Updateable(entity).ExecuteCommand();
     }

     public void Delete(TEntity entity)
     {
         _db.Deleteable(entity).ExecuteCommand();
     }

     public void SaveData(TEntity entity)
     {
         //_db.Saveable(entity).ExecuteReturnEntity(); 过时
         _db.Storageable(entity).ExecuteCommand();
     }
     public void DeleteById(object id)
     {
         _db.Deleteable<TEntity>(id).ExecuteCommand();
     }
 }

4、复杂业务需要自定义泛型接口,(包括但不仅限于输入输出DTO),此时子类方法需要用到AutoMapper或者Maspter

 public interface IBaseRepository<TEntity> where TEntity : class, new()
 {
     Task<bool> CreateAsync(TEntity entity);
     Task<bool> DeleteAsync(int id);
     Task<bool> EditAsync(TEntity entity);
     Task<TEntity> FindAsync(int id);
     Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> func);
     /// <summary>
     /// 查询全部的数据
     /// </summary>
     /// <returns></returns>
     Task<List<TEntity>> QueryAsync();
     /// <summary>
     /// 自定义条件查询
     /// </summary>
     /// <param name="func"></param>
     /// <returns></returns>
     Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func);
     /// <summary>
     /// 分页查询
     /// </summary>
     /// <param name="page"></param>
     /// <param name="size"></param>
     /// <param name="total"></param>
     /// <returns></returns>
     Task<List<TEntity>> QueryAsync(int page, int size, RefAsync<int> total);
     /// <summary>
     /// 自定义条件分页查询
     /// </summary>
     /// <param name="func"></param>
     /// <param name="page"></param>
     /// <param name="size"></param>
     /// <param name="total"></param>
     /// <returns></returns>
     Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func, int page, int size, RefAsync<int> total);
 }
 public class BaseRepository<TEntity> : SimpleClient<TEntity>, IBaseRepository<TEntity> where TEntity : class, new()
{
    public BaseRepository(ISqlSugarClient context = null) : base(context)
    {
        base.Context = xxx;

    }
    public async Task<bool> CreateAsync(TEntity entity)
    {
        return await base.InsertAsync(entity);
    }
    public async Task<bool> DeleteAsync(int id)
    {
        return await base.DeleteByIdAsync(id);
    }

    public async Task<bool> EditAsync(TEntity entity)
    {
        return await base.UpdateAsync(entity);
    }
    //导航查询
    public virtual async Task<TEntity> FindAsync(int id)
    {
        return await base.GetByIdAsync(id);
    }

    public async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> func)
    {
        return await base.GetSingleAsync(func);
    }

    public virtual async Task<List<TEntity>> QueryAsync()
    {
        return await base.GetListAsync();
    }

    public virtual async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func)
    {
        return await base.GetListAsync(func);
    }

    public virtual async Task<List<TEntity>> QueryAsync(int page, int size, RefAsync<int> total)
    {
        return await base.Context.Queryable<TEntity>()
          .ToPageListAsync(page, size, total);
    }

    public virtual async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func, int page, int size, RefAsync<int> total)
    {
        return await base.Context.Queryable<TEntity>()
          .Where(func)
          .ToPageListAsync(page, size, total);
    }
}
posted @ 2024-01-15 02:49  孤沉  阅读(403)  评论(2编辑  收藏  举报