DEV Community

Dylan Oh
Dylan Oh

Posted on • Updated on

Short Read: Domain Model vs View Model?

I have been learning C# and ASP.NET recently and came across this idea of Domain Model vs View Model.

I used to think that, as an example one single User model, is used from data layer to the presentation layer. However, in some cases, you might want to have different attributes for the User model in presentation layer and the data layer.

A common example would be, in a Sign up form (presentation layer), you might want to have these three attributes for User model: Username, Password and RePassword(Re-enter your password). In the database, we do not want to store RePassword attribute, and therefore we would use a different User model for database which only contains Username and Password.

You could define the method to retrieve User data in User services layer, and then map the retrieved User attributes that you want to the View User Model.

Let's make another simple example.

We have an Employee model defined in Domain layer:

public class Employee
    {
        public int Id { get; set; }
        [Required]
        public string EmployeeNo { get; set; }
        [Required]
        public string FirstName { get; set; }

        public string MiddleName { get; set; }
        [Required]
        public string LastName { get; set; }

        public string FullName { get; set; }   
    }
Enter fullscreen mode Exit fullscreen mode

This Employee model is mapped to ApplicationDbContext, which is how our Employees information are stored in the database.

We need a method from Service Layer to retrieve this Employee context from ApplicationDbContext.

public class EmployeeService : IEmployeeService
    {
        private readonly ApplicationDbContext _context;
        public EmployeeService(ApplicationDbContext context)
        {
            _context = context;
        }

        public IEnumerable<Employee> GetAll()=>_context.Employees;
    }

Enter fullscreen mode Exit fullscreen mode

In our presentation layer, we define an Employee Controller to handle the mapping from Domain Employee model to View Employee model.

public class EmployeeController : Controller
    {
        // bring the employee service in
        private readonly IEmployeeService _employeeService;
        public EmployeeController(IEmployeeService employeeService)
        {
            _employeeService = employeeService;
        }

        public IActionResult Index()
        {
            // Do the mapping, in this case I only want a few attributes to be in View Employee model
            var employees = _employeeService.GetAll().Select(employee => new EmployeeViewIndexModel
            {
                Id = employee.Id,
                EmployeeNo = employee.EmployeeNo,
                FullName = employee.FullName,
            }).ToList();
            return View(employees);
        }
    }

Enter fullscreen mode Exit fullscreen mode

Appreciate if any of you would like to elaborate some use cases of independent View Model from Domain Model in the comment section below.

Cheers.

Do follow me for more future articles on web design, programming and self-improvement 😊

Discussion (0)