首页 > 其他分享 >成功使用自研ORM框架赚到第一桶金

成功使用自研ORM框架赚到第一桶金

时间:2022-12-01 12:11:19浏览次数:48  
标签:httpContextAccessor 自研 req db ORM var HttpContext 第一桶金 public

展示一下库存服务实现类,支持动态改变数据库,动态表达式 等,写法还是很舒适的

/// <summary>
    /// 库存服务实现类
    /// </summary>
    public class StockService : IStockService
    {

        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly IDbContext db;

        /// <summary>
        /// Http上下文访问
        /// </summary>
        private readonly IHttpContextAccessor httpContextAccessor;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db"></param>
        public StockService(IDbContext db, IHttpContextAccessor httpContextAccessor)
        {
            this.db = db;
            this.httpContextAccessor = httpContextAccessor;
            if (httpContextAccessor.HttpContext != null && !string.IsNullOrWhiteSpace(httpContextAccessor.HttpContext.Request.Headers["SysCode"]))
            {
                db.ChangeDb(httpContextAccessor.HttpContext.Request.Headers["SysCode"]);
            }
        }

        /// <summary>
        /// 获取商品库存
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<List<GoodsStock>> GetGoodsStocks(ReqGoodsStock req)
        {
            var ex = DynamicWhereExp.Create<GoodsStock>()
                .AndIF(req.KTypeIds.Any(), a => SqlFunc.In(a.KTypeId, req.KTypeIds))
                .AndIF(req.PTypeIds.Any(), a => SqlFunc.In(a.PTypeId, req.PTypeIds));
            var refAsync = new RefAsync<int>();
            var data = await db.Query<GoodsStock>().Where(ex.Build()).ToPageListAsync(req.Pagination.Page, req.Pagination.PageSize, refAsync);
            req.Pagination.Count = refAsync.Value;
            return data;
        }
    }

  

搜索

复制

标签:httpContextAccessor,自研,req,db,ORM,var,HttpContext,第一桶金,public
From: https://www.cnblogs.com/China-Mr-zhong/p/16941045.html

相关文章