C# – Is it Right to Have Dependencies to ViewModel from Data Access Layer and View Layer?

asp.netcdependenciesdesign-patternsmvvm

Context

In my application I use the MVVM pattern. I'll have a ViewModel that can contains information of a student. This viewmodel is used to communicatie between my view and model. I use ASP.net core MVC whit vue.js.

My project contains out of the following elements:

  1. Views (UI)
  2. Controller API (responsible for responding to requests from the view)
  3. Models (logic)
  4. ViewModels(Containing information for the views)
  5. Repository (repository pattern for communicating whit database)

Senario

When the end user creates for example a student it will fill in some data and and press button to safe the student. Now the information will be send to the server whit a ajax call. The Controller API now catches the HTTP request and cast it to the view model.

Controller API

    [HttpPut]
    public JsonResult Put([FromBody] StudentViewModel studentViewModel)
    {
        //...
    }

This is the process in steps:

  1. The view makes an ajax request to the Controller Api. This request has all the property's of the StudentViewModel so it can be parsed in the Controller Api.
  2. The Controller Api takes in the information and will parse it to the StudentViewModel
  3. The Controller Api asks the repository to create a new student from the StudentViewmodel.
  4. The repository will create a new student from the information in the StudentViewModel

I have my data acces layer wrapped in repository pattern. It will look something like this:

Controller API

    [HttpPut]
    public JsonResult Put([FromBody] StudentViewModel studentViewModel)
    {
        using (var db = new UserQuery(DatabaseContext.GetInstance()))
        {
           // creates a user based on the information in the viewmodel
           db.Students.CreateNew(studentViewModel);
        }
    }

But for some reason I do not like this approach. Because I now have my View layer and data layer depend on the same viewmodel. The view is heavily bound to
the StudentViewModel because the data it provide should be convertible and this same VieModel is used for the database to create a new user.

Question

Is there a better way for this scenario? Or is this just fine.

Best Answer

ViewModel objects and Model objects are not the same thing.

The purpose of a Model object is to model something in your domain. The purpose of a ViewModel object is to model something in your view.

Do you see the difference?

The reason this doesn't look right to you is that the Model shouldn't know anything about your ViewModels. Your repository should only know about Models.

There are a couple of ways you can fix this. One way is to build a Model object from the studentViewModel in your controller, and pass that to your repository. The other way to do it is to add a service layer that accepts ViewModel objects and translates them into Model objects for your repository.

Related Topic