GenericRepository

Namespace: Babel.Data Assembly: Babel.Data.dll

A generic repository.

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class

Type Parameters

NameDescription

TEntity

Type of the entity.

Inheritance

objectGenericRepository<TEntity>

Implements

IGenericRepository<TEntity>

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Constructors

GenericRepository(BabelDbContext)

Initializes a new instance of the GenericRepository class.

public GenericRepository(BabelDbContext context)

Parameters

NameDescription

The database context.

Properties

Context

Gets the Babel database context.

public BabelDbContext Context { get; }

Property Value

BabelDbContext

DbSet

Gets the set the database belongs to.

public DbSet<TEntity> DbSet { get; }

Property Value

DbSet<TEntity>

Methods

CountAsync(Expression<Func<TEntity, bool>>, CancellationToken)

Asynchronously returns the number of elements in the repository.

public virtual Task<int> CountAsync(Expression<Func<TEntity, bool>> filter = null, CancellationToken cancellationToken = default)

Parameters

NameDescription

(Optional) Specifies the filter.

(Optional) A token that allows processing to be cancelled.

Returns

NameDescription

An asynchronous result that yields the number of elements in the repository.

CountAsync(string, CancellationToken)

Asynchronously returns the number of elements in the repository.

public virtual Task<int> CountAsync(string filter, CancellationToken cancellationToken = default)

Parameters

NameDescription

Specifies the string filter.

(Optional) A token that allows processing to be cancelled.

Returns

NameDescription

An asynchronous result that yields the number of elements in the repository.

Delete(params object[])

Deletes an entity with the given primary key values. Begins tracking the given entity in the Detached state such that it will be deleted in the database when SaveChanges() is called.

public virtual void Delete(params object[] ids)

Parameters

NameDescription

The values of the primary key for the entity to be found.

Delete(TEntity)

Deletes the given entity from the repository. Begins tracking the given entity in the Detached state such that it will be deleted in the database when SaveChanges() is called.

public virtual void Delete(TEntity entityToDelete)

Parameters

NameDescription

entityToDelete TEntity

The entity to delete.

Get(Expression<Func<TEntity, bool>>, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>, string, string, int?, int?)

Gets a list of entities from this repository.

public virtual IList<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string columns = "", string includeProperties = "", int? skip = null, int? take = null)

Parameters

NameDescription

(Optional) Specifies the filter expression.

(Optional) Order by expression.

(Optional) A comma separated list of column names to retrieve.

(Optional) A comma separated list of nested entity names to retrieve.

(Optional) The number of records to skip before the first record found.

(Optional) The number of records to take after the first record found.

Returns

NameDescription

A list of entities.

GetAsync(Expression<Func<TEntity, bool>>, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>, string, string, int?, int?, CancellationToken)

Asynchronously gets a list of entities from this repository.

public virtual Task<List<TEntity>> GetAsync(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string columns = "", string includeProperties = "", int? skip = null, int? take = null, CancellationToken cancellationToken = default)

Parameters

NameDescription

(Optional) Specifies the filter expression.

(Optional) Order by expression.

(Optional) A comma separated list of column names to retrieve.

(Optional) A comma separated list of nested entity names to retrieve.

(Optional) The number of records to skip before the first record found.

(Optional) The number of records to take after the first record found.

(Optional) A token that allows processing to be cancelled.

Returns

NameDescription

A task that represents the asynchronous get operation.

GetById(params object[])

Gets an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found in the context or the store, then null is returned.

public virtual TEntity GetById(params object[] ids)

Parameters

NameDescription

The values of the primary key for the entity to be found.

Returns

GetByIdAsync(params object[])

Asynchronously gets an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found in the context or the store, then null is returned.

public virtual Task<TEntity> GetByIdAsync(params object[] ids)

Parameters

NameDescription

The values of the primary key for the entity to be found.

Returns

NameDescription

A task that represents the asynchronous get operation. The task result contains the entity found, or null.

GetByIdAsync(object[], CancellationToken)

Asynchronously gets an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found in the context or the store, then null is returned.

public virtual Task<TEntity> GetByIdAsync(object[] ids, CancellationToken cancellationToken = default)

Parameters

NameDescription

The values of the primary key for the entity to be found.

(Optional) A token that allows processing to be cancelled.

Returns

NameDescription

A task that represents the asynchronous get operation. The task result contains the entity found, or null.

Insert(TEntity)

Inserts the given entity in the repository. Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called

public virtual void Insert(TEntity entity)

Parameters

NameDescription

entity TEntity

The entity to be inserted.

InsertAsync(TEntity, CancellationToken)

Asynchronously inserts the given entity in the repository. Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called

public virtual Task<EntityEntry<TEntity>> InsertAsync(TEntity entity, CancellationToken cancellationToken = default)

Parameters

NameDescription

entity TEntity

The entity.

(Optional) A token that allows processing to be cancelled.

Returns

NameDescription

A task that represents the asynchronous Insert operation.

QueryAsync(string, string, string, string, int?, int?, CancellationToken)

Asynchronously query a list of entities from this repository.

public virtual Task<List<TEntity>> QueryAsync(string select = null, string filter = null, string orderBy = null, string includeProperties = "", int? skip = null, int? take = null, CancellationToken cancellationToken = default)

Parameters

NameDescription

(Optional) Specifies a select expression.

(Optional) Specifies the filter expression.

(Optional) Order by expression.

(Optional) A comma separated list of nested entity names to retrieve.

(Optional) The number of records to skip before the first record found.

(Optional) The number of records to take after the first record found.

(Optional) A token that allows processing to be cancelled.

Returns

NameDescription

A task that represents the asynchronous get operation.

Save()

Saves all changes made in this repository to the underlying database.

public virtual bool Save()

Returns

NameDescription

True if it succeeds, false if it fails.

SaveAsync(CancellationToken)

Asynchronously saves all changes made in this repository to the underlying database.

public virtual Task<bool> SaveAsync(CancellationToken cancellationToken = default)

Parameters

NameDescription

(Optional) A token that allows processing to be cancelled.

Returns

NameDescription

A task that represents the asynchronous save operation.

Update(TEntity)

Updates the given entity in the repository. Begins tracking the given entity in the Modified state such that it will be updated in the database when SaveChanges() is called.

public virtual void Update(TEntity entityToUpdate)

Parameters

NameDescription

entityToUpdate TEntity

The entity to update.

Last updated