Say I have RegisterModel for user registration and some UserService that implementing IUserService
假设我有用于用户注册的RegisterModel和一些实现IUserService的UserService
public interface IUserService
{
User CreateUser(User newUser);
}
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// ... logic for newuser
User user = _userService.CreateUser(newuser);
_authenticationService.SetAuthenticatedUser(user);
return RedirectToRoute("Homepage");
}
return View(model);
}
Given that RegisterModel might be very complex, where is the logic should go for mapping RegisterModel to User object
鉴于RegisterModel可能非常复杂,逻辑应该用于将RegisterModel映射到User对象
1 个解决方案
#1
14
You never pass a view model to a service. A service doesn't even know about the existence of a view model that you might have defined in your GUI (ASP.NET MVC) tier. A service works with domain models. Personally I use AutoMapper to map between view models and models and vice versa, so this logic goes into the mapping layer.
您永远不会将视图模型传递给服务。服务甚至不知道您可能已在GUI(ASP.NET MVC)层中定义的视图模型的存在。服务适用于域模型。我个人使用AutoMapper在视图模型和模型之间进行映射,反之亦然,因此这个逻辑进入映射层。
#1
14
You never pass a view model to a service. A service doesn't even know about the existence of a view model that you might have defined in your GUI (ASP.NET MVC) tier. A service works with domain models. Personally I use AutoMapper to map between view models and models and vice versa, so this logic goes into the mapping layer.
您永远不会将视图模型传递给服务。服务甚至不知道您可能已在GUI(ASP.NET MVC)层中定义的视图模型的存在。服务适用于域模型。我个人使用AutoMapper在视图模型和模型之间进行映射,反之亦然,因此这个逻辑进入映射层。