I am trying to implement dependency injection in the ASP.NET (MVC) template program.
Program Structure:
I have the following class/interfaces in the Model
folder:
Employee.cs
namespace TestWebApplication.Models
{
public class Employee
{
public int EmployeeID { get; set; }
public string FullName { get; set; }
}
}
IEmployeeRepository.cs
namespace TestWebApplication.Models
{
public interface IEmployeeRepository
{
Employee GetEmployee(int employeeId);
}
}
EmployeeRepository.cs
namespace TestWebApplication.Models
{
public class EmployeeRepository : IEmployeeRepository
{
private List<Employee> _employeeList;
public EmployeeRepository()
{
_employeeList = new List<Employee>()
{
new Employee() {EmployeeID = 1, FullName = "ABC"},
new Employee() {EmployeeID = 2, FullName = "XYZ"},
};
}
public Employee GetEmployee(int employeeId)
{
return _employeeList.First();
}
}
}
and now, when I try to do the dependency injection in the HomeController and run the web application, it throws exception.
HomeController .cs
namespace TestWebApplication.Controllers
{
public class HomeController : Controller
{
IEmployeeRepository _employeeList;
public HomeController(IEmployeeRepository repo)
{
_employeeList = repo;
}
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public ActionResult Basics()
{
ViewBag.Message = "Your Basics page.";
return View();
}
}
}
Exception:
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean & canBeCached, RuntimeMethodHandleInternal & ctor, Boolean & bNeedSecurityCheck) + 0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark & stackMark) + 122
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark & stackMark) + 239
System.Activator.CreateInstance(Type type, Boolean nonPublic) + 85
System.Activator.CreateInstance(Type type) + 12
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) + 55
[InvalidOperationException: An error occurred when trying to create a controller of type 'TestWebApplication.Controllers.HomeController'.Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) + 178
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) + 80
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) + 102
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController & controller, IControllerFactory & factory) + 188
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) + 50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) + 48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) + 16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 105
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) + 50
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean & completedSynchronously) + 163
QUESTION: How do I register the EmployeeRepository
?