Well, will your controllers get code that translates POCOs from the Entity Framework into separate view model objects?
If so, then you should move this code to a separate class and follow the principle of single responsibility. Regardless of whether this class is in the "service layer" or not, it is up to you. And whether you use AutoMapper or not is up to you. But these types of data mappers should not be part of the controller logic; controllers should be as deep as possible.
OK, now let it ignore the problem of data matching and pretend that you can always use your POCO directly as view models. Then you still need a service level, because it will broadcast between
userService.GetByUserName("bob")
in your dumb controller and implement this in a certain way by returning
userRepository.Users.Single(u => u.UserName == "bob")
Combining them, your
UserController ends up accepting
IUserService and
IUserDataMapper , and the code is super-dumb if required:
public ActionResult ShowUserPage(string userName) { var user = userService.GetByUserName(userName); var viewModel = userDataMapper.MakeViewModel(user); return View(viewModel); }
Now you can test the controller using stubs for both dependencies or cut out IUserDataMapper while you mock IUserService , or vice versa. Your controller has very little logic, and has only one axis of change . The same can be said for the user-mapper class and the user service class.
This morning I read an article about how you can highlight these architectural issues a bit. This is somewhat condescending, titled " Fundamentals of Software Development, Part 2: Layered Architecture ." You probably wonβt be able to switch from a database application model to a permanent uninformed model, which is discussed in the article. But that may point you in the right direction.
Domenic
source share