# 实体服务基类

具体代码可查看LessSharp.ServiceBase目录下的EntityServer类。

这是QueryService的派生类,添加了对实体增删改的方法与相关属性,所以查询服务类有的方法,它也具有,这里只列出实体处理相关的方法,查询方面的文档请查看查询服务基类的说明。

一般是用于实现了IEntity接口的实体对象,有两种泛型定义,如下所示

public abstract class EntityService<TCreateDto, TUpdateDto, TDto, TQueryDto, TEntity, TId> 
    : QueryService<TDto, TQueryDto, TEntity, TId>
    where TEntity : class, IEntity, new()
    where TQueryDto : QueryDto
    where TCreateDto : class
    where TUpdateDto : class
    {}
泛型类型 说明
TCreateDto 创建传输对象
TUpdateDto 更新传输对象
TDto 显示传输对象
TQueryDto 查询参数传输对象
TEntity 实体对象,一般是实现了IEntity的类
TId IEntity对象Id字段的类型

例如

我要创建一个用户实体服务类UserService,用户实体为User(已实现IQuery接口),Id字段类型为int,创建DtoUserCreateDto,更新DtoUserUpdateDto,显示DtoUserDto,查询参数DtoUserQueryDto,那类的定义如下所示:

 public class UserService 
     : EntityService<UserCreateDto,UserUpdateDto,UserDto, UserQueryDto, User, int>
{

}

# 属性

属性名 修饰 类型 说明
DbSet protected DbSet<TEntity> 实体操作类
EntityHandlers protected List<IEntityHandler<TEntity>> 实体处理器列表
AutoSaveChanges public bool 是否自动进行保存,默认为true
IsFindOldEntity protected bool 是否在更新删除时先查出数据库里面的实体出来
DefaultUpdateExcludeField protected bool 更新操作中的fields是用来排除字段修改,还是仅修改props里面的字段,默认为true
CreateDtoToEntityMapExpression protected Expression<Func<TCreateDto, TEntity>> 创建对象映射成实体表达式,赋值时会采用这个映射规则,不赋值会使用autoMapper进行自动映射
UpdateDtoToEntityMapExpression protected Expression<Func<TUpdateDto, TEntity>> 更新对象映射成实体表达式,赋值时会采用这个映射规则,不赋值会使用autoMapper进行自动映射

提示

如果需要修改属性的值 ,请在服务类的构造函数中进行修改。

# 方法

方法名 修饰 说明
CreateAsync protected
async
根据传输对象创建实体,会经过实体处理器及事件
Task<TId> CreateAsync(TCreateDto createDto, Expression<Func<TCreateDto, TEntity>> mapExpression)
UpdateAsync protected
async
根据传输对象更新实体,会经过实体处理器及事件
Task<TId> UpdateAsync(TUpdateDto updateDto, Expression<Func<TUpdateDto, TEntity>> mapExpression)
SaveAsync protected
async
根据传输对象保存实体,会经过实体处理器及事件
Task<TId> SaveAsync(TUpdateDto updateDto, Expression<Func<TUpdateDto, TEntity>> mapExpression)
DeleteAsync protected
async
删除实体,会经过实体处理器及事件
Task<int> DeleteAsync(TEntity entity)
DeleteEntityByIdAsync protected
async
根据Id值删除实体,会经过实体处理器及事件
Task<int> DeleteEntityByIdAsync(TId id)
DeleteEntitiesByIdsAsync protected
async
根据Id数组批量删除实体
Task<int> DeleteEntitiesByIdsAsync(TId[] ids)
SoftDeleteAsync protected
async
软删除,会经过实体处理器及事件
Task<int> SoftDeleteAsync(TEntity entity)
SoftDeleteEntityByIdAsync protected
async
根据Id值软删除实体,会经过实体处理器及事件
Task<int> SoftDeleteEntityByIdAsync(TId id)
SoftDeleteEntitiesByIdsAsync protected
async
根据Id数组批量软删除实体
Task<int> SoftDeleteEntitiesByIdsAsync(TId[] ids)
CreateEntityAsync<T> protected
async
直接创建实体,不会经过实体处理器及事件
Task<int> CreateEntityAsync<T>(T entity)
UpdateEntityAsync<T> protected
async
直接更新实体,不会经过实体处理器及事件,不会更新关联实体
Task<int> UpdateEntityAsync<T>(T entity,List<Expression<Func<T, object>>> excludeOrIncludeFields = null, bool excludeField = true) where T : class
DeleteEntityAsync<T> protected
async
直接删除实体,不会经过实体处理器及事件
Task<int> DeleteEntityAsync<T>(T entity)
SaveEntities<T,TKey> protected 保存实体数组,传入一个新数组数据跟一个数据库的旧数组数据,把数据库的数据同步为新数据,需在调用后手动进行SaveChange
void SaveEntities<T, TKey>(IEnumerable<T> newEntities,IEnumerable<T> oldEntities,Expression<Func<T, TKey>> entityKey,Action<T> afterUpdate = null,Action<T> afterDelete = null)
SaveEntities<T> protected 保存实体数组,传入一个新数组数据跟一个数据库的旧数组数据,把数据库的数据同步为新数据,需在调用后手动进行SaveChange
void SaveEntities<T>(IEnumerable<T> newEntities,IEnumerable<T> oldEntities,Func<T, T, bool> condition,Action<T> afterUpdate = null,Action<T> afterDelete = null)
CreateAsync public
virtual
async
创建实体
Task<TId> CreateAsync(TCreateDto createDto)
Create public
virtual
创建实体
TId Create(TCreateDto createDto)
UpdateAsync public
virtual
async
更新实体
Task<TId> UpdateAsync(TUpdateDto updateDto)
Update public
virtual
更新实体
TId Update(TUpdateDto updateDto)
SaveAsync public
virtual
保存实体
Task<TDto> GetByIdAsync(TId id)
Save public
virtual
[可重写] 根据Id获取单个对象
TDto GetById(TId id)
DeleteByIdAsync public
virtual
async
根据Id删除一个实体
Task<T> GetByIdAsync<T>(TId id, Expression<Func<TQuery, T>> selectExp = null)
DeleteById public
virtual
根据Id删除一个实体
int DeleteById(TId id)
DeleteByIdsAsync public
virtual
async
根据Id数组批量删除实体
Task<int> DeleteByIdsAsync(TId[] ids)
DeleteByIds public
virtual
根据Id数组批量删除实体
int DeleteByIds(TId[] ids)
SoftDeleteByIdAsync public
virtual
async
根据Id软删除一个实体
Task<int> SoftDeleteByIdAsync(TId id)
SoftDeleteById public
virtual
根据Id软删除一个实体
int SoftDeleteById(TId id)
SoftDeleteByIdsAsync public
virtual
async
根据Id数组批量软删除实体
Task<int> SoftDeleteByIdsAsync(TId[] ids)
SoftDeleteByIds public
virtual
根据Id数组批量软删除实体
int SoftDeleteByIds(TId[] ids)

# 事件

事件名 说明
OnCreating 实体创建前处理事件
void OnCreating(TEntity entity, TCreateDto createDto)
OnCreated 实体创建后处理事件
void OnCreated(TEntity entity, TCreateDto createDto)
OnUpdating 实体更新前处理事件
void OnUpdating(TEntity newEntity, TEntity oldEntity, TUpdateDto updateDto, List<Expression<Func<TEntity, object>>> excludeOrIncludeFields, bool isExcludeField)
OnUpdated 实体更新后处理事件
void OnUpdated(TEntity entity, TUpdateDto updateDto)
OnSaving 保存实体前的处理事件
void OnSaving(TEntity newEntity, TEntity oldEntity, TUpdateDto saveDto)
OnSaved 保存实体后的处理事件
void OnSaved(TEntity entity, TUpdateDto saveDto)
OnDeleting 实体删除前处理事件
void OnDeleting(TEntity entity)
OnDeleted 实体删除后处理事件
void OnDeleted(TEntity entity)
OnSoftDeleting 实体软删除前处理事件
void OnSoftDeleting(TEntity entity, List<Expression<Func<TEntity, object>>> includeProps)
OnSoftDeleted 实体软删除后处理事件
void OnSoftDeleted(TEntity entity)

提示

这里的事件不是真正的事件类型,而是一些带有virtual修饰的方法,通过重写此方法达到事件的效果。

上次更新: 1/3/2021, 9:01:12 PM