I’m creating a web shop using MVC4 razor syntax, repository and Unit of work. At least that is what I’m trying to do.
I still have some doubts about the usage of repository and Unit of work and I’m hoping I can get some clarity here.
I’ve found two good tutorials regarding it.
The first one:
http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle
Now, I’ve read both and I’m a bit confused.
In the first one, the GenericRepository class is abstract and has TC and T. TC being a DbContext object and T being a class. In the second tut, only a class is needed.
Why these different approaches?
Is it because the second tutorial also uses Unit Of Work? Is one better than the other or is Unit Of Work not really necessarily?
In my project I have 20 tables, does this mean I have to create 20 repositories if some of those are used to create a multi-language site?
Finally, Do these repositories belong in the Model folder or in a seperate one like “Data Access Layer”?
If it’s the later, Does anything ever belong on the Model folder using repositories?
Could someone please provide clarity?
P.S.: My own repository code looks like this and works (no Unit Of Work):
IGenericRepository
namespace ArtWebShop.Data_Access_Layer
{
public interface IGenericRepository<T> where T : class
{
IQueryable<T> GetAll();
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
void Save();
}
}
GenericRepository
public abstract class GenericRepository<TC, T> : IGenericRepository<T>
where T : class
where TC : DbContext, new()
{
private TC _entities = new TC();
public TC Context
{
get { return _entities; }
set { _entities = value; }
}
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = _entities.Set<T>();
return query;
}
...
IMenuRepository
public interface IMenuRepository : IGenericRepository<menu_items_local>
{
List<menu_items_local> GetMenuItemsByLanguage(string language);
}
MenuRepository
public class MenuRepository : GenericRepository<ArtWebshopEntities, menu_items_local>, IMenuRepository
{
public List<menu_items_local> GetMenuItemsByLanguage(string language)
{
var menuItemsLocal = Context.menu_items_local.Where(m => m.cultures.name == language);
if (!menuItemsLocal.Any()) throw new HttpException(404, "menu items not found");
return menuItemsLocal.ToList();
}
}
CommonController
public class CommonController : Controller
{
private readonly IMenuRepository _menuRepository;
public CommonController()
{
this._menuRepository = new MenuRepository();
}
public CommonController(IMenuRepository menuRepository)
{
_menuRepository = menuRepository;
}
[HandleError]
[ChildActionOnly]
public ActionResult MenuItems(string language)
{
return PartialView("_menuPartial", _menuRepository.GetMenuItemsByLanguage(language));
}
}
After analyzing the two approaches I came to the conclusion that the major difference is that in case of the second tutorial where Unit Of Work is being used, the entity context is only declared once whereas in the first tutorial the context entity is mentioned in every repository.
Since the first tutorial doesn’t use Unit Of Work the different repositories aren’t stored in one place.
In conclusion I can state that the second tutorial is the better approach.