Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC View Model Naming Conventions

I know I will probably get a mixed opinion on this, but I was wondering if there were and "Best Practices" for model naming conventions.

I have a rather large application and I have adopted the following model naming convention:

  • Models
    • Add
      • CategoryAddModel
      • ProductAddModel
    • Edit
      • CategoryEditModel
      • ProductEditModel
    • View
      • CategoryViewModel
      • ProductViewModel

I was thinking of the following as well:

  • Models
    • Add
      • AddCategoryModel
      • AddProductModel
    • Edit
      • EditCategoryModel
      • EditProductModel
    • View
      • ViewCategoryModel
      • ViewProductModel

Which do you prefer, and why?

Do you think it really matters?

like image 774
Sam Avatar asked Sep 01 '11 02:09

Sam


People also ask

How do you name a ViewModel?

Naming Conventions for Name of a Type“ViewModel” is simply added to a “Page”-suffixed name to yield the name of its ViewModel. However, only “Model” is added to a “View”-suffixed name to yield the name of its companion ViewModel.

What is ViewModel in ASP NET MVC?

In ASP.NET MVC, ViewModel is a class that contains the fields which are represented in the strongly-typed view. It is used to pass data from controller to strongly-typed view.

Which of the following example follows the naming convention used for the controller class in ASP NET MVC?

What are the naming conventions to follow in ASP.NET MVC? Controller - Its name must end with “controller” word. Eg. PersonalDetailsController, EmployeesController.


2 Answers

I prefer like {ViewName}{Controller}ViewModel. I also remove Models folder, instead I put view models in ViewModels folder. That makes more sense to me.

eg. AddCategoryViewModel

like image 153
Charandeep Singh Avatar answered Sep 28 '22 00:09

Charandeep Singh


It doesn't matter. You should name things in a consistent, logical, and straightforward way. Basically, just pick something that makes sense and makes you most productive. Consider how your naming convention would work with IntelliSense.

You might also want to consider how easy your code will be to maintain a year from now.

like image 37
marcind Avatar answered Sep 28 '22 02:09

marcind