tags:

views:

116

answers:

2

I'm struggling a little over naming classes for my MVVM application.

I have a TrainingCourse, which is called a TrainingCourseViewModel, but I can have many of these, so I have created a TrainingCourseViewManager to hold the list of courses and allow them to be added/removed. I also have an EmployeeViewController which has a reference to other view models as well as the TrainingCourseViewManager.

The EmployeeViewController essentially wraps all of the other view models and view managers and when its instantiated it gets the employee and in turn instantiates each of the view models and view managers.

The question is... What naming conventions are people using?

Should my TrainingCourseViewManager be called TrainingCoursesViewModel and should my EmployeeViewManager be called EmployeeViewModel?

Thanks

A: 

Should my TrainingCourseViewManager be called TrainingCoursesViewModel and should my EmployeeViewManager be called EmployeeViewModel?

What are your window classes called? (what is your .xaml file called?)

The naming convention goes, that you create one ViewModel class per View (a view is a .xaml/.xaml.cs pair)

If you have a single window which displays a list of Employees and Training Courses, then you'd have something like this:

namespace Models
{
  public class Employee : INotifyPropertyChanged { ... }
  public class TrainingCourse : INotifyPropertyChanged { ... }
}

namespace ViewModels
{
  // assuming you have TrainingWindow.xaml

  public class TrainingWindowViewModel : INotifyPropertyChanged
  {
    public ObservableCollection<TrainingCourse> TrainingCourses
    { get{ return m_trainingCourses; } }
    { set{ m_trainingCourses = value; RaisePropertyChanged("TrainingCourses"); } }

    ...
  }

  // so on and so forth
}
Orion Edwards
+1  A: 

There might be a confusion over the role of view model.

Classes in your example (and in Orion's answer to that matter) seem more like actual data model. For example, it doesn't make sense for a view model to "hold the list of courses and allow them to be added/removed" - that's what data model should do. Add and remove operations on a view model wouldn't operate on the collection itself - instead, they would access and modify underlying data model.

Do properties of TrainingCourseViewModel class store actual data values, or wrap properties of some TrainingCourseDataModel class (with additional processing)? Or if you need to serialize data, would you serialize TrainingCourseViewModel objects? If former is true, you are binding directly to the data model, and there should be no 'ViewModel' suffix in names.


On the topic of naming conventions, if names become too complex, namespaces can help. For example:

namespace TrainingCourseView.ViewModel
{ 
class TrainingCourse {} 
class Manager {}
class Controller {}
}
...
Data.TrainingCourse course;
new ViewModel.TrainingCourse(course);
ima
My TrainingCourseViewModel is a wrapper for the TrainingCourseModel class. An employee can have many courses so I have to store each TrainingCourseModel in a list. I then have a list of TrainingCourseViewModels which exposes each Training Course. I bind to this list from the view. I have a class that allows a course (TrainingCourseViewModel) to be added/removed to the list , because otherwise it would have to add them directly to the Model, which means that I would have to expose the collection of TrainingCourseModels to the view and I wouldn't be able to format the data as I want it.
I want to format all of my data in the view model rather than using converters, as I don't want a mix of the two, so I've opted to expose all string properties in the view model that formats/converts to the correct value. If I don't do this then I would have to format in the model, which I don't want to.I appreciate any help with this.
"because otherwise it would have to add them directly to the Model" - no, you'll have to add them to the collection of data model objects. This collection can well be temporarily, created in some function like: DataCollection data = FilterData(); ViewModel vm(data); view.DataContext = vm;
ima