The Model View Controller (MVC) is one of the popular software architectures that make applications compose out of three interconnected elements — model, view, and controller
- The Model represents the data and business logic of the application.
- View displays data, and
- The Controller handles communication between user and application in MVC model.
The MVC framework has been implemented in Salesforce (SFDC) very efficiently to achieve application separation. Visual force pages work as the View layer over here- UI, data presentation. So What are Visualforce pages?
Visualforce pages are similar to Java Server Pages (JSP) in which a Controller is mapped for each Visualforce page. Salesforce provides Standard Controllers for all database objects as well as custom controllers, which are written in Apex (which is Salesforce object-oriented language analogous to Java). By doing so, developers can enable functionality customization based on business needs.
Salesforce Model layer has sObjects which are core representation of all Database entities. This layer, containing the object data available in Salesforce (Objects, Fields and Relationships) is our Model layer, representing the schema and relations within the database.
This layer, called the Controller layer is where all the application logic lives for both workflow (or declarative) automation as well as triggers (or programmatic) automation via Apex Classes. Apex is a programming language that allows to write business logic, execute based actions on sObjects and manage UI behavior as user actions.
SFDC MVC: You can write your VIEW pages using SFDC visual force (VF pages). VF pages are similar to our JSP pages. Each VF page is associated with a Controller. you can make use to already built Standard controllers or you can write your own controller using Apex language. Apex is OO and very much similar to our JAVA. you can also write Model Classes using Apex.
Visualforce uses the traditional model-view-controller (MVC) paradigm, with the option to use auto-generated controllers for database objects, providing simple and tight integration with the database. You can write your own controllers, or extensions to controllers, using Apex Code. Visualforce also provides AJAX components, and embeds the formula expression language for action, data and component binding interaction.
Salesforce.com is award winning tool to manage all the data of sales team of an organization. The flexibility and assurance of safe data provided by Salesforce.com results into nonparallel development capabilities to the developer.
SFDC MVC pattern contains below three modules:
- Model
- View
- Controller
Model: What schema and data does salesforce uses to represent the system completely. In salesforce, we can say that sObjects are the model as every entity in salesforce is mapped to some sObject.
View: How the schema and data is represented. Visualforce is used to present the data to users.
Controller: How the interface actions. Controllers are used to perform the actions whenever users interact with visual force.
In SFDC
1. Visual Force pages, Page Layouts, Tabs comes under View Layer of Model View controller .
2. Workflows, Apex Classes, Triggers comes under Controller part in Model View controller .
3. Objects, Fields, Relationships comes under Model Layer of Model View Controller .
Model View Controller (MVC) Example for Beginners
Certainly! Here are some beginner-friendly examples for understanding each layer in the Model-View-Controller (MVC) pattern within Salesforce:
Example 1: Model (Data Layer)
The Model layer represents the data and business logic of the application. In Salesforce, this is typically an sObject, such as a Contact
or Account
.
Task: Create a Custom Object
- Go to Setup in Salesforce.
- Navigate to Objects and Fields > Object Manager.
- Click Create Object and define a new object, like
Project
. - Add fields to represent the data structure, such as
Project Name
,Project Manager
, andDeadline
.
This object, Project
, will act as the Model in the MVC pattern, storing project-related information.
Example 2: View (User Interface)
The View layer in Salesforce is typically created using Visualforce pages or Lightning Components. For this example, let’s use a Visualforce page to display data from the Project
object.
Task: Create a Visualforce Page
- Go to Setup > Visualforce Pages and click New.
- Name the page (e.g.,
ProjectView
). - Use the following code to display a list of projects:
<apex:page controller="ProjectController">
<h1>Project List</h1>
<apex:pageBlock title="Projects">
<apex:pageBlockTable value="{!projects}" var="proj">
<apex:column value="{!proj.Name}" headerValue="Project Name"/>
<apex:column value="{!proj.Project_Manager__c}" headerValue="Project Manager"/>
<apex:column value="{!proj.Deadline__c}" headerValue="Deadline"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
This Visualforce page will render a simple table of project names, managers, and deadlines by using the View layer.
Example 3: Controller (Logic Layer)
The Controller layer processes the logic between the Model and View. Here, we’ll create an Apex Controller to pull data from the Project
object and make it available to the Visualforce page.
Task: Create an Apex Controller
- Go to Setup > Apex Classes and click New.
- Name the controller (e.g.,
ProjectController
). - Use the following code:
public class ProjectController {
public List<Project__c> projects { get; set; }
public ProjectController() {
projects = [SELECT Name, Project_Manager__c, Deadline__c FROM Project__c];
}
}
This ProjectController
retrieves data from the Project
object and passes it to the View layer. When the Visualforce page loads, it uses this controller to get project data and display it in the table.
The above examples demonstrate how each layer in the MVC architecture (Model, View, Controller) interacts within Salesforce.
Salesforce MVC Implementation (MVC) in the Salesforce context enables organized data management, effective UI control combined with a robust business logic giving compressed developers an option to easily manage their architecture for scalable applications.
Model view controller (MVC) is a software architecture pattern which separates the representation of information from the user’s interaction with it
In addition to dividing the application into three kinds of components, the MVC design defines the interactions between them.
A controller can send commands to its associated view to change the view’s presentation of the model (e.g., by scrolling through a document). It can also send commands to the model to update the model’s state (e.g., editing a document).
A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands. A passive implementation of MVC omits these notifications, because the application does not require them or the software platform does not support them.
A view requests from the model the information that it needs to generate an output representation.