0%

SqlSugar 仓储

仓储可以让你的方法更加的规范,需要什么方法都封装到仓储中,下次就能重复使用,并且能很好的和你业务拆分开

这种设计模式简单粗暴用起来也方便

仓储方法

仓储有一套自带的数据库操作方法,比起 db.xx.xxx 来说可能更简便些满足一些常用需求,复杂的功能还是用 db.xxx.xxx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//查询
var data1 = base.GetById(1); //根据 id 查询
var data2 = base.GetList(); //查询所有
var data3 = base.GetList(it => it.Id == 1); //TOP1 条件
var data4 = base.GetSingle(it => it.Id == 1); //查询单条记录,结果集不能超过 1,不然会提示错误
var data = base.GetFirst(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);
var data8 = base.AsQueryable().Where(x => x.Id == 1).ToList();//使用 Queryable

//插入
base.Insert(insertObj);
base.InsertRange(InsertObjs);
var id = base.InsertReturnIdentity(insertObj); //插入返回自增
var SnowflakeId=InsertReturnSnowflakeId(insertObj); //插入返回雪花 ID
base.AsInsertable(insertObj).ExecuteCommand(); //复杂功能使用 Insertable

//删除
base.Delete(T); //实体删除 需要有主键
base.Delete(List<T>); //集合删除 需要有主键
base.DeleteById(1);
base.DeleteByIds(new object [] { 1, 2 }); //数组带是 ids 方法 ,封装传 object [] 类型
//技巧 int [] 转换成 object[] 写法:ids.Cast<object>().ToArray()
base.Delete(it => it.Id == 1);
base.AsDeleteable().Where(it => it.Id == 1).ExecuteCommand();//复杂功能用 Deleteable

//更新
base.Update(insertObj);
base.UpdateRange(InsertObjs);
base.Update(it => new Order() { Name = "a" /*可以多列*/ }, it => it.Id == 1); //只更新 name 并且 id=1
base.AsUpdateable(insertObj).UpdateColumns(it=>new { it.Name }).ExecuteCommand();//复杂功能用 Updateable

//高级操作
base.AsSugarClient // 获取完整的 db 对象
base.AsTenant // 获取多库相关操作

//切换仓储
base.ChangeRepository<Repository<OrderItem>>() //支持多租户和扩展方法,使用 SqlSugarScope 单例(或者 SqlSugarClient Scope 注入)
base.Change<OrderItem>() //只支持自带方法和单库

创建仓储

只需要几行代码就搞定了,定义的 Repository 是公用类,不能包含具体的类务逻辑,即使不使用扩展方法自带的方法也够开发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Repository<T> : SimpleClient<T> where T : class, new()
{
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于 null
{
base.Context=context;//ioc 注入的对象
// base.Context=DbScoped.SugarScope; SqlSugar.Ioc 这样写
// base.Context=DbHelper.GetDbInstance() 当然也可以手动去赋值
}

/// <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 表的

1
2
3
4
5
6
7
8
9
//订单服务
public class OrderService: Repository<Order>
{
//业务方法
public Order GetOrderByName(string name)
{
return base.GetSingle(it=>it.Name==name); //GetSingle 是仓储自带的方法,本文最上方有详细介绍
}
}

调用外部仓储

当继承了Repository<Order>就能使用仓储里面的方法,但只是针对 Order 表的操作,可还想使用 OrderItem 这个仓储怎么办?

用法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class OrderService : Repository<Order>
{
public List<OrderItem> GetOrderItems()
{
//切换仓储 请升级到 5.0.2.9+
var orderItemDb = base.ChangeRepository<Repository<OrderItem>>();;

//base.Change 已过期 不支持多库和自定义方法
return orderItemDb.GetList();
}

public List<Order> GetOrders()
{
return base.GetList(); //使用自已的仓储方法
}

public List<Custom> GetCustom()
{
return base.Context.Queryable<Custom>().ToList(); //使用完整 SqlSugar
}
}

仓储中使用事务

1
2
3
4
5
6
7
8
9
10
11
12
13
try
{
base.AsTenant().BeginTran();

//你的增查改方法

base.AsTenant().CommitTran();
}
catch (Exception ex)
{
base.AsTenant().RollbackTran();
throw;
}

欢迎关注我的其它发布渠道