Jose Capistrano
Jose Capistrano

Reputation: 693

Overriding Method in WPF ViewModel

I have a BaseFormViewModel class which some of ViewModels inherit from. It inherits from another base class called BaseViewModel which just contains implementation of INotifyPropertyChanged interface.

The BaseFormViewModel contains several methods which I intend to be overriden in the derived classes:

public class BaseFormViewModel : BaseViewModel {

        public BaseFormViewModel() {
            _InitiateParameterAnswer = new Command(param => RaiseInitiateParameterAnswer(param));
        }

        protected Command _InitiateParameterAnswer;
        public Command InitiateParameterAnswer {
            get {
                return _InitiateParameterAnswer;
            }
        }

        protected virtual void RaiseInitiateParameterAnswer(object values) {
            //Implementation
        }

        protected virtual Parameter SetAuditParameter(ParameterOption parameterOption, Guid parameterId, string remarks, string description, int weight) {
            //Implementation

        }
}

Here's one of the derived classes:

public class FSCGeneralProductionProcessMainViewModel : BaseFormViewModel {

    public FSCGeneralProductionProcessMainViewModel() {

    }

    protected override void RaiseInitiateParameterAnswer(object values) {
        //Implementation
    }

    protected override Parameter SetAuditParameter(ParameterOption parameterOption, Guid parameterId, string remarks, string description, int weight) {
        //Implementation
    }

However, whenever I invoke the InitiateParameterAnswer Command, the base class methods are being called. What am I missing here?

UPDATE: Thanks for the feedbacks. With @Muds comment, I realised that I am calling each form (View) via Reflection and downcasting its DataContext (ViewModel) to BaseFormViewModel:

//Create an instance of the corresponding form using reflection
var instance = Activator.CreateInstance(Type.GetType(formName, true));

//Set the FormId
var viewModel       = ((UserControl)instance).DataContext as BaseFormViewModel;
viewModel.FormId    = formId;
viewModel..AuditId  = auditId;

(The reason I have to is to set the value of base properties FormId and AuditId as illustrated above).

UPDATE 2: The Command class is implemented as follows:

public class Command : ICommand {

    public Command(Action action, bool canExecute = true) {
        this.action = action;
        this.canExecute = canExecute;
    }
    //ICommand Interface implementation here
}

UPDATE 3:

I instantiate the ViewModel inside the Form's constructor:

public partial class FSCStationProductionProcessMainView : UserControl {

    public FSCStationProductionProcessMainView () {
        InitializeComponent();
        InitializeViewModel();
    }

    private void InitializeViewModel () {
        this.DataContext = new FSCStationProductionProcessMainViewModel();
    }
}

Upvotes: 0

Views: 1629

Answers (1)

AwkwardCoder
AwkwardCoder

Reputation: 25651

The simple answer would be to mark your BaseFormViewModel RaiseInitiateParameterAnswer method implementation as 'abstract' and force all derived classes to provide an implementation.

But in general this design seems overly complex:

Do you really need to so many levels of inheritence - it will be confusing and difficult in the long term to support and maintain.

Inheritence in my opinion should be used sparingly in object orientiated design.

Upvotes: -1

Related Questions