仓储可以让你的方法更加的规范,需要什么方法都封装到仓储中,下次就能重复使用,并且能很好的和你业务拆分开
这种设计模式简单粗暴用起来也方便
只需要几行代码就搞定了,我们定义的Repository是公用类,不能包含具体的类务逻辑,即使不使用扩展方法自带的方法也够开发
public class Repository<T> : SimpleClient<T> where T : class, new() { public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null { if (context == null) { base.Context = new SqlSugarClient(new ConnectionConfig() { DbType = SqlSugar.DbType.SqlServer, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConnectionString = Config.ConnectionString }); } } /// <summary> /// 扩展方法,自带方法不能满足的时候可以添加新方法 /// </summary> /// <returns></returns> public List<T> CommQuery(string json) { //base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作 return null; } }
注意:public Repository(ISqlSugarClient context = null) 默认值不能少,不然无参方式 IOC 不好注入
继承的时候指定类型为Order,那么OrderService的所有操作都是针对Order表的
//订单服务 public class OrderService: Repository<Order> { //业务方法 public Order GetOrderByName(string name) { return base.GetSingle(it=>it.Name==name); //GetSingle是仓储自带的方法,本文最下方有详细介绍 } } //Order.cs文件 public class Order { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } }
当继承了Repository<Order>就能使用仓储里面的方法,但只是针对Order表的操作,可我还想使用OrderItem这个仓储怎么办?
用法如下:
public class OrderService : Repository<Order> { public List<OrderItem> GetOrderItems() { var orderItemDb = base.Change<OrderItem>();//切换仓仓(新功能) return orderItemDb.GetList(); } public List<Order> GetOrders() { return base.GetList(); //使用自已的仓储方法 } public List<Custom> GetCustom() { return base.Context.Queryable<Custom>().ToList(); //使用完整SqlSugar } }
正确用法1: 直接new
OrderService orderService=new OrderService();//不能静态
正确用法2: 使用IOC
Autoface IOC http://www.donet5.com/Home/Doc?typeId=1223
自带IOC http://www.donet5.com/Home/Doc?typeId=1211
正常情况下主线程就能完成所有开发需求,但是特殊要求需要开线程,代码需要如下改动
public class OrderService: Repository<Order> { public void Test() { var list = base.GetList();//主线程 Task.Run(() => { //工作线程 OrderService bll = new OrderService(); //new出一个新实例 bll.GetList(); }); } }
//查询 var data1 = base.GetById(1);//根据id查询 var data2 = base.GetList();//查询所有 var data3 = base.GetList(it => it.Id == 1); //根据条件查询 var data4 = base.GetSingle(it => it.Id == 1); var p = new PageModel() { PageIndex = 1, PageSize = 2 }; var data5 = base.GetPageList(it => it.Name == "xx", p); Console.Write(p.PageCount); var data6 = base.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc); Console.Write(p.PageCount); List<IConditionalModel> conModels = new List<IConditionalModel>(); conModels.Add(new ConditionalModel(){FieldName="id",ConditionalType=ConditionalType.Equal,FieldValue="1"});//id=1 var data7 = base.GetPageList(conModels, p, it => it.Name, OrderByType.Asc); base.AsQueryable().Where(x => x.Id == 1).ToList(); //插入 base.Insert(insertObj); base.InsertRange(InsertObjs); var id = base.InsertReturnIdentity(insertObj); base.AsInsertable(insertObj).ExecuteCommand(); //删除 base.Delete(insertObj); base.DeleteById(1); base.DeleteByIds(new object [] { 1, 2 }); //数组带是 ids方法 ,封装传 object [] 类型 base.Delete(it => it.Id == 1); base.AsDeleteable().Where(it => it.Id == 1).ExecuteCommand(); //更新 base.Update(insertObj); base.UpdateRange(InsertObjs); base.Update(it => new Order() { Name = "a", }, it => it.Id == 1); base.AsUpdateable(insertObj).UpdateColumns(it=>new { it.Name }).ExecuteCommand();
18K大小, 可以运行的完整DEMO,不需要建库建表,直接运行
学完这篇贴子,你就了解了最主流的仓储模式,简单好用,开始你的项目吧!!!!
2016 © donet5.comApache Licence 2.0