Class ServiceBase<TEntity, TModel, TFilter>
- Namespace
- Magicalizer.Domain.Services
- Assembly
- Magicalizer.dll
Base of model services: gets models with filtering, sorting, paging and inclusions, creates, edits and deletes them.
public abstract class ServiceBase<TEntity, TModel, TFilter> : IService<TModel, TFilter> where TEntity : class, IEntity, new() where TModel : class, IModel<TEntity> where TFilter : class, IFilter
Type Parameters
TEntityEntity type.
TModelModel type.
TFilterFilter type.
- Inheritance
-
ServiceBase<TEntity, TModel, TFilter>
- Implements
-
IService<TModel, TFilter>
- Derived
- Inherited Members
Constructors
ServiceBase(DbContext, IEnumerable<IQueryPrefilter<TEntity, TFilter>>?, IValidator<TModel>?)
Creates a service.
protected ServiceBase(DbContext dbContext, IEnumerable<IQueryPrefilter<TEntity, TFilter>>? queryPrefilters = null, IValidator<TModel>? validator = null)
Parameters
dbContextDbContextDatabase context.
queryPrefiltersIEnumerable<IQueryPrefilter<TEntity, TFilter>>Query prefilters that apply initial restrictions to queries.
validatorIValidator<TModel>Model validator.
Fields
dbContext
Database context.
protected readonly DbContext dbContext
Field Value
queryPrefilters
Query prefilters that apply initial restrictions to queries.
protected readonly IEnumerable<IQueryPrefilter<TEntity, TFilter>>? queryPrefilters
Field Value
- IEnumerable<IQueryPrefilter<TEntity, TFilter>>
validator
Model validator.
protected readonly IValidator<TModel>? validator
Field Value
- IValidator<TModel>
Methods
CountAsync(TFilter?, CancellationToken)
Counts the models that match the filter.
public virtual Task<int> CountAsync(TFilter? filter = null, CancellationToken cancellationToken = default)
Parameters
filterTFilterFilter to apply.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
CreateAsync(TModel, CancellationToken)
Creates the model. If the database generates the primary key, the primary key values of the model are ignored, and the created model has the generated ones.
public virtual Task<TModel> CreateAsync(TModel model, CancellationToken cancellationToken = default)
Parameters
modelTModelModel to create.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<TModel>
Created model.
Remarks
The model is validated by the validator first (if it's registered) with the Default and Create rule sets. If the database generates the primary key, the primary key values of the entity are reset, so the database generates new ones. Otherwise, if the database rejects the entity, it's checked whether an entity with the same primary key exists. In either case, if the database rejects the entity, it's also checked whether the models it refers to exist.
Exceptions
- ModelAlreadyExistsException
A model with the same primary key already exists.
- ValidationException
The model isn't valid or refers to a model that doesn't exist.
DeleteByKeyValuesAsync(object?[], CancellationToken)
Deletes the entity by the primary key values. The entity must be available through the query prefilters (as it is for getting it).
protected Task DeleteByKeyValuesAsync(object?[] keyValues, CancellationToken cancellationToken = default)
Parameters
keyValuesobject[]Primary key values in the order of primary key properties.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
Exceptions
- ModelNotFoundException
The entity doesn't exist (or isn't available through the query prefilters).
- ModelInUseException
The database restricts the deletion of the entity (e.g. other entities reference it).
EditAsync(TModel, CancellationToken)
Edits the model.
public virtual Task EditAsync(TModel model, CancellationToken cancellationToken = default)
Parameters
modelTModelModel to edit.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
Remarks
The entity of the model must be available through the query prefilters (as it is for getting it). Then the model is validated by the validator (if it's registered) with the Default and Edit rule sets. If the database rejects the entity, it's checked whether the models it refers to exist.
Exceptions
- ModelNotFoundException
The model doesn't exist (or isn't available through the query prefilters).
- ValidationException
The model isn't valid or refers to a model that doesn't exist.
EntityToModel(TEntity?)
Converts the entity to the model.
protected virtual TModel? EntityToModel(TEntity? entity)
Parameters
entityTEntityEntity to convert.
Returns
- TModel
Model, or
nullif the entity isnull.
GetAllAsync(TFilter?, IEnumerable<ISorting<TModel>>?, int?, int?, IEnumerable<IInclusion<TModel>>?, CancellationToken)
Gets the models that match the filter, sorted and paged, with related models included.
public virtual Task<IEnumerable<TModel>> GetAllAsync(TFilter? filter = null, IEnumerable<ISorting<TModel>>? sortings = null, int? offset = null, int? limit = null, IEnumerable<IInclusion<TModel>>? inclusions = null, CancellationToken cancellationToken = default)
Parameters
filterTFilterFilter to apply.
sortingsIEnumerable<ISorting<TModel>>Sortings to apply (property paths with directions).
offsetint?Number of models to skip.
limitint?Maximum number of models to return.
inclusionsIEnumerable<IInclusion<TModel>>Inclusions to apply (property paths of related models).
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<IEnumerable<TModel>>
Models that match the filter.
GetByKeyValuesAsync(object?[], IEnumerable<IInclusion<TModel>>?, CancellationToken)
Gets the model by the primary key values. The entity must be available through the query prefilters.
protected Task<TModel?> GetByKeyValuesAsync(object?[] keyValues, IEnumerable<IInclusion<TModel>>? inclusions = null, CancellationToken cancellationToken = default)
Parameters
keyValuesobject[]Primary key values in the order of primary key properties.
inclusionsIEnumerable<IInclusion<TModel>>Inclusions to apply (property paths of related models).
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<TModel>
Model, or
nullif it doesn't exist.
GetKeyValues(TModel)
Gets the primary key values of the model (e.g. to build the URL of a created model).
public virtual object?[] GetKeyValues(TModel model)
Parameters
modelTModelModel to get the primary key values of.
Returns
- object[]
Primary key values in the order of primary key properties.
GetPrefilteredQuery(TFilter?)
Gets the non-tracking entity query with all query prefilters applied.
protected virtual IQueryable<TEntity> GetPrefilteredQuery(TFilter? filter)
Parameters
filterTFilterFilter that is passed to the query prefilters.
Returns
- IQueryable<TEntity>
Prefiltered query.
GetPreprocessedFilter(TFilter?)
Preprocesses the filter before it's applied to a query (e.g. to add mandatory conditions).
protected virtual TFilter? GetPreprocessedFilter(TFilter? filter = null)
Parameters
filterTFilterFilter to preprocess.
Returns
- TFilter
Preprocessed filter.