MVVM class for base view

I am working on creating a base view model class. ViewModelBase is an abstract class, and I want to define the properties that I want all my other derived view models to implement.

One of the properties is ObservableCollection:

public abstract ObservableCollection<???> Items { get; set; } 

In classes that are derived from this base class, different types of elements will be defined ( ObservableCollection<Person> , ObservableCollection<Car> ).

If I set the ObservableCollection type to object in ViewModelBase, I would need to do many different castings in derived classes to make it work.

Is this the right approach?

+6
source share
5 answers

I'm not quite sure why you would like to make it so universal, but if you did, I would recommend that you create a common base class:

 public abstract class ViewModelBase<T> { public abstract ObservableCollection<T> Items { get; set; } } 

Hope you also make sure your ViewModelBase implements INotifyPropertyChanged.

+11
source
 public abstract ObservableCollection<TYPE> Items { get; set; } 

You can define TYPE in many ways, including when using / inheriting from a base class or interface.

+2
source

If you create a base view model for all view models in your application, it is unlikely that each of them will contain an observable collection.

You can use composition instead of inheritance (or, more likely, also inheritance) to add common features to your view models.

For instance:

 // Base for ALL view models implements necessary interfaces. public abstract class BaseViewModel : INotifyPropertyChanged, IDisposable { // Functionality useful to ALL view models public string DisplayName { get; } // etc. ... } // Generic collection view model implementing functionality required by // all collection view models. public abstract class CollectionViewModel<T> : BaseViewModel { public abstract ObservableCollection<T> Items { get; set; } } // Specific collection view model using generic one through composition public class PersonCollectionViewModel : BaseViewModel { public CollectionViewModel<Person> PersonCollection { get; } // other functionality not included in all collection view models. // ... } 
+2
source

Define some class {Entity}, which can be the base class of Car and Person: you get what you want + strong typing.

0
source

Do you want to create a common base class

Then you may have such a deviation

 public abstract class ViewModelBase<T> { public abstract ObservableCollection<T> Items { get; set; } } 

If you need a list of view modes, I would recommend creating a non-generic base class that inherits the generic one, and has a list of this

 public abstract class NonGenericBaseViewModel { ObservableCollection<object> ItemsAsObjects{get;set;} } public abstract class ViewModelBase<T> : NonGenericBaseViewModel { public ObservableCollection<T> Items { get; set; } } 

then you can have

 ObservableCollection<NonGenericBaseViewModel> 

if you want to

0
source

Source: https://habr.com/ru/post/892032/


All Articles