Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD
Build lego disney princess castle | Making princess castle | Sewing Unicorn | Build lego smoothie van | Solve frozen elsa puzzle | Taj mahal jigsaw puzzle | Statue of liberty jigsaw puzzle | Coloring a cat | Frozen elsa and anna jigsaw puzzle | Build lego disney princess castle | Making princess castle | Sewing Unicorn | Build lego smoothie van | Solve frozen elsa puzzle | Taj mahal jigsaw puzzle | Statue of liberty jigsaw puzzle | Coloring a cat | Frozen elsa and anna jigsaw puzzle

Model in ASP.NET Core MVC

Suggested Videos
Part 15 - ASP.NET Core MVC tutorial | Text | Slides
Part 16 - Setup mvc in asp.net core | Text | Slides
Part 17 - ASP.NET Core AddMvc vs AddMvcCore | Text | Slides

In this video we will discuss Model in ASP.NET Core MVC with an example. 


We want to ultimately, retrieve a specific employee details from the Employees database table and display on the web page as shown below.

asp.net core model in mvc


Model in MVC contains a set of classes that represent data and the logic to manage that data. So, to represent the employee data that we want to display, we use the following Employee class.

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Department { get; set; }
}

Model classes in ASP.NET Core does not have to be in the Models folder, but keeping them in a folder named Models is a good practice as it is easier to find them later.

In addition to the Employee class that represent the data, model also contains the class that manages the model data. To manage the data i.e to retrieve and save employee data we are going to use the following IEmployeeRepository service. At the moment, we only have one method GeEmployee() that retrieves employee by ID. As we progress through the course we will add methods to Create, Update and Delete as well.

public interface IEmployeeRepository
{
    Employee GetEmployee(int Id);
}

The following MockEmployeeRepository class provides the implementation for IEmployeeRepository interface. At the moment, we are hard coding the Employee data in the MockEmployeeRepository class. In our upcoming videos, we will provide another implementation for IEmployeeRepository interface, and that implementation will retrieve data from a SQL Server database.

public class MockEmployeeRepository : IEmployeeRepository
{
    private List<Employee> _employeeList;

    public MockEmployeeRepository()
    {
        _employeeList = new List<Employee>()
        {
            new Employee() { Id = 1, Name = "Mary", Department = "HR", Email = "mary@pragimtech.com" },
            new Employee() { Id = 2, Name = "John", Department = "IT", Email = "john@pragimtech.com" },
            new Employee() { Id = 3, Name = "Sam", Department = "IT", Email = "sam@pragimtech.com" },
        };
    }

    public Employee GetEmployee(int Id)
    {
        return this._employeeList.FirstOrDefault(e => e.Id == Id);
    }
}

Throughout our application we will be programming against the interface IEmployeeRepository and not the concrete implementation MockEmployeeRepository. This interface abstraction allows us to use dependency injection which in turn makes our application flexible and easily unit testable. We will discuss Dependency Injection in detail in our next video.

asp.net core tutorial for beginners

2 comments:

  1. Venkat, At some point, could you identify which code would need to be in "Business Logic Layer or Domain Layer" and
    "Data Access layer" of application layers?
    Since the Model has data classes representation and the logic to manage that data,
    It does not appear that we need any more classes in the Business logic and Data access layers.

    ReplyDelete
  2. I am facing this kind of error

    Inconsistent accessibility: parameter type 'IEmployeeRepository' is less accessible than method 'HomeController.HomeController(IEmployeeRepository)'

    thanks in advance !

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.