ASP.Net Core MVC Project Part 3

In this article we will add Model classes and repository interfaces. This article is third part of Asp.Net Core MVC project series. Please read article ASP.Net Core MVC Project Part 2 before reading this article.

In previous article our application is setup to use MVC. Now we will add required Models. Models are domain related classes which contain data and properties. Yo can think them as mirror of database tables. By default model classes needs to be created under ‘Models’ folder. Right click on Project and add new folder ‘Models’:

Now right click on Models folder and add new class file ‘Product.cs’

Product.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace eShop.Models
{
 public class Product
 {
	public int ProductId { get; set; }
	public string  Name { get; set; }
	public string Description { get; set; }
	public decimal Price { get; set; }
	public string ImageUrl { get; set; }
	public string ImageThumbnailUrl { get; set; }
	public bool IsOnSale { get; set; }
	public bool IsInStock { get; set; }
	public int CategoryId { get; set; }
	public Category Category { get; set; }

 }
}

We have added 2 columns for displaying image of Product. ImageUrl will hold URL of a bigger image and ImageThumbnailUrl will hold URL of a smaller image. IsOnSale property identifies if product is on sale cureently. We have also added CategoryId property to identify category of Product and we have addded another property of type Category.It will refere to complete category object.

We have not added Category class yet, so now lets create Category class. Right click on Models folder and create new class file Category.cs, below is the code of Category.cs:

public class Category
{
	public int CategoryId { get; set; }
	public string CategoryName { get; set; }
	public string CategoryDescription { get; set; }
	public List<Product> Products { get; set; }
}

We have added a list of all products associated with a particular Category using Products property.Object of our model classes will contain data but we need repository to process that data.We will create interfaces first to work with repository.

Repositories

Right click on project and add new folder “Repositories”. We will create all repository interface here.

Right click on Repositories folder and add a new interface ‘IProductRepository’

This interface will contain properties and methods declaration for Products. Below is the interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using eShop.Models;

namespace eShop.Repositories
{
 public interface IProductRepository
 {
	IEnumerable<Product> GetAllProduct { get; }
	IEnumerable<Product> GetProductOnSale { get; }
	Product GetProductById(int ProudctId);
  }
}

IProductRepository has GetAllProduct and GetProductOnSale properties which are read only and will return list of Products. There is method GetProductById() which return a particular product based on ProductId.

Now right click on Repositories folder and add interface ‘ICategoryRepository’:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using eShop.Models;

namespace eShop.Repositories
{
   public interface ICategoryRepository
    {
        IEnumerable<Category> GetAllCategories { get; }
    }
}

ICategoryRepository has only one property GetAllCategories which is read only and will return all categories. We will implement these interfaces in next article.

Download code from below URL:
https://github.com/vijendra-ksh/eShop

Leave a Comment

Your email address will not be published. Required fields are marked *