0

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);
        }
    }
3
  • Seems to be issue with Dependency Injection declaration, can you check that? Commented May 1, 2018 at 8:31
  • I will start with renaming StusentService to StudentService. Commented May 1, 2018 at 8:32
  • You need to create Interface of StudentService and In your controller use that Interface and Using DI to resolve the dependency of StudentService, Commented May 1, 2018 at 8:43

3 Answers 3

5

"No parameterless constructor defined for this object."

This message means that you forgot to register a dependency for StudentService. That's why it ignored the constructor

public StudentController(StudentService service)
        {
            _service = service;
        }

and then it started to look for another constructor which is a parameterless constructor.

What I suggest is that you should create interface IStudentService

public class IStudentService

and make StudentService implements IStudentService

public class StudentService: IStudentService

Then in your ServiceModule

public class ServiceModule : NinjectModule
    {
        private string connection;
        public ServiceModule(string connection)
        {
            this.connection = connection;
        }

        public override void Load()
        {
            Bind<IUnitOfWork>().To<UnitOfWork>().WithConstructorArgument(connection);
            Bind<IStudentService>().To<StudentService>();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

I've resolved the problem:

StudentModule

public class StudentModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IStudentService>().To<StudentService>();
        }
    }
}

Global.asax :

 public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
           ///

            //dependencies
            NinjectModule serviceModule = new ServiceModule("connection");
            NinjectModule studentModule = new StudentModule();
            var kernel = new StandardKernel(studentModule, serviceModule);
            DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
        }
    }

Comments

0

This error is not due to UnitOfWork. It's happening because of your controller StudentController and checks the constructor of that controller

public StudentController(StudentService service)

Your controller requires StudentService instance and You doesn't register any dependence for that StudentService class. Asp.Net MVC controller requires no parameter constructor or if you've any dependency then that needs to be resolved before controller creation.

The solution is to make an Interface for StudentService class.

public interface IStudentService
{
     IEnumerable<StudentView> GetStudentViews();
}

and update your StudentService class to implement that Interface

public class StudentService : IStudentService
{
    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);
    }
}

Update your DI registration code to register this dependence.

public class ServiceModule : NinjectModule
    {
        private string connection;
        public ServiceModule(string connection)
        {
            this.connection = connection;
        }

        public override void Load()
        {
            Bind<IUnitOfWork>().To<UnitOfWork>().WithConstructorArgument(connection);
            Bind<IStudentService>().To<StudentService>();
        }
    }

Update your StudentController and use IStudentService instead of StudentService.

public class StudentController : Controller
    {
        private readonly IStudentService _service;

        public StudentController(IStudentService service)
        {
            _service = service;
        }

        // GET: Student
        public ActionResult Index()
        {
            IEnumerable<StudentView> studentViews = _service.GetStudentViews();
            return View("Index", studentViews);
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.