CrudRepository

v1.0

Description


Base class of the library, the CrudRepository<TEntity, TKey> class provides the most basic features required to handle CRUD operations.

Methods


Creation methods

public int Add(TEntity entity)

Adds the specified entity to the repository. Returns 1 if the entity was added successfully, 0 otherwise.

ParameterDescription
entityThe entity to add.

public Task<int> AddAsync(TEntity entity, CancellationToken cancellationToken = default)

Adds the specified entity to the repository asynchronously. Returns 1 if the entity was added successfully, 0 otherwise.

ParameterDescription
entityThe entity to add.

public int AddMany(IEnumerable<TEntity> entities)

Adds the specified entities to the repository. Returns the number of entities added successfully. If an error occurs, 0 is returned.

ParameterDescription
entitiesThe entities to add.

Read methods

public Optional<TEntity> FindById(TKey id)

Returns the entity with the specified id, if any.

ParameterDescription
idThe primary key of the entity in the database.

public Task<Optional<TEntity>> FindByIdAsync(TKey id, CancellationToken cancellationToken = default)

Returns the entity with the specified id, if any.

ParameterDescription
idThe primary key of the entity in the database.

public Optional<TEntity> FindWhere(Expression<Func<TEntity, bool>> predicateFn)

Returns the entity that matches the specified predicate, if any.

ParameterDescription
predicateFnThe predicate function to filter the entities.

public Task<Optional<TEntity>> FindWhereAsync(Expression<Func<TEntity, bool>> predicateFn, CancellationToken cancellationToken = default)

Returns the entity that matches the specified predicate, if any.

ParameterDescription
predicateFnThe predicate function to filter the entities.

Update methods

public int Update(TEntity entity)

Updates the specified entity in the repository. Returns 1 if the entity was updated successfully, 0 otherwise.

ParameterDescription
entityThe entity to update

public int UpdateMany(IEnumerable<TEntity> entities)

Updates the specified entities in the repository. Returns the number of entities updated successfully. If an error occurs, 0 is returned.

ParameterDescription
entitiesThe entities to update

Deletion methods

public int DeleteById(TKey id)

Deletes the entity with the specified id from the repository. Returns 1 if the entity was deleted successfully, 0 otherwise.

ParameterDescription
idThe primary key of the entity in the database.

public Task<int> DeleteByIdAsync(TKey id, CancellationToken cancellationToken = default)

Deletes the entity with the specified id from the repository asynchronously. Returns 1 if the entity was deleted successfully, 0 otherwise.

ParameterDescription
idThe primary key of the entity in the database.

public int DeleteWhere(Expression<Func<TEntity, bool>> predicateFn)

Deletes the entities that match the specified predicate from the repository.

ParameterDescription
predicateFnThe predicate function to filter the entities.

public Task<int> DeleteWhereAsync(Expression<Func<TEntity, bool>> predicateFn, CancellationToken cancellationToken = default)

Deletes the entities that match the specified predicate from the repository asynchronously.

ParameterDescription
predicateFnThe predicate function to filter the entities.