Skip to content

Repository

Concept

A repository hides persistence details: the domain talks to an interface, infrastructure implements it (EF, mooSQL, …). In DDD, one repository per aggregate root is typical.

Why use it

  1. Domain code focuses on rules, not SQL/ORM details.
  2. Swapping ORMs mostly changes the implementation, not domain contracts.

Setup

c#
private readonly SooRepository<SysRegion> _sysRegionRep;

public SysRegionService(SqlSugarRepository<SysRegion> sysRegionRep)
{
    _sysRegionRep = DBCash.useRepo<SysRegion>(0);
}

GetList

c#
<List<SysRegion> list= _sysRegionRep.GetList(u => u.Pid == input.Id);

First 100 rows:

c#
<List<SysRegion> list= _sysRegionRep.GetList(100);

GetFirst

c#
var sysRegion =  _sysRegionRep.GetFirst(u => u.Id == input.Id);

GetPageList

c#
var res=Rep.GetPageList(input.Page, input.PageSize, (c, d) => {
    c.where(()=>d.CreateTime,input.StartTime,">=")
    .where(()=>d.CreateTime,input.EndTime,"<=")
    .orderByDesc(()=>d.CreateTime);
});

GetTreeList

c#
var menuList = _sysMenuRep.GetTreeList((m) => m.Pid, 0
    , (c, m) => {
        c.where(() => m.Type, MenuTypeEnum.Btn, "!=")
        .where(() => m.Status, StatusEnum.Enable)
        .orderBy(() => m.OrderNo)
        .orderBy(() => m.Id);
    });

GetChildList

c#
var list = Rep.GetChildList((r) => r.Pid, input.Id);

Insert / InsertRange

c#
_sysRegionRep.Insert(sysRegion)
c#
_sysRegionRep.InsertRange(sysRegions)

IsAny

c#
var isExist =  _sysRegionRep.IsAny(u => u.Name == input.Name && u.Code == input.Code);
if (isExist)
    throw Oops.Oh(ErrorCodeEnum.R2002);

Update / UpdateRange

c#
_sysRegionRep.Update(new SysRegion());
c#
_sysRegionRep.UpdateRange(new SysRegion());

DeleteByIds

c#
public async Task DeleteRegion(DeleteRegionInput input)
{
    var regionTreeList = _sysRegionRep.useBus().Where(u => u.Pid == input.Id).ToList();
    var regionIdList = regionTreeList.Select(u => u.Id).ToList();
    regionIdList.Add(input.Id);
    _sysRegionRep.DeleteByIds(regionIdList);
}

Delete

c#
_sysPosRep.Delete(u => u.Id == input.Id);

ChangeTo

c#
var hasPosEmp =  _sysPosRep.ChangeTo<SysUser>()
    .IsAny(u => u.PosId == input.Id);