MVC stands for Model-View-Controller. Model-View-Controller (MVC) model in Salesforce is a software architecture design pattern that divides the application into three interconnected components – Model, View and Controller that work cohesively to ensure overall organisation and efficiency of the system.
This helps by dividing the application into three manageable components, where each component has a distinct role and responsibility within the application.
Model
Model in Model-View-Controller (MVC) model represents the database layer of the application. In Salesforce, Model represents Objects, Fields and the Relationship established between them.
Objects represent the Model in Model-View-Controller. It can be of two types-
- Standard Objects – Salesforce provides some built-in objects for use such as Accounts, Lead, Contact, Opportunity, etc.
- Custom Objects – We can also design our own objects for our own business needs.
- It is a relational database which means that the objects can have relationships among them via the fields that are present in them.
- It deals with the data stored in the application, schema of the data and business logic. It encapsulates certain capabilities and principles that help us manipulate and manage data effectively while maintaining its integrity.
- It is responsible for storing the CRM data, enforcing relationships between the data and applying data-level security.
View
View in Model-View-Controller (MVC) model represents the presentation layer of the application. It is the UI that is presented to the end-users. It visually represents how the data stored in the database is presented to the user.
In Salesforce, View represents Visualforce Pages (classic approach), Lightning Components (Aura/Lightning Web Components) in Lightning Experience and Page Layouts and Record Pages in standard UI.
- We can design how the user would see the information using Visualforce Pages.
Users can interact with the application, like input data in the system, and receive visual feedback of their actions through View.
- View does not contain any business logic or data manipulation algorithms. It simply receives input from the user and visually present the changes made to the data in the database by the user. It remains unacquainted to the underlying business logic that performs the operation so performed.
- It solely focuses on translating the data and information stored in the database in a more user-friendly way.
- Its responsibilities include displaying data to users in a meaningful format, rendering forms, dashboards or custom UI and accepting input from the user and transmitting it to the Controller.
Controller
Controller in Model-View-Controller (MVC) model represents the logic layer of the application. It acts as an intermediary between the Model and the View.
- Controller receives the input from the View, processes it, and makes the referred changes to the Model. It also checks for any changes made to the Model by the user and updates the view accordingly to display those changes.
- It manages data and event flow, thereby encouraging seamless communication between the Model and View.
Controller can be standard or custom in Salesforce.
- Standard Controllers – Salesforce provides standard controllers for us to use. They can perform basic operations in Salesforce.
- Custom Controllers – We can also design our own custom controllers for more specific business needs.
Controllers, in Salesforce, include Apex Controllers for Visualforce Pages, Client-side Controllers for Aura Components, JavaScript in Lightning Web Components for handling UI logic, Standard Controllers and Custom Controllers.
- Its responsibilities lie in handling user inputs, including button clicks and form submissions, interacting with the Model and returning processed data to the View for display.
Benefits of MVC model:
- Separation of Concerns:
- Each layer (Model, View, Controller) has its own responsibility, which makes it easier to design and use applications.
- Reusability:
- A single Model can be displayed across multiple Views, without rewriting the logic.
- Controllers can also be extended for multiple UI needs.
- Flexibility:
- Developers can switch between UI without changing any data or underling business logic.
- Maintainability:
- Because of the modular system, bugs become very easy to locate and fix.
- One part of the system can be updated without disrupting the other parts.
- Scalability:
- Applications belonging to a business with complex objects and business logic can be scaled and extended with ease.
- Consistency:
- Standard controllers in Salesforce provide a consistent way to interact with objects.
- Users and developers get predictable experience across different parts of the platform.