anshul anshul - 1 year ago 195
C# Question

ASP.NET MVC 5 application - Repository pattern without Entity Framework

I am trying my hands on creating an ASP.NET MVC 5 app without entity framework.
I have some existing database, but do not want to use Entity Framework for that. Came up with simple and uncluttered architecture having Entities, Repository and DAL.
I have created a controller passing Repository context to it.

public class EmployeeController : Controller
private readonly IEmployeeRespository repository;

public EmployeeController(IEmployeeRespository _repository)
repository = _repository;

// GET: Employee
public ActionResult Index()
IEnumerable<Employee> Employees = repository.GetEmployees();
return View(Employees);

Issue here is, I have not created a parameterless contructor for this controller. Now how do I pass my repository context to this controller. I am missing out some step, but not able to figure out.

Also, if anyone know of any downloadable sample application for such scenario, it will be of great help.

Answer Source

Dependency injection is your answer. there are some libraries that will do it for you. You can also do poor-mans injection yourself, or with a service locator.

You can use autofac or ninject that will orchestrate your dependency resolution.

This would help: How do I properly register AutoFac in a basic MVC5.1 website?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download