1

I am using the Entity Framework/Repository-UnitOfWork/Service layer method on this ASP.NET MVC Application and it works great, but it seems a layer might be missing in order to keep the controllers thin.

Lets take for example a user authentication scenario:

1) The AuthenticationController takes a IAuthenticationService which in turn takes a IUnitOfWork and IRepository<User> (I am using generic repositories).

2) In the controller I want to make its only concern that the service authenticates the user:

if (userService.AuthenticateUser(model.userName, model.password)) {
    FormsAuthentication.SetCookie(...);
    return RedirectToAction(...);
}

return View(model);

Some will say this is too much logic in the controller right? So it seems as though we might need a Application Manager if you will:

if (appManager.AuthenticateUser(model.userName, model.password)) {
    // Here the app manager calls the service???
    return RedirectToAction(...);
}

I am trying to keep my domain services agnostic of the consuming application so I can use them on MVC, WinForms, Console, WPF, WCF, etc.

My service layers only return domain objects, I need a place to transform them into View Models, but I want to keep that out of the controllers.

Any input on this would be great!!

Sam
  • 15,336
  • 25
  • 85
  • 148
  • This might be a better fit for programmers.stackexchange.com, if you're just looking for "best practice" advice. You're definitely heading in the right direction, though. – Tieson T. Aug 26 '12 at 01:50

1 Answers1

2

Typically, one would use something like AutoMapper to map your domain objects to ViewModels. Then you only have a Map call that wraps your service layer. There is little reason to introduce an entirely new layer just for object mapping.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • I agree with using AutoMapper, I already am, but in this example setting the `FormsAuthentication` cookie is not the responsibility of the controller, or is it? Also what about a more complex situation where you are building up an aggregate model? – Sam Aug 26 '12 at 03:29
  • 3
    @SamStriano - You have to realize that the presentation layer is responsible for setting cookies and dealing with front-end authentication. Setting the cookie is part of the UI layer, not part of lower levels. – Erik Funkenbusch Aug 26 '12 at 03:37