MVC Domain Mode, View Model and Edit Model

 Author: Shengtao Zhou       Created: 1/10/2019 9:56:31 PM       Modified: 2/4/2019 2:12:02 PM   More...

1. Domain Model


A Domain Model represents the object that represents the data in the database. The Domain Model usually has one to one relationship with the tables in the database.

For example, Student class has StudentID, FirstName, LastName, DateOfBirth, UpdateDate fields and Grade foreign key class. These matches Student table in the database.

    public class Student
    {
        public int StudentID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DateOfBirth { get; set; }
        public DateTime UpdateDate { get; set; }
        public Grade Grade { get; set; }
    }
    public class Grade
    {
        public int GradeId { get; set; }
        public string GradeName { get; set; }
        public string Section { get; set; }
        public ICollection<Student> Students { get; set; }
    }

2. View Model


The View Model refers to the objects which hold the data that needs to be shown to the user.

For example, StudentViewModel class has GradeName, FullName, Age for display and do not need Grade class. 

    public class StudentViewModel
    {
        public int StudentID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string GradeName { get; set; }
        public string FullName
        {
            get { return FirstName + LastName; }
        }
        public int Age
        {
            get { return DateTime.Today.Year - DateOfBirth.Year; }
        }
    }

3. Edit Model


The Edit Model or Input Model represents the data that needs to be presented to the user for modification or inserting.

For example, StudentEditViewModel has DataAnnotation attritubes on FirstName, LastName, DateOfBirth and GradeId. These attributes help validate user input.

    public class StudentEditViewModel
    {
        public int StudentID { get; set; }

        [Required(ErrorMessage = "Please enter first name.")]
        [StringLength(20, ErrorMessage = "The {0} length must be between {2} and {1} characters long.", MinimumLength = 2)]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Please enter last name.")]
        [StringLength(20, ErrorMessage = "The {0} length must be between {2} and {1} characters long.", MinimumLength = 2)]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Please enter date of birth.")]
        public DateTime DateOfBirth { get; set; }

        [Required]
        public int GradeId { get; set; }
    }

 Suggestions:

1. Always create View Model and Edit Mode for the Views instead of passing Domain Model directly. This will separate  the UI layer and the Database Layer, so change of one layer will has less impact with another layer.

2. Create an interface with common fields among Domain Model, View Model and Edit Mode for the same entity. This helps ensure that change of one model won't break other part of the code.


More...          Back to List          

(Please enter you comments between 100 to 2000 characters. Thanks for your contribution.) 

         Created:       Modified: 

Editing a comment

       (Please enter you comments between 100 to 2000 characters. Please login before edit comment.)