john doe
john doe

Reputation: 9662

Collection Getting Empty When Calling the Base Class Method

I have a base call BaseViewModel which consists of a IList collection as shown below:

public class BaseViewModel
{
    private IList<BrokenRule> _brokenRules = new List<BrokenRule>();                    

    public IList<BrokenRule> BrokenRules
    {
        get { return _brokenRules; }
        set { _brokenRules = value;  }
    }     

    public void ShowBrokenRules()
    {
        _brokenRulePresenter.Present(_brokenRules);           
    }
}

My view model UserEditViewModel inherits from the BaseViewModel. The validation engine validate the model and populate the broken rules. When I call the ShowBrokenRules from the concrete class (UserEditViewModel) then the broken rules collection does not contain anything.

UserEditViewModel Save Method:

private void Save(UserEditViewModel userEditViewModel)
{
    bool isValid = ValidationEngine.Validate(this.UserViewModel); // 

    if (!isValid)
    {
        ShowBrokenRules();
    }
}    

I think the reason is that inside ShowBrokenRules method the _brokenRules refers to the base class implementation. Am I correct?

UserEditViewModel class:

 public class UserEditViewModel : BaseViewModel 
    {
        private UserViewModel _userViewModel;    

        private void Save(UserEditViewModel userEditViewModel)
        {
            bool isValid = ValidationEngine.Validate(this.UserViewModel); // The UserViewModel.brokenrules is populated not the UserEditViewModel.brokenrules maybe that is the problem.

            if (!isValid)
            {
                ShowBrokenRules();
            }
        }    

Upvotes: 0

Views: 41

Answers (1)

Evil Dog Pie
Evil Dog Pie

Reputation: 2320

You are calling ValidationEngine.Validate() on the UserViewModel member property of the UserEditViewModel instance and not on the instance itself. This will result in the function returning false because the validation failed, but the BrokenRules are set on the member object, not the object itself. So when you call ShowBrokenRules(), it shows the broken rules for the parent object, not the object that was passed to the ValidationEngine.Validate() method.

Upvotes: 1

Related Questions