ASP.NET Core MVC model in the Model

ASP.NET Core MVC in Model

 

We hope that the final query-specific details from the Student student database table and displayed on a Web page, as shown below.

18 1 18 2

MVC model contains a set of classes and represents a logical data management of the data. Therefore, in order to show the students that we want to display the data, we use the following Student class.

public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ClassName { get; set; }
    }

Model class ASP.NET Core Models need not be located in the folder, but they would be a good idea to save the file in a folder called Models, because they are easier to find later.

In addition to the data representing the Student class, further comprising a model-based management model data. In order to manage the data that retrieve and save student data, we will use the following IStudentRepository service. At present, we have only one way ** GetStudent () ** by student ID queries. As the course progresses, we will add to create, update, and delete methods.

 public interface IStudentRepository
    {
        Student GetStudent(int id);

    }

The following MockStudentRepository class provides IStudentRepository implementation of the interface. We are currently on MockStudentRepository class Student data is hard-coded. In the video we are about to release, we will IStudentRepository interface provides another implementation, from the SQL Server database to retrieve data for the implementation.

 public class MockStudentRepository : IStudentRepository
    {
        private List<Student> _studentList;

        public MockStudentRepository()
        {
            _studentList = new List<Student>()
            {
            new Student() { Id = 1, Name = "张三", ClassName = "一年级", Email = "[email protected]" },
            new Student() { Id = 2, Name = "李四", ClassName = "二年级", Email = "[email protected]" },
            new Student() { Id = 3, Name = "王二麻子", ClassName = "二年级", Email = "[email protected]" },
            };
        }


        public Student GetStudent(int id)
        {
            return _studentList.FirstOrDefault(a => a.Id == id);
        }
    }

In our application, we will focus on IStudentRepository programming interface, rather than a specific implementation MockStudentRepository. This interface abstraction is to allow us to use dependency injection, which in turn makes our application flexible and easy unit testing.

Welcome to add a personal Micro Signal: Like if thoughts.

I welcome the attention of the public numbers, not only recommend the latest blog for you, there are more surprises waiting for you and resources! Learn together and common progress!

 

Guess you like

Origin www.cnblogs.com/cool2feel/p/11455365.html