繁体   English   中英

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控制器中使用此服务时,出现错误:“没有为此对象定义无参数构造函数。”

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

“没有为此对象定义无参数构造函数。”

此消息表示您忘记注册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>();
        }
    }

我已经解决了这个问题:

学生模块

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

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

public StudentController(StudentService service)

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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