Created
March 20, 2016 19:02
-
-
Save cguldogan/b1ccfd51f41ac32a2e0f to your computer and use it in GitHub Desktop.
a generic repository for net entity framework 6 with async operations
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
using System; | |
using System.Collections.Generic; | |
using System.Data.Entity; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Threading.Tasks; | |
namespace BUSINESS | |
{ | |
//http://www.itworld.com/article/2700950/development/a-generic-repository-for--net-entity-framework-6-with-async-operations.html | |
public class BaseService<TObject> where TObject : class | |
{ | |
protected DbContext _context; | |
public BaseService() | |
{ | |
var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance; | |
this._context = new cglabsContext(); | |
this._context.Configuration.LazyLoadingEnabled = true; | |
} | |
public BaseService(DbContext context) | |
{ | |
_context = context; | |
} | |
public ICollection<TObject> GetAll() | |
{ | |
return _context.Set<TObject>().ToList(); | |
} | |
public async Task<ICollection<TObject>> GetAllAsync() | |
{ | |
return await _context.Set<TObject>().ToListAsync(); | |
} | |
public TObject Get(int id) | |
{ | |
return _context.Set<TObject>().Find(id); | |
} | |
public async Task<TObject> GetAsync(int id) | |
{ | |
return await _context.Set<TObject>().FindAsync(id); | |
} | |
public TObject Find(Expression<Func<TObject, bool>> match) | |
{ | |
return _context.Set<TObject>().SingleOrDefault(match); | |
} | |
public async Task<TObject> FindAsync(Expression<Func<TObject, bool>> match) | |
{ | |
return await _context.Set<TObject>().SingleOrDefaultAsync(match); | |
} | |
public ICollection<TObject> FindAll(Expression<Func<TObject, bool>> match) | |
{ | |
return _context.Set<TObject>().Where(match).ToList(); | |
} | |
public async Task<ICollection<TObject>> FindAllAsync(Expression<Func<TObject, bool>> match) | |
{ | |
return await _context.Set<TObject>().Where(match).ToListAsync(); | |
} | |
public TObject Add(TObject t) | |
{ | |
_context.Set<TObject>().Add(t); | |
_context.SaveChanges(); | |
return t; | |
} | |
public async Task<TObject> AddAsync(TObject t) | |
{ | |
_context.Set<TObject>().Add(t); | |
await _context.SaveChangesAsync(); | |
return t; | |
} | |
public TObject Update(TObject updated, int key) | |
{ | |
if (updated == null) | |
return null; | |
TObject existing = _context.Set<TObject>().Find(key); | |
if (existing != null) | |
{ | |
_context.Entry(existing).CurrentValues.SetValues(updated); | |
_context.SaveChanges(); | |
} | |
return existing; | |
} | |
public async Task<TObject> UpdateAsync(TObject updated, int key) | |
{ | |
if (updated == null) | |
return null; | |
TObject existing = await _context.Set<TObject>().FindAsync(key); | |
if (existing != null) | |
{ | |
_context.Entry(existing).CurrentValues.SetValues(updated); | |
await _context.SaveChangesAsync(); | |
} | |
return existing; | |
} | |
public void Delete(TObject t) | |
{ | |
_context.Set<TObject>().Remove(t); | |
_context.SaveChanges(); | |
} | |
public async Task<int> DeleteAsync(TObject t) | |
{ | |
_context.Set<TObject>().Remove(t); | |
return await _context.SaveChangesAsync(); | |
} | |
public int Count() | |
{ | |
return _context.Set<TObject>().Count(); | |
} | |
public async Task<int> CountAsync() | |
{ | |
return await _context.Set<TObject>().CountAsync(); | |
} | |
public virtual IQueryable<TObject> Query(Expression<Func<TObject, bool>> exp) | |
{ | |
return _context.Set<TObject>().AsQueryable<TObject>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment