I use UnitOfWork pattern in my mvc5 project.
I have a BLL layer with services.
public class StudentService
{
private readonly IUnitOfWork _db;
public StudentService(IUnitOfWork uow)
{
_db = uow;
}
public IEnumerable<StudentView> GetStudentViews()
{
List<Student> students = _db.Students.GetAll().ToList();
return Mapper.Map<List<Student>, List<StudentView>>(students);
}
}
But when I'm trying to use this service in mvc controller I have an error: "No parameterless constructor defined for this object."
public class StudentController : Controller
{
private readonly StudentService _service;
public StudentController(StudentService service)
{
_service = service;
}
// GET: Student
public ActionResult Index()
{
IEnumerable<StudentView> studentViews = _service.GetStudentViews();
return View("Index", studentViews);
}
}
I have no parameterless constructor, but how can I use my service with parameterless constructor in controller?
I use DI for UnitOf Work:
public class ServiceModule : NinjectModule
{
private string connection;
public ServiceModule(string connection)
{
this.connection = connection;
}
public override void Load()
{
Bind<IUnitOfWork>().To<UnitOfWork>().WithConstructorArgument(connection);
}
}
StusentServicetoStudentService.