简体   繁体   中英

Errors with UnitOfWork in asp.net mvc

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);
        }
    }

"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>();
        }
    }

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));
        }
    }

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);
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM