Last active
January 19, 2017 22:12
-
-
Save alikrc/7fee456c45126c38ad84ac79072a6ca6 to your computer and use it in GitHub Desktop.
BaseRepository class implementation
This file contains hidden or 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 abstract class BaseRepository<T> where T : class | |
| { | |
| private DbContext _context; | |
| protected DbContext Context | |
| { | |
| get { return _context ?? (_context = DatabaseFactory.Get()); } | |
| } | |
| private readonly IDbSet<T> _dbSet; | |
| protected IDatabaseFactory DatabaseFactory { get; private set; } | |
| //ctor | |
| protected BaseRepository() | |
| { | |
| this.DatabaseFactory = new DatabaseFactory(); | |
| this._dbSet = Context.Set<T>(); | |
| } | |
| protected BaseRepository(IDatabaseFactory dbFactory) | |
| { | |
| this.DatabaseFactory = dbFactory; | |
| this._dbSet = Context.Set<T>(); | |
| } | |
| public virtual int Count() | |
| { | |
| return _dbSet.Count(); | |
| } | |
| public virtual bool Any(Expression<Func<T, bool>> predicate) | |
| { | |
| return _dbSet.Any(predicate); | |
| } | |
| public virtual void Add(T entity) | |
| { | |
| _dbSet.Add(entity); | |
| Context.Entry(entity).State = EntityState.Added; | |
| } | |
| public virtual void Update(T entity) | |
| { | |
| _dbSet.Attach(entity); | |
| Context.Entry(entity).State = EntityState.Modified; | |
| } | |
| public virtual void Delete(T entity) | |
| { | |
| _dbSet.Remove(entity); | |
| Context.Entry(entity).State = EntityState.Deleted; | |
| } | |
| public virtual void Delete(Expression<Func<T, bool>> where) | |
| { | |
| IEnumerable<T> objects = _dbSet.Where(where).AsEnumerable(); | |
| foreach (T o in objects) | |
| { | |
| Delete(o); | |
| } | |
| } | |
| public virtual bool SaveChanges() | |
| { | |
| return Context.SaveChanges() > 0; | |
| } | |
| public virtual T GetById(int id) | |
| { | |
| //if (_dbSet.IsDeletedRecord()) | |
| //{ | |
| // return null; | |
| //} | |
| var o = _dbSet.Find(id); | |
| return o; | |
| } | |
| public virtual T GetById(string id) | |
| { | |
| //if (_dbSet.IsDeletedRecord()) | |
| //{ | |
| // return null; | |
| //} | |
| return _dbSet.Find(id); | |
| } | |
| public virtual T Get(Expression<Func<T, bool>> where) | |
| { | |
| //if (_dbSet.IsDeletedRecord()) | |
| //{ | |
| // return null; | |
| //} | |
| return _dbSet.Where(where).FirstOrDefault<T>(); | |
| } | |
| public virtual TSelect Get<TSelect>(Expression<Func<T, bool>> where, Expression<Func<T, TSelect>> select) | |
| { | |
| return _dbSet.Where(where).Select(select).FirstOrDefault(); | |
| } | |
| public virtual List<T> GetList() | |
| { | |
| return _dbSet.ToList(); | |
| } | |
| public virtual List<T> GetList(Expression<Func<T, bool>> where) | |
| { | |
| return _dbSet.Where(where).ToList(); | |
| } | |
| public virtual List<TSelect> GetList<TSelect>(Expression<Func<T, bool>> where, Expression<Func<T, TSelect>> select) | |
| { | |
| return _dbSet.Where(where).Select(select).ToList(); | |
| } | |
| public virtual List<T> GetList<TKey>(Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, Expression<Func<T, TKey>> thenBy) | |
| { | |
| return _dbSet.Where(where).OrderBy(order).ThenBy(thenBy).ToList(); | |
| } | |
| public virtual List<T> GetListOrderBy<TKey>(Expression<Func<T, TKey>> order) | |
| { | |
| return _dbSet.OrderBy(order).ToList(); | |
| } | |
| public virtual List<T> GetListOrderBy<TKey>(Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order) | |
| { | |
| var query = _dbSet.Where(where).AsQueryable(); | |
| var count = query.Count(); | |
| if (count > 0) | |
| { | |
| return query.OrderBy(order).ToList(); | |
| } | |
| return new List<T>(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment