亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

一 新建類庫

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

聲明實體

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

二 基本倉儲

先實現(xiàn)基本倉儲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>
    /// 基類實現(xiàn)
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class BaseRepository<TEntity> : DbContext<TEntity>, IBaseRepository<TEntity> where TEntity : class, new()
    {
        /// <summary>
        /// 寫入實體數(shù)據(jù)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Add(TEntity model)
        {
            //這里需要注意的是,如果使用了Task.Run()就會導(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ù)據(jù)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Update(TEntity model)
        {
            //這種方式會以主鍵為條件
            var i = await Db.Updateable(model).ExecuteCommandAsync();
            return i > 0;
        }
    }

三 實現(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,//開啟自動釋放模式
        });
        //調(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表的常用操作
}

四 實現(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());
        }
    }

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

五 應(yīng)用層使用

直接引用對應(yīng)的IBaseRepository倉儲

    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就完成了。但是他確實了很ABP很多好用的操作。

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

相關(guān)文章

最新評論