GenericRepository
Namespace: Babel.Data Assembly: Babel.Data.dll
A generic repository.
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : classType Parameters
TEntity
Type of the entity.
Inheritance
object ← GenericRepository<TEntity>
Implements
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
context BabelDbContext
The database context.
Properties
Context
Gets the Babel database context.
public BabelDbContext Context { get; }Property Value
DbSet
Gets the set the database belongs to.
public DbSet<TEntity> DbSet { get; }Property Value
DbSet<TEntity>
NoTracking
Whether to disable entity tracking
public bool NoTracking { get; set; }Property Value
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
filter Expression<Func<TEntity, bool>>
(Optional) Specifies the filter.
cancellationToken CancellationToken
(Optional) A token that allows processing to be cancelled.
Returns
CountAsync(string, CancellationToken)
Asynchronously returns the number of elements in the repository.
public virtual Task<int> CountAsync(string filter, CancellationToken cancellationToken = default)Parameters
filter string
Specifies the string filter.
cancellationToken CancellationToken
(Optional) A token that allows processing to be cancelled.
Returns
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
ids object[]
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
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
filter Expression<Func<TEntity, bool>>
(Optional) Specifies the filter expression.
orderBy Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>
(Optional) Order by expression.
columns string
(Optional) A comma separated list of column names to retrieve.
includeProperties string
(Optional) A comma separated list of nested entity names to retrieve.
skip int?
(Optional) The number of records to skip before the first record found.
take int?
(Optional) The number of records to take after the first record found.
Returns
IList<TEntity>
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
filter Expression<Func<TEntity, bool>>
(Optional) Specifies the filter expression.
orderBy Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>
(Optional) Order by expression.
columns string
(Optional) A comma separated list of column names to retrieve.
includeProperties string
(Optional) A comma separated list of nested entity names to retrieve.
skip int?
(Optional) The number of records to skip before the first record found.
take int?
(Optional) The number of records to take after the first record found.
cancellationToken CancellationToken
(Optional) A token that allows processing to be cancelled.
Returns
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
ids object[]
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
ids object[]
The values of the primary key for the entity to be found.
Returns
Task<TEntity>
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
ids object[]
The values of the primary key for the entity to be found.
cancellationToken CancellationToken
(Optional) A token that allows processing to be cancelled.
Returns
Task<TEntity>
A task that represents the asynchronous get operation. The task result contains the entity found, or null.
GroupByAsync(string, string, int?, int?, CancellationToken)
Asynchronously groups a list of entities from this repository.
public virtual Task<List<GroupItem>> GroupByAsync(string filter = null, string groupBy = null, int? skip = null, int? take = null, CancellationToken cancellationToken = default)Parameters
filter string
(Optional) Specifies the filter expression.
groupBy string
(Optional) Group by expression.
skip int?
(Optional) The number of records to skip before the first record found.
take int?
(Optional) The number of records to take after the first record found.
cancellationToken CancellationToken
(Optional) A token that allows processing to be cancelled.
Returns
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
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
entity TEntity
The entity.
cancellationToken CancellationToken
(Optional) A token that allows processing to be cancelled.
Returns
Task<EntityEntry<TEntity>>
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
select string
(Optional) Specifies a select expression.
filter string
(Optional) Specifies the filter expression.
orderBy string
(Optional) Order by expression.
includeProperties string
(Optional) A comma separated list of nested entity names to retrieve.
skip int?
(Optional) The number of records to skip before the first record found.
take int?
(Optional) The number of records to take after the first record found.
cancellationToken CancellationToken
(Optional) A token that allows processing to be cancelled.
Returns
Save()
Saves all changes made in this repository to the underlying database.
public virtual bool Save()Returns
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
cancellationToken CancellationToken
(Optional) A token that allows processing to be cancelled.
Returns
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
entityToUpdate TEntity
The entity to update.
Last updated