Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Last active December 15, 2015 22:19
Show Gist options
  • Save yemrekeskin/5331956 to your computer and use it in GitHub Desktop.
Save yemrekeskin/5331956 to your computer and use it in GitHub Desktop.
Using Repository Design Pattern
// Repository Pattern
public interface IRepository<TObject>
{
IQueryable<TObject> All();
IQueryable<TObject> Filter(Expression<Func<TObject, bool>> predicate);
IQueryable<TObject> Filter<Key>(Expression<Func<TObject, bool>> filter,
out int total, int index = 0, int size = 50);
bool Contains(Expression<Func<TObject, bool>> predicate);
TObject Find(params object[] keys);
TObject Find(Expression<Func<TObject, bool>> predicate);
TObject Create(TObject t);
int Delete(TObject t);
int Delete(Expression<Func<TObject, bool>> predicate);
int Update(TObject t);
int Count { get; }
}
public class BaseRepository<TObject>
:IRepository<TObject>
where TObject:class
{
protected DbContext _Context = null;
protected DbContext Context
{
get { return _Context ?? (_Context = ContextFactory.GetContext()); }
}
protected IContextFactory ContextFactory
{
get;
private set;
}
public BaseRepository(IContextFactory contextFactory)
{
this.ContextFactory = contextFactory;
}
public BaseRepository(IUnitOfWork unitOfWork)
{
}
protected DbSet<TObject> DbSet
{
get
{
return _Context.Set<TObject>();
}
}
#region Operational Methods
public virtual IQueryable<TObject> All()
{
return DbSet.AsQueryable();
}
public virtual IQueryable<TObject> Filter(Expression<Func<TObject, bool>> predicate)
{
return DbSet.Where(predicate).AsQueryable<TObject>();
}
public virtual IQueryable<TObject> Filter<Key>(Expression<Func<TObject, bool>> filter, out int total, int index = 0, int size = 50)
{
int skipCount = index * size;
var resetSet = filter != null ? DbSet.Where(filter).AsQueryable() : DbSet.AsQueryable();
resetSet = skipCount == 0 ? resetSet.Take(size) : resetSet.Skip(skipCount).Take(size);
total = resetSet.Count();
return resetSet.AsQueryable();
}
public virtual bool Contains(Expression<Func<TObject, bool>> predicate)
{
return DbSet.Count(predicate) > 0;
}
public virtual TObject Find(params object[] keys)
{
return DbSet.Find(keys);
}
public virtual TObject Find(Expression<Func<TObject, bool>> predicate)
{
return DbSet.FirstOrDefault(predicate);
}
public virtual TObject Create(TObject t)
{
var newEntry = DbSet.Add(t);
_Context.SaveChanges();
return newEntry;
}
public virtual int Delete(TObject t)
{
DbSet.Remove(t);
return _Context.SaveChanges();
}
public virtual int Delete(Expression<Func<TObject, bool>> predicate)
{
var objects = Filter(predicate);
foreach (var obj in objects)
DbSet.Remove(obj);
return _Context.SaveChanges();
}
public virtual int Update(TObject t)
{
var entry = _Context.Entry(t);
DbSet.Attach(t);
entry.State = EntityState.Modified;
return 0;
}
public virtual int Count
{
get { return DbSet.Count(); }
}
#endregion
public void Dispose()
{
if (this._Context != null)
this._Context.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment