[英]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.