展示一下库存服务实现类,支持动态改变数据库,动态表达式 等,写法还是很舒适的
/// <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