Question

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?

Was it helpful?

Solution

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;
   }
}

OTHER TIPS

MVC and MVC-inspired design patterns are combination of two layers:

  • Presentation layer
  • Model layer

Presentation layer is made up from views, controllers and (mostly in web-oriented solutions) templates. This layer deals with user interaction. It recognizes user input, produces responses and governs other aspect of user interface. The controllers, based on user interaction, change the state of model layer.

The model layer deals with domain business rules and interacts with different forms of storage. Model layer, just like presentation layer, is no any single object or class, but a group of structures with different responsibilities.

In this case, it would make sense for the service, which deals with user management, to use the different structures, that would both send the verification email, create an account and store this newly created user.

Services in model layer act like the barrier, that isolated the presentation layer from business logic. They deal with interaction between domain objects and storage abstractions (data mappers, repositories, units of work, etc.).

TL;DR

Email, with activation code for the newly created user, should be sent from model layer.

The controller is an object which simplifies and delegates messages to the model objects.

What you will have is an Interface object (or boundary object) within your model that represents the link between two systems (your system and email). class EmailClient. Your model objects will collaborate with this object when required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top