【问题标题】:Errors with UnitOfWork in asp.net mvcasp.net mvc 中的 UnitOfWork 错误
【发布时间】:2018-05-01 08:29:46
【问题描述】:

我在我的 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 用于工作单元:

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

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

【问题讨论】:

  • 似乎是依赖注入声明的问题,你能检查一下吗?
  • 我将从将StusentService 重命名为StudentService 开始。
  • 您需要创建 StudentService 的接口,并在您的控制器中使用该接口并使用 DI 来解决 StudentService 的依赖关系,

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我已经解决了这个问题:

    学生模块

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

    【讨论】:

      【解决方案3】:

      此错误不是由 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);
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-20
        • 2021-07-29
        • 2016-06-26
        相关资源
        最近更新 更多