MVC is a design pattern that is widely used in user-facing software, such as web applications. It is intended for splitting data and representing it in a way that makes it convenient for user interaction. To understand what it does, understand the following:
There could be some differences in the MVC implementation, but in general it conforms to the following scheme:
Worldwide practice shows that the use of the MVC pattern provides various benefits to the developer:
Following the separation of the concerned paradigm, which splits an application into independent parts, it is easier to modify or replace
It achieves code reusability by rendering a model in different views without the need to implement model functionality in each view
It requires less training and has a quicker startup time for the new developers within an organization
To have a better understanding of the MVC pattern, we are going to design a Billing Application. We will refer to this design throughout the book when we are learning specific topics.
Our Billing Application will allow users to generate invoices, manage them, and send them to clients. According to the worldwide practice, the invoice should contain a reference number, date, information about the buyer and seller, bank account details, a list of provided products or services, and an invoice sum. Let's have a look at the following screenshot to understand how an invoice appears:
Let's follow the ensuing steps to design an MVC structure for the Billing Application:
Let's write down a list of functional requirements for this application. We assume that the end user may want to be able to do the following:
Generate an invoice
E-mail the invoice to the buyer
Print the invoice
See a list of existing invoices
Manage invoices (create, read, update, and delete)
Update an invoice status (draft, issued, paid, and canceled)
View a yearly income graph and other reports
To simplify the process of creating multiple invoices, the user may want to manage information about buyers and his personal details in the specific part of the application before he/she creates an invoice. So, our application should provide additional functionalities to the end user, such as the following:
The ability to see a list of buyers and use it when generating an invoice
The ability to manage buyers (create, read, update, and delete)
The ability to see a list of bank accounts and use it when generating an invoice
The ability to manage his/her own bank accounts (create, read, update, and delete)
The ability to edit personal details and use them when generating an invoice
Of course, we may want to have more functions, but this is enough for demonstrating how to design an application using the MVC pattern.
Next, we architect an application using the MVC pattern.
After we have defined the features of our application, we need to understand what is more related to the model (business logic) and what is more related to the view (presentation). Let's split the functionality into several parts.
Then, we learn how to define models.
Models present data and provide data-specific business logic. Models can be related to each other. In our case, they are as follows:
InvoiceModel
InvoiceItemModel
BuyerModel
SellerModel
BankAccountModel
Then, will define collections of models.
Our application allows users to operate on a number of models, so they need to be organized into a special iterable object named Collection. We need the following collections:
InvoiceCollection
InvoiceItemCollection
BuyerCollection
BankAccountCollection
Next, we define views.
View present a model or a collection to the application user. A single model or collection can be rendered to be used by multiple views. The views that we need in our application are as follows:
EditInvoiceFormView
InvoicePageView
InvoiceListView
PrintInvoicePageView
EmailInvoiceFormView
YearlyIncomeGraphView
EditBuyerFormView
BuyerPageView
BuyerListView
EditBankAccountFormView
BankAccountPageView
BankAccountListView
EditSellerInfoFormView
ViewSellectInfoPageView
ConfirmationDialogView
Finally, we define a controller.
A controller allows users to interact with an application. In MVC, each view can have a different controller that is used to do following:
Map a URL to a specific view
Fetch models from a server
Show and hide views
Handle user input