简体   繁体   English

ASP.NET MVC中的UnitOfWork错误

[英]Errors with UnitOfWork in asp.net mvc

I use UnitOfWork pattern in my mvc5 project. 我在mvc5项目中使用UnitOfWork模式。

I have a BLL layer with services. 我有一个带有服务的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);
        }
}

But when I'm trying to use this service in mvc controller I have an error: "No parameterless constructor defined for this object." 但是,当我尝试在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);
        }
}

I have no parameterless constructor, but how can I use my service with parameterless constructor in controller? 我没有无参数构造函数,但是如何在控制器中将我的服务与无参数构造函数一起使用?

I use DI for UnitOf Work: 我将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);
        }
    }

"No parameterless constructor defined for this object." “没有为此对象定义无参数构造函数。”

This message means that you forgot to register a dependency for StudentService. 此消息表示您忘记注册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 我建议您应该创建IStudentService接口

public class IStudentService

and make StudentService implements IStudentService 并使StudentService实现IStudentService

public class StudentService: IStudentService

Then in your ServiceModule 然后在您的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 : 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. 此错误不是由于UnitOfWork。 It's happening because of your controller StudentController and checks the constructor of that controller 由于您的控制器StudentController发生了这种情况,并检查了该控制器的构造函数

public StudentController(StudentService service)

Your controller requires StudentService instance and You doesn't register any dependence for that StudentService class. 您的控制器需要StudentService实例,并且您没有为该StudentService类注册任何依赖项。 Asp.Net MVC controller requires no parameter constructor or if you've any dependency then that needs to be resolved before controller creation. Asp.Net MVC控制器不需要参数构造函数,或者如果您有任何依赖关系,则需要在创建控制器之前解决该问题。

The solution is to make an Interface for StudentService class. 解决方案是为StudentService类创建一个Interface。

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

and update your StudentService class to implement that Interface 并更新您的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);
    }
}

Update your DI registration code to register this dependence. 更新您的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>();
        }
    }

Update your StudentController and use IStudentService instead of 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.

相关问题 将项目插入到UnitofWork存储库-不保存对象-ASP.Net MVC - Inserting items to UnitofWork Repository - objects not saving - ASP.Net MVC 标准化存储库,UnitOfWork,IOC容器Asp.Net MVC - Standardize Repository, UnitOfWork, IOC Container Asp.Net MVC ASP.NET MVC工作单元和业务服务/层 - ASP.NET MVC UnitOfWork and Business Services / Layer 嵌套的UnitOfWork在Asp.net应用程序中不起作用 - Nested UnitOfWork is not working in Asp.net application 在ASP.NET MVC 5中使用具有通用存储库模式UnitOfWork的继承接口的方法 - Using Method of Inherited Interface with Generic Repository Pattern, UnitOfWork in ASP.NET MVC 5 在我的asp.net MVC Web应用程序中获取通用存储库+子存储库+ UnitOfWork - Getting Generic Repository + Child repository + UnitOfWork inside my asp.net mvc web application asp.net MVC 3剃须刀和reCaptcha错误 - asp.net mvc 3 razor and reCaptcha errors ASP.NET MVC 验证错误未显示 - ASP.NET MVC Validation errors not displaying ASP.NET MVC自定义错误 - ASP.NET MVC Custom Errors 在 MVC5 ASP.Net 应用程序中使用 Repository 和 UnitOfWork 模式时,如何修改数据库初始值设定项 Seed 方法? - How do modify database initializer Seed method when using Repository and UnitOfWork patterns in MVC5 ASP.Net application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM