开发者

How to pass complex ViewModel to Service Layer in ASP.NET MVC?

Say I have RegisterModel for user registration and some UserService that implementing IUserService

public interface IUserService
{
   User CreateUser(User newUser);
}


[HttpPost]
public ActionResult Register(RegisterModel model)
{
            if (ModelState.IsValid)
            {

                // ... logic for newuser

开发者_如何学Go                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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜