Hello Geeks!

I am back with the next part of this series and this time it's a "MODEL". This article contains a classy explanation of MVC Model for beginners with all the goodies in it, that a MVC beginner needs to understand. So let's see what I have.

Quick View

Agenda

A Brief Introduction

The world "Model" has a very strong background in our development world since it covers around hundreds of various concepts. But the Model I'll be discussing in this article as an object that you can use to communicate with your database, performs various business calculations and assists in rendering a view for your application.



The Model's Role

The Model represents the real world object and provides data to the view. A Model is a business activity on which the complete scenario of the application depends on or you can work upon.

Because of a Model in MVC there is no need to define the Data Access Layer specifically. It is understood to be encapsulated by the model itself. The following is the diagrammatic representation that explains it all:



Working with a Model

ASP.NET MVC provides a number of tools and features to build application features using only the definition of the model object. Let's explore this with a simple example.

Suppose you want to create a picture gallery. ASP.NET MVC provides you friendly tools and functionalities to create a Controller and View (as discussed in previous articles) for some sort of functioning in your gallery. These features, or I better say scenarios, can be create, delete, edit and so on for each of the model objects.

All this construction work is called Scaffolding in MVC.

Scaffolding | Model

Scaffolding in ASP.NET MVC can generate the base code you need for Create, Delete, Update and Delete (CRUD) operations in MVC for you application. It is actually a template. You just need to select one template among the others and based on that template the code required for the CRUD operations will be generated for you.

The Scaffolding is smart, it knows all the following things:

(You don't need to be worry about any of these. Scaffolding will do all that for you. I'll explain Scaffolding in a separate article in detail.)

Some Common Mind Twisters

MT1: What if there was no Model?


MT2: Is Model something new?

The answer to this twister is short, "no".

There is nothing new in it. Just remember the concept of a 3-Tier Architecture for development. There used to be three layers. One of them is "Database Layer". So the Model in MVC is the same as the Database Layer in the 3-Tier architecture. The one and only difference between them is the "separation of concerns".

MT3: Why Model? or What is the use of it?

The answer to both of these twisters is the same. Actually let me present the two most important points:

MT4: How does a Model work with both a Controller and a View?

The Controller handles a request from the View and updates the Model with the corresponding changes. This results in a change of the Model's current state.



(If you guys have any other mind twisters in your mind then let me know I'll update that twister here.)

Hierarchy | Model

This is a typical Model Hierarchy in MVC. I tried to keep this simple and less descriptive since the diagram says it all.
Just go through this and understand the typical Model structure and functionalities in MVC.

Functionality Oriented Hierarchy
Structure Oriented Hierarchy

Exploring Model

Let's explore the "Model".

Click on the Solution Explorer, here you will get a list containing a set of options that includes Images, Model, View, Controller, Filter and App_Data.



Just click on "Model" and further you will see a list containing the following fields:



Actually these are not just options, these items in the list will define the working functionality of your web application. Let's explore these one by one.

Interview Questions Based on "Model"

So, if you guys are into MVC then try to memorize these sets of questions based on the MVC model because in a parallel world you can encounter these questions during an interview or discussion with other Geeks and colleagues.

  1. How does a Model accommodate a Controller and a View in MVC?
  2. What does a Model represent in the MVC framework?
  3. Is a Model in MVC similar to a database layer in 3-Tier Architecture?
  4. What do you understand by Scaffolding in MVC?
  5. What are the various types of Scaffold templates available in ASP.NET MVC?
  6. Where do you see separation of concerns in MVC?
  7. What is a ViewModel in MVC?
  8. Name some application templates in MVC?
  9. What is NuGet in MVC?
  10. Can we customize Scaffolding in MVC?

(These are a very few questions. If anyone has more questions then let me know and I'll update all those here. One more thing I didn't rovide answers to in order to keep this article as brief as possible. If any of you have query regarding any question, then his/her queries are warmly welcomed.)

Points

Here's a summary of this article:

Summary

I used some complex terms in this article like Scaffolding, templates and so on. I'll explain all these terms in a separate article of this series because all these are as important as learning OOP concepts before going through any object oriented language.

A few tips from my side is that, if you guys really want to become familiar with MVC:

I hope you guys enjoyed this.

#HappyCoding