using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; using Church.Net.DAL.Core; namespace Church.Net.DAL.EF { public class RepositoryBase : IRepository where T : class { private ChurchNetContext db = null; private DbSet dbSet = null; public void Dispose() { db?.Dispose(); } public RepositoryBase(string connString) { InitializeConnectionString(connString); } public void InitializeConnectionString(string connString) { db = new ChurchNetContext(connString); foreach (PropertyInfo propertyInfo in db.GetType().GetProperties()) { if (propertyInfo.PropertyType == typeof(DbSet)) { dbSet = (DbSet) propertyInfo.GetValue(db, null); } } } public void Add(T obj) { dbSet.Add(obj); db.SaveChanges(); } public void Edit(T obj) { dbSet.Attach(obj); db.SaveChanges(); } public void Delete(T obj) { dbSet.Remove(obj); db.SaveChanges(); } public void Attach(T obj) { throw new NotImplementedException(); } public T GetSingle() { return dbSet.First(); } public T GetSingle(Expression> whereCondition) { return dbSet.Where(whereCondition).FirstOrDefault(); } public IList GetAll() { return dbSet.ToList(); } public IList GetAll(Expression> whereCondition) { return dbSet.Where(whereCondition).ToList(); } public IQueryable GetQueryable() { return dbSet; } public IQueryable GetQueryable(Expression> whereCondition) { return dbSet.Where(whereCondition); } } }