asp.net mvc 中的 UnitOfWork 错误

Errors with UnitOfWork in asp.net mvc

我在我的 mvc5 项目中使用 UnitOfWork 模式。

我有一个带服务的 BLL 层。

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

但是当我尝试在 mvc 控制器中使用此服务时出现错误:"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);
        }
}

我没有无参数构造函数,但如何在控制器中使用我的无参数构造函数服务?

我将 DI 用于工作单元:

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

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

我已经解决了问题:

学生模块

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

"No parameterless constructor defined for this object."

此消息表示您忘记为 StudentService 注册依赖项。这就是为什么它忽略了构造函数

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

然后它开始寻找另一个构造函数,它是一个无参数构造函数。

我建议您创建接口 IStudentService

public class IStudentService

并使 StudentService 实现 IStudentService

public class StudentService: IStudentService

然后在你的服务模块中

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

此错误不是由 UnitOfWork 引起的。它的发生是因为你的控制器 StudentController 并检查该控制器的构造函数

public StudentController(StudentService service)

您的控制器需要 StudentService 个实例,并且您没有为该 StudentService class 注册任何依赖项。 Asp.Net MVC 控制器不需要参数构造函数,或者如果您有任何依赖关系,则需要在创建控制器之前解决。

解决方案是为 StudentService class 创建一个接口。

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

并更新您的 StudentService class 以实现该接口

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

更新您的 DI 注册码以注册此依赖项。

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

更新您的 StudentController 并使用 IStudentService 而不是 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);
        }
}