asp.net mvc中UnitOfWork的错误

时间:2018-05-01 08:29:47

标签: c# asp.net asp.net-mvc unit-of-work

我在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控制器中使用此服务时,我有一个错误:&#34;没有为此对象定义无参数构造函数。&#34;

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进行UnitOf工作:

 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 个答案:

答案 0 :(得分:5)

&#34;没有为此对象定义无参数构造函数。&#34;

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

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

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

我建议你应该创建接口IStudentService

public class IStudentService

并使StudentService实现IStudentService

public class StudentService: IStudentService

然后在你的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>();
        }
    }

答案 1 :(得分:0)

我已经解决了这个问题:

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

答案 2 :(得分:0)

此错误不是由UnitOfWork引起的。由于您的控制器StudentController而发生这种情况并检查该控制器的构造函数

public StudentController(StudentService service)

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

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

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

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

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