Models & Repository – Session 15

Models on ASP.NET Core MVC - Session 15

In this session of ASP.NET Core tutorial we are going to talk about Models and Repository in MVC Architectural Design pattern. As we describe earlier, Models indicate the state of application and its Business Logic. During this session we will create our first Entity Model and its Repository.

What is Model in MVC Architectural Design?

The Model part indicates the state of application or any business logic that is supposed to be performed by it. It is highly recommended that all the business logic should be encapsulated in Model part.

You can create your Web App Model, Repository and Interface in any place in your application, but due to better file structure, we highly recommend creating these classes within Models folder.

Create Model for Entity

For this purpose you should create a Class within your Web App Models folder. Then based on your entity properties, complete your Model. In this tutorial, we try to implement a Web App that can store your daily cost. Thus, we need an Entity for storing costs. We call this Entity Model “Cost” that has 5 properties. ID as primary key, Date, Amount, Category and Comments.

ASP.NET Core Cost Entity

For adding Property, you can use prop abbreviation and then press Tab key twice

Adding Required Interface and Repository

To perform CRUD (Create – Read – Update – Delete) operations, we need to add an Interface and Repository for our Entities. This step is not mandatory. But, for implementing Dependency Injection that we will talk about it in further sessions, we need to create Interface and Repository for our Entity.

  • Step 1: Create an Interface within your Models Folder
  • Step 2: Create a Class within Models folder and Implement it from above created Interface
  • Step 3: Implement Interface Member to your Repository Class
  • Step 4: As we have not created database for storing data, we add some static record in our Repository

Generally, for using Visual Studio IntelliSense you can press Ctrl + . or Alt + Enter in piece of code that indicated with red squiggle.

How we can use our Repository within our Controller

  • Step 1: Register Interface service ConfigureServices Method of Startup Class
  • Step 2: Create Constructor for your Controller
  • Step 3: Set the Interface as input parameter of Constructor
  • Step 4: Create a private instance of Interface
  • Step 5: Set the value of Private Instance of Interface

We have just one Interface and Repository until now within our Web App and we can omit Dependency Injection and do this process much more easier. But, In further sessions you will realize the benefits of Dependency Injection and importance of this process.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram or YouTube and you can have access to list of all sessions HERE.

You can download this Session Slides form HERE

0.00 avg. rating (0% score) - 0 votes

Tags

0 0 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments