Skip to content

Instantly share code, notes, and snippets.

@Alexei000
Last active March 28, 2018 14:58
Show Gist options
  • Save Alexei000/341a151376f863c0f3bb56ccb3497381 to your computer and use it in GitHub Desktop.
Save Alexei000/341a151376f863c0f3bb56ccb3497381 to your computer and use it in GitHub Desktop.
Generic repository interface
public interface IRepository<T> : IRepository
where T : BaseModel, new()
{
/// <summary>
/// specifies if current repository is cached
/// </summary>
bool IsCached { get; }
/// <summary>
/// gets all non-tracked items as IQueryable reference
/// </summary>
IQueryable<T> AllNoTracking { get; }
/// <summary>
/// gets all tracked items as IQueryable reference
/// </summary>
IQueryable<T> All { get; }
/// <summary>
/// gets a tracked entity based on identifier
/// </summary>
/// <param name="id">identifier</param>
/// <returns>a tracked entity</returns>
T Get(int id);
/// <summary>
/// gets a non-tracked item based on identifier
/// </summary>
/// <param name="id">identifier</param>
/// <returns>a non-tracked entity</returns>
T GetNoTracking(int id);
/// <summary>
/// gets property value or default for the entity having a specified identifier
/// </summary>
/// <typeparam name="TProp">property type</typeparam>
/// <param name="id">entity identifier</param>
/// <param name="expr">entity property expression</param>
/// <returns></returns>
TProp GetPropertyValueOrDefault<TProp>(int id, Expression<Func<T, TProp>> expr);
/// <summary>
/// marks entity for insertion on next save
/// </summary>
/// <param name="entity"></param>
void Insert(T entity);
/// <summary>
/// bulk inserts a list of entities. Insert is performed in its own transaction, so a Transaction scope is required when other queries make the atomic save
/// </summary>
/// <param name="entities">list of entities to be inserted</param>
void BulkInsert(IEnumerable<T> entities);
/// <summary>
/// marks entity for removal on next save
/// </summary>
/// <param name="entity"></param>
void Delete(T entity);
/// <summary>
/// directly deletes a batch of entities
/// </summary>
/// <param name="entities">entities query</param>
void DeleteBatch(IQueryable<T> entities);
// other methods removed for brevity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment