The MVC pattern is used to create an abstraction between the business logic (model) and the graphical interface (presentation). A controller is just an adapter (Google adapter template) between these two blocks.
Therefore, the controller should only have code that is used to extract the necessary information from the controller and accept it so that it matches the presentation. Any other logic should be in the model.
This makes sense if you understand that the model is not one class, but all your business logic.
Example (concrete implementation, but I hope you understand):
public class UserController : Controller
{
public ActionResult Register(RegisterViewModel model)
{
UserService service;
User user = service.Register(model.UserName);
return View("Created");
}
}
public class UserService
{
public User Register(string userName)
{
var repository = new UserRepository();
var user = repository.Create(userName);
var emailService = new EmailService();
emailService.SendActivationEmail(user.Email);
return user;
}
}
source
share