欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ABP引入SqlSugar框架的簡單版創(chuàng)建使用

 更新時(shí)間:2022年04月29日 14:38:21   作者:騙你學(xué)計(jì)算機(jī)  
這篇文章主要為大家介紹了ABP引入SqlSugar框架的簡單版創(chuàng)建使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

上一篇引入了Dapper框架,估計(jì)大家都會(huì)用了。但是很多都被封裝,想探究原理的小伙伴就很失望了。那么今天的SqlSugar就說說大概思路。簡單版和ABP的關(guān)聯(lián)比較少,未來我還會(huì)寫一期切合ABP框架的,小伙伴稍等下。

一 新建類庫

為了代碼清晰,我新建了一個(gè)類庫。引入了SqlSugar的框架包,2個(gè)倉儲(chǔ)類,1個(gè)DbContext

聲明實(shí)體

    [SugarTable("BasBloodLevel")]
    public class BasBloodLevel
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        public string Code { get; set; }
    }

二 基本倉儲(chǔ)

先實(shí)現(xiàn)基本倉儲(chǔ)IBaseRepository 與BaseRepository

    /// <summary>
    /// 基類接口,其他接口繼承該接口
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public interface IBaseRepository<TEntity> where TEntity : class
    {
        /// <summary>
        /// 根據(jù)ID查詢
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        Task<TEntity> QueryByID(object objId);
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        Task<bool> Add(TEntity model);
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        Task<bool> Update(TEntity model);
        /// <summary>
        /// 刪除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        Task<bool> DeleteByIds(object[] ids);
    }
    /// <summary>
    /// 基類實(shí)現(xiàn)
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class BaseRepository<TEntity> : DbContext<TEntity>, IBaseRepository<TEntity> where TEntity : class, new()
    {
        /// <summary>
        /// 寫入實(shí)體數(shù)據(jù)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Add(TEntity model)
        {
            //這里需要注意的是,如果使用了Task.Run()就會(huì)導(dǎo)致 sql語句日志無法記錄改成下面的
            //var i = await Task.Run(() => Db.Insertable(model).ExecuteCommand());
            var i = await Db.Insertable(model).ExecuteCommandAsync();
            return i > 0;
        }
        /// <summary>
        /// 根據(jù)ID刪除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByIds(object[] ids)
        {
            var i = await Db.Deleteable<TEntity>().In(ids).ExecuteCommandAsync();
            return i > 0;
        }
        /// <summary>
        /// 根據(jù)ID查詢一條數(shù)據(jù)
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        public async Task<TEntity> QueryByID(object objId)
        {
            return await Db.Queryable<TEntity>().InSingleAsync(objId);
        }
        /// <summary>
        /// 更新實(shí)體數(shù)據(jù)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Update(TEntity model)
        {
            //這種方式會(huì)以主鍵為條件
            var i = await Db.Updateable(model).ExecuteCommandAsync();
            return i > 0;
        }
    }

三 實(shí)現(xiàn)SqlSugar的DB

此處的ConnectionString 地址,我們可以直接讀取 ABP框架的配置文件,但是為了方便我直接寫死了

    public class DbContext<T> where T : class, new()
    {
        public DbContext()
    {
        Db = new SqlSugarClient(new ConnectionConfig()
        {
//數(shù)據(jù)庫地址我們可以直接讀取 ABP框架的配置文件,但是為了方便我直接寫死了
            ConnectionString = "Server=****; Database=****; Uid=sa; Pwd=****;MultipleActiveResultSets=true;",
            DbType = DbType.SqlServer,
            InitKeyType = InitKeyType.Attribute,//從特性讀取主鍵和自增列信息
            IsAutoCloseConnection = true,//開啟自動(dòng)釋放模式
        });
        //調(diào)式代碼 用來打印SQL 
        Db.Aop.OnLogExecuting = (sql, pars) =>
        {
            Console.WriteLine(sql + "\r\n" +
                Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            Console.WriteLine();
        };
    }
    //注意:不能寫成靜態(tài)的
    public SqlSugarClient Db;//用來處理事務(wù)多表查詢和復(fù)雜的操作
    public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用來操作當(dāng)前表的數(shù)據(jù)
    public SimpleClient<BasBloodLevel> BasBloodLevelDb { get { return new SimpleClient<BasBloodLevel>(Db); } }//用來處理User表的常用操作
}

四 實(shí)現(xiàn)依賴注入

這樣我們就能全局使用了

    [DependsOn(typeof(AbpZeroCoreModule))]
    public class Module : AbpModule
    {
        public override void Initialize()
        {
            IocManager.Register(typeof(IBaseRepository<>), typeof(BaseRepository<>), DependencyLifeStyle.Singleton);
            //依賴注入程序集 
            IocManager.RegisterAssemblyByConvention(typeof(Module).GetAssembly());
        }
    }

既然要實(shí)現(xiàn)依賴注入,那肯定要初始化這個(gè)類觸發(fā)注入了。我選擇在EF層里加,這樣可以不影響原有的EF層初始化

五 應(yīng)用層使用

直接引用對(duì)應(yīng)的IBaseRepository倉儲(chǔ)

    public class BasBloodBreedAppService : BloodTestLibSystemAppServiceBase,IApplicationService
    {
        private  IBaseRepository<BasBloodLevel> _baseRepository { get; set; }
        public BasBloodBreedAppService(IBaseRepository<BasBloodLevel> baseRepository) {
            _baseRepository = baseRepository;
        }
        public async Task<BasBloodLevel> GetBase() {
           var ce=await _baseRepository.QueryByID(1);
            return ce;
        }
    }

證明一下我是成功的

此刻引入SqlSugar就完成了。但是他確實(shí)了很ABP很多好用的操作。

下一篇繼續(xù)優(yōu)化,更多關(guān)于ABP引入SqlSugar框架的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論