Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC design pattern model logic

According to the MVC design pattern, if we create a user (database work) and we have to send a mail with an activation code to the user, would this fit in the model or in the controller, after the model created the database record?

like image 307
onlineracoon Avatar asked Aug 27 '12 12:08

onlineracoon


People also ask

What are the design patterns used in MVC?

Design Patterns - MVC Pattern. MVC Pattern stands for Model-View-Controller Pattern. This pattern is used to separate application's concerns. Model - Model represents an object or JAVA POJO carrying data. It can also have logic to update controller if its data changes. View - View represents the visualization of the data...

What is the use of model and view pattern?

This pattern is used to separate application's concerns. Model - Model represents an object or JAVA POJO carrying data. It can also have logic to update controller if its data changes. View - View represents the visualization of the data that model contains. Controller - Controller acts on both model and view.

What is diagram of interactions within the MVC pattern?

Diagram of interactions within the MVC pattern. Model–view–controller (usually known as MVC) is a software design pattern commonly used for developing user interfaces which divides the related program logic into three interconnected elements.

What is the difference between business logic and controller in MVC?

A1: Business Logic goes to Model part in MVC. Role of Model is to contain data and business logic. Controller on the other hand is responsible to receive user input and decide what to do. A2: A Business Rule is part of Business Logic. They have a has a relationship.


1 Answers

The MVC pattern is used to create an abstraction between the business logic (the model) and the GUI (the view). The controller is just an adapter (google adapter pattern) between those two blocks.

Hence the controller should only have code which is used to fetch the required information from the controller and adopt it so it fits the view. Any other logic should be in the model.

That only make sense if you understand that the model is not a single class but all of your business logic.

Example (implementation specific, but I hope that you understand):

public class UserController : Controller
{
    // notice that it's 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;
   }
}
like image 161
jgauffin Avatar answered Sep 27 '22 23:09

jgauffin