MVC pattern model logic

According to the MVC design pattern, if we create a user (working with a database) and we have to send an email with an activation code to the user, will this correspond to the model or controller after the model has created the database record?

+5
source share
3 answers

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
{
    // notice that it a view model and not a model
    public ActionResult Register(RegisterViewModel model)
    {
        UserService service;
        User user = service.Register(model.UserName);
        return View("Created");
    }
}

// this class is located in the "model"
public class UserService
{
   public User Register(string userName)
   {
       // another class in the "model"
       var repository = new UserRepository();
       var user = repository.Create(userName);

       // just another "model" class
       var emailService = new EmailService();
       emailService.SendActivationEmail(user.Email);

       return user;
   }
}
+8
source

MVC and MVC design templates are a combination of two layers:

  • Presentation level
  • Model level

, ( - ) . . , . , , .

- . , , , .

, , , , .

, -. ( data mappers, , ..).

TL; DR

​​ .

+2

- , .

( ) , ( ). EmailClient. .

0

All Articles