using Application.DAL.Contracts; using Application.Model; using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Linq.Expressions; namespace Application.DAL.Implementations { public abstract class Repository<T> : IRepository<T> where T : class { private AdventureWorksEntities dataContext; private readonly IDbSet<T> dbset; protected Repository(IDatabaseFactory databaseFactory) { DatabaseFactory = databaseFactory; dbset = DataContext.Set<T>(); } protected IDatabaseFactory DatabaseFactory { get; private set; } protected AdventureWorksEntities DataContext { get { return dataContext ?? (dataContext = DatabaseFactory.Get()); } } public virtual void Add(T entity) { dbset.Add(entity); } public virtual void Update(T entity) { dbset.Attach(entity); dataContext.Entry(entity).State = EntityState.Modified; } public virtual void Delete(T entity) { dbset.Remove(entity); } public virtual void Delete(Expression<Func<T, bool>> where) { IEnumerable<T> objects = dbset.Where<T>(where).AsEnumerable(); foreach (T obj in objects) dbset.Remove(obj); } public virtual T GetById(long id) { return dbset.Find(id); } public virtual T GetById(string id) { return dbset.Find(id); } public virtual IEnumerable<T> GetAll() { return dbset.ToList(); } public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where) { return dbset.Where(where).ToList(); } public T Get(Expression<Func<T, bool>> where) { return dbset.Where(where).FirstOrDefault<T>(); } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)