Table of Contents

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

TEntity

Entity type.

TModel

Model type.

TFilter

Filter 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

dbContext DbContext

Database context.

queryPrefilters IEnumerable<IQueryPrefilter<TEntity, TFilter>>

Query prefilters that apply initial restrictions to queries.

validator IValidator<TModel>

Model validator.

Fields

dbContext

Database context.

protected readonly DbContext dbContext

Field Value

DbContext

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

filter TFilter

Filter to apply.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<int>

Number of models that match the filter.

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

model TModel

Model to create.

cancellationToken CancellationToken

Token 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

keyValues object[]

Primary key values in the order of primary key properties.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task

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

model TModel

Model to edit.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task

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

entity TEntity

Entity to convert.

Returns

TModel

Model, or null if the entity is null.

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

filter TFilter

Filter to apply.

sortings IEnumerable<ISorting<TModel>>

Sortings to apply (property paths with directions).

offset int?

Number of models to skip.

limit int?

Maximum number of models to return.

inclusions IEnumerable<IInclusion<TModel>>

Inclusions to apply (property paths of related models).

cancellationToken CancellationToken

Token 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

keyValues object[]

Primary key values in the order of primary key properties.

inclusions IEnumerable<IInclusion<TModel>>

Inclusions to apply (property paths of related models).

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<TModel>

Model, or null if 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

model TModel

Model 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

filter TFilter

Filter 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

filter TFilter

Filter to preprocess.

Returns

TFilter

Preprocessed filter.