I'm designing a MVC app and I'm having a hard time figuring how to build my models. For example, I have in my database, Managers, that have a role, are linked to an institution, a program and courses.

I'll have a View for Managers, with their profile information, contact info and the like. I'll also want to link them to one or many institution, programs and courses.

I was going to create models for each of those (Manager, Role, Institution, Program, Courses) but after reading many posts here I believe that for a ManagerProfileView, I should go with a ManagerProfileViewModel. In the ViewModel, I'll then put the ManagerViewModel, etc, etc.

Is this the way I should go or is there a better design ? Thanks in advance !

  • 1
    can you clarify that last paragraph? maybe list the classes – Ewan Aug 18 '17 at 8:03

In ASP.Net MVC you have both ViewModels backing up your views and regular Models representing your business entities.

So you are basically already on the right track. You should create model classes for Manager, Role, Institution, Program and Course and you create ViewModels containing all the data you use inside the respective view.

The motivation for this approach to use two different kind of models is the fact that you usually need to manipulate or transform the data coming from your data source before you can present it to the user. This logic shouldn't be directly inside the View, as it violates SRP and leads to messy code. Therefore, you have this layer of ViewModels in between.

Also see this answer for a great explanation of ViewModels.

  • Thanks for the reply and for the link. I'll work with that and see where it leads me. – jonlapointe Aug 20 '17 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.