UPDATE 3:
I see this video and how the author emphasize to use Repository/UOW...as oppose to what i discouraging. btw author is using ORM(EF)
http://pluralsight.com/training/Courses/TableOfContents/spa
UPDATE 2:
As I was playing with the Repository and I have this scanrio to tackle and I'm not sure if I'm following the right direction... So in my controller:
public class PersonsController : Controller
{
GenericRepository<Person> _genericRepository = new GenericRepository<Person>(new PersonsContext());
public ActionResult Index()
{
GenericRepository<Actors> _genericActorRepository = new GenericRepository<Actors>(new PersonsContext());
IEnumerable<Actors> _actorList = _genericActorRepository.GetAll();
//IList<Actors> _actorList1 = _genericActorRepository.GetAll().ToList();
ViewBag.ActorList = new SelectList(_actorList);
return View(_genericRepository.GetAll());
}
}
UPDATE:
Here is the link of Microsoft Developer Network talks about GenericRepository!
I am trying to implement best practices during the design phase of a system. I am going to be using Entity Framework, ASP.NET MVC 5 C#, and the generic repository/unit of work pattern (hopefully).
My question: how do I introduce Unit Of Work in my GenericRepository?
Here is my GenericRepository class:
public interface IGenericRepository<TEntity> : IDisposable
{
Task<TEntity> GetByIdAsync(int id);
IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate);
IQueryable<TEntity> GetAll();
Task EditAsync(TEntity entity);
Task InsertAsync(TEntity entity);
Task DeleteAsync(TEntity entity);
}
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
protected DbSet<TEntity> _dbSet;
private readonly DbContext _dbContext;
public GenericRepository(DbContext dbContext)
{
_dbContext = dbContext;
_dbSet = _dbContext.Set<TEntity>();
}
public GenericRepository() {}
public IQueryable<TEntity> GetAll()
{
return _dbSet;
}
public async Task<TEntity> GetByIdAsync(int id)
{
return await _dbSet.FindAsync(id);
}
public IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
{
return _dbSet.Where(predicate);
}
public async Task EditAsync(TEntity entity)
{
_dbContext.Entry(entity).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
}
public async Task InsertAsync(TEntity entity)
{
_dbSet.Add(entity);
await _dbContext.SaveChangesAsync();
}
public async Task DeleteAsync(TEntity entity)
{
//if (context.Entry(entityToDelete).State == EntityState.Detached)
//{
// dbSet.Attach(entityToDelete);
//}
_dbSet.Remove(entity);
await _dbContext.SaveChangesAsync();
}
public void Dispose(bool disposing)
{
if (_dbContext != null)
{
_dbContext.Dispose();
}
GC.SuppressFinalize(this);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Model class:
public class Person
{
public int Id { get; set; }
public String Fullname { get; set; }
public String Profession { get; set; }
public int Age { get; set; }
}
Context:
public class PersonsContext : DbContext
{
public PersonsContext() : base("name=PersonsContext")
{
}
public DbSet<Person> People { get; set; }
}
Controller:
public class PersonsController : Controller
{
GenericRepository<Person> _genericRepository = new GenericRepository<Person>(new PersonsContext());
//
// GET: /Persons/
public ActionResult Index()
{
return View(_genericRepository.GetAll());
}
//
// GET: /Persons/Details/5
public async Task<ActionResult> Details(Int32 id)
{
Person person = await _genericRepository.GetByIdAsync(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
//
// GET: /Persons/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Persons/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(Person person)
{
if (ModelState.IsValid)
{
await _genericRepository.InsertAsync(person);
return RedirectToAction("Index");
}
return View(person);
}
//
// GET: /Persons/Edit/5
public async Task<ActionResult> Edit(Int32 id)
{
Person person = await _genericRepository.GetByIdAsync(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
//
// POST: /Persons/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(Person person)
{
if (ModelState.IsValid)
{
await _genericRepository.EditAsync(person);
return RedirectToAction("Index");
}
return View(person);
}
}