Why repository pattern is extensively used in entity framework as though it is complex?
asp.net-mvc, c#, entity-framework, nopcommerce, repository-pattern
Solution
To provide a code example of why Approach 1 is better consider a test scenario. With Approach 2, when you call the Insert method in a unit test you will actually be calling:
Controller.Insert > MyBal.Insert > Database execution.
This is not what we want in unit testing. In unit testing we just want to test the unit (code block) not the object graph of it's entire call stack.
This is because you have hardcoded a dependency on MyBal and there is no way to switch it out. If we used Approach 1 however we might have:
public class HomeController : Controller
{
private readonly IMyBalService _ibalService;
public HomeController(IMyBalService ibalService)
{
_ibalService = ibalService;
}
public ActionResult Insert(Model M)
{
ibalService.Insert();
}
}
Now you can have your MyBal class implement IBal interface:
public class MyBal : IBalService
{
public void Insert()
{
using (MyEntities context = new MyEntities ())
{
var result = context.MyTable.Add(_MyTable);
context.SaveChanges();
return result;
}
}
And under production scenario all would work as it currently does. BUT under test now you can also make an BalMockService.
public class BalMockService : IBalService
{
public void Insert() {}
}
And you can pass that into your HomeController when testing. Notice how we removed all the database calls? They are not needed for testing Controller.Insert. Alternatively we could have made BalMockService return some test data, but you have a void so it's not needed.
The point is we've decoupled HomeController from MyBal and we've also advertised the fact the HomeController requires an IBalService of some kind to run.
Problem
I am creating a demo project which contains crud operations using a repository pattern and dependency injection. This is my structure: Approach 1 (very popular, used by many developers) My repository interface: ``` public partial interface IRepository<T> { void Insert(T entity); } ``` My service layer: ``` public partial interface IEmployeeService { void InsertCategory(EmployeeMaster employeeMaster); } ``` My class which will implement that interface (service): ``` public partial class EmployeeService : IEmployeeService { private readonly IRepository<EmployeeMaster> _employeeMasterRepository; public EmployeeService(IRepository<EmployeeMaster> employeeMasterRepository) { this._employeeMasterRepository = employeeMasterRepository; } public virtual void InsertCategory(EmployeeMaster employeeMaster) { _employeeMasterRepository.Insert(employeeMaster); } ``` This is my controller: ``` public class HomeController : Controller { private readonly IEmployeeService _employeeService; public HomeController(IEmployeeService employeeService) { this._employeeService = employeeService; } ``` Above is the structure which I am following from Nop Commerce project (http://www.nopcommerce.com) and I think most developers now a days are following this structure only that is repository pattern and dependency injection. Previously I was doing like Below like making one Bal(business Access layer) project in my application and then making class file in Bal and doing insert like below: Approach 2: ``` public class MyBal() { public void Insert() { using (MyEntities context = new MyEntities ()) { var result = context.MyTable.Add(_MyTable); context.SaveChanges(); return result; } } ``` And then Calling this Method straight Away from My Mvc application like this: ``` [HttpPost] public ActionResult Insert(Model M) { MyBal bal = new MyBal (); bal.Insert(); } ``` So why most of the developers go on creating such a complex structure that is repository pattern.Can anybody explain me the difference between Approach 1 and Approach 2 ?? Does Approach 1 increases performance or it is just related to separation of code or better maintainability of code.