Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you name your ViewModel classes?

What kind of naming convention is appropriate for ViewModel classes?

Example: for HomeController, Index view? HomeIndexViewModel doesn't seem right.

like image 905
Alex Avatar asked Jun 02 '09 21:06

Alex


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 a ViewModel class?

ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment . It also handles the communication of the Activity / Fragment with the rest of the application (e.g. calling the business logic classes).


2 Answers

I use the following pattern because it's clear and unambiguous :

  • Model : Foo
  • View : FooView
  • ViewModel : FooViewModel
like image 115
Thomas Levesque Avatar answered Sep 22 '22 06:09

Thomas Levesque


EmployeesViewData.

That's what I use and what I've seen in sample applications as well.

About your added example: Imho the name of the class should specify what kind of data it contains. "....IndexViewData" is rather meaningless. What exactly is displayed on that page? Try to summarize it in 1 or 2 word(s) and add 'ViewData' behind it.

Or else just take the name of the controller and drop the "index". HomeViewData sounds fine to me.

like image 33
Thomas Stock Avatar answered Sep 25 '22 06:09

Thomas Stock