Last active
August 29, 2015 14:22
-
-
Save mesuttalebi/293ebcc08ba35acfd307 to your computer and use it in GitHub Desktop.
Generic Repository Interface and class (used in repository pattern)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GenericRepository<T> : IRepository<T> where T: class | |
{ | |
private readonly DbContext _context; | |
private IDbSet<T> dbSet; | |
public GenericRepository(DbContext context) | |
{ | |
_context = context; | |
} | |
protected virtual IDbSet<T> DbSet { | |
get { return dbSet ?? (dbSet = _context.Set<T>()); } | |
} | |
public virtual void Insert(T entity) | |
{ | |
try | |
{ | |
if (entity == null) | |
throw new ArgumentNullException("entity"); | |
this.DbSet.Add(entity); | |
//this._context.SaveChanges(); | |
} | |
catch (DbEntityValidationException dbEx) | |
{ | |
var fail = GenerateException(dbEx); | |
//Debug.WriteLine(fail.Message, fail); | |
throw fail; | |
} | |
} | |
public virtual void Delete(object id) | |
{ | |
var entityToDelete = DbSet.Find(id); | |
Delete(entityToDelete); | |
} | |
public virtual void Delete(T entity) | |
{ | |
try | |
{ | |
if (entity == null) | |
throw new ArgumentNullException("entity"); | |
this.DbSet.Remove(entity); | |
//this._context.SaveChanges(); | |
} | |
catch (DbEntityValidationException dbEx) | |
{ | |
var fail = GenerateException(dbEx); | |
//Debug.WriteLine(fail.Message, fail); | |
throw fail; | |
} | |
} | |
public virtual void Update(T entity) | |
{ | |
try | |
{ | |
if (entity == null) | |
throw new ArgumentNullException("entity"); | |
//this._context.SaveChanges(); | |
} | |
catch (DbEntityValidationException dbEx) | |
{ | |
var fail = GenerateException(dbEx); | |
//Debug.WriteLine(fail.Message, fail); | |
throw fail; | |
} | |
} | |
public void Save() | |
{ | |
try | |
{ | |
this._context.SaveChanges(); | |
} | |
catch (DbEntityValidationException dbEx) | |
{ | |
var fail = GenerateException(dbEx); | |
//Debug.WriteLine(fail.Message, fail); | |
throw fail; | |
} | |
} | |
public virtual IQueryable<T> GetAll | |
{ | |
get { return DbSet; } | |
} | |
public virtual IQueryable<T> GetAllNoTracking | |
{ | |
get { return DbSet.AsNoTracking(); } | |
} | |
public virtual IQueryable<T> GetAllIncluding(params Expression<Func<T, object>>[] includedProperties) | |
{ | |
var entities = DbSet.AsQueryable(); | |
foreach (var includedPropery in includedProperties) | |
{ | |
entities = entities.Include(includedPropery); | |
} | |
return entities; | |
} | |
public virtual IQueryable<T> GetAllIncluding(string includedProperties) | |
{ | |
var entities = DbSet.AsQueryable(); | |
var relations = includedProperties.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries); | |
foreach (var property in relations) | |
{ | |
entities = entities.Include(property); | |
} | |
return entities; | |
} | |
public virtual T GetById(object id) | |
{ | |
return this.DbSet.Find(id); | |
} | |
private static Exception GenerateException(DbEntityValidationException dbEx) | |
{ | |
var msg = string.Empty; | |
foreach (var validationErrors in dbEx.EntityValidationErrors) | |
foreach (var validationError in validationErrors.ValidationErrors) | |
msg += Environment.NewLine + | |
string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); | |
var fail = new Exception(msg, dbEx); | |
return fail; | |
} | |
private bool disposed = false; | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (!this.disposed) | |
{ | |
if (disposing) | |
{ | |
_context.Dispose(); | |
} | |
} | |
this.disposed = true; | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IRepository<T> : IDisposable where T : class | |
{ | |
/// <summary> | |
/// Insert entity | |
/// </summary> | |
/// <param name="entity">Entity</param> | |
void Insert(T entity); | |
/// <summary> | |
/// Delete entity | |
/// </summary> | |
/// <param name="id">Identifier</param> | |
void Delete(object id); | |
/// <summary> | |
/// Delete entity | |
/// </summary> | |
/// <param name="entity">Entity</param> | |
void Delete(T entity); | |
/// <summary> | |
/// Update entity | |
/// </summary> | |
/// <param name="entity">Entity</param> | |
void Update(T entity); | |
/// <summary> | |
/// Saves The Entity State To DB | |
/// </summary> | |
void Save(); | |
/// <summary> | |
/// Gets All Entities | |
/// </summary> | |
IQueryable<T> GetAll { get; } | |
/// <summary> | |
/// Gets All Entities with "no tracking" enabled (EF feature) Use it only when you load record(s) only for read-only operations | |
/// </summary> | |
IQueryable<T> GetAllNoTracking { get; } | |
/// <summary> | |
/// Gets All Entities Including Eager Loaded Relations | |
/// </summary> | |
/// <param name="includedProperties">Included Relations</param> | |
/// <returns>Entities with eager loaded selected relations</returns> | |
IQueryable<T> GetAllIncluding(params Expression<Func<T, object>>[] includedProperties); | |
/// <summary> | |
/// Get all Entities Including Eager loaded Relations also nested relations. | |
/// </summary> | |
/// <param name="includedProperties">Comma seperated table (relation) names to include in result set.</param> | |
/// <returns>Entities with Eager loaded selected relations</returns> | |
IQueryable<T> GetAllIncluding(string includedProperties); | |
/// <summary> | |
/// Get entity by identifier | |
/// </summary> | |
/// <param name="id">Identifier</param> | |
/// <returns>Entity</returns> | |
T GetById(object id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment