mardi 29 mai 2018

No derived ViewModels but the same behavior?

I'm writing a small wpf desktop application. My BaseViewModel looks like this:

public abstract class BaseViewModel : INotifyPropertyChanged, IComparable<BaseViewModel>
{
    public abstract string GetDisplayText();
    public abstract string GetImageName();

    // INotifyPropertyChanged
}

I was looking for a best paxis for mvvm. The most say, that there are multiple ViewModels for one Model and I agree to it.

Because I want that all ViewModels of the same type handle the basics in the same way, i thougth they should derived from each other.

public abstract class BaseCustomerVm : BaseViewModel
{
    public abstract string Name { get; set; }
    public abstract int Number { get; set; }
    public abstract bool IsPerson { get; set; }

    public override string GetDisplayText()
    {
        return Name;
    }

    public override string GetImageName()
    {
        if (IsPerson)
            return "Person";
        else
            return "Company";
    }
}

public class Customer1Vm : BaseCustomerVm
{
    public override string Name { get; set; }
    public override int Number { get; set; }
    public override bool IsPerson { get; set; }
}

To implement this, I have the following options:

Version 1:

public class Customer2Vm : BaseCustomerVm
{
    public override string Name { get; set; }
    public override int Number { get; set; }
    public override bool IsPerson { get; set; }
    // Further Properties
}

Version 2:

public class Customer2Vm : Customer1Vm
{
    // Further Properties
}

In my search, I read ViewModels shouldn't derive from each other. This was also answerd in this post. My questions are:

  1. Why should I not derive in this way?
  2. What would be the correct way to handle sutch basics with no inheritance?

Aucun commentaire:

Enregistrer un commentaire