将IDataErrorInfo与嵌套对象一起使用

And*_*eas 4 c# mvvm idataerrorinfo

我正在使用MVVM,我想使用IDataErrorInfo来验证我的View.

我当前的实现包括嵌套对象和不同的ViewModel.例如,业务实体"客户"包含业务实体"地址".我在我的视图中直接访问Address,例如"Customer.Address".要验证Address中的更改,我必须在Address中实现IDataErrorInfo.

我在不同的Views/ViewModels中使用Customer或Address.在不同的Views/ViewModel中使用会导致不同的验证行为.因此,在实体本身中实现验证是不够的.

公开我想直接在ViewModel中更改的属性(创建直接设置/获取实体的新属性)似乎使ViewModel方式过于僵化.并且太大了.

我不能从Base Classes继承,因为一些商业实体已经从其他对象派生(事实上我无法改变).我目前看到的唯一选择是向业务实体添加ViewModel接口,并将业务实体中的this []调用转发到该ViewModel接口.

有关如何在ViewModel中验证这些嵌套对象的最佳实践吗?

编辑:另一个原因验证我没有看到Business Objects中的验证作为一个有用的想法是我需要在我的ViewModel中使用不同的Business Objects来验证View和数据条目.

Rac*_*hel 7

我过去做过这种方法的一种方法是在Model上公开ValidationDelegate,它允许ViewModel将自己的验证代码附加到模型中.

通常我这样做是因为我将Model图层用作普通数据对象,因此我的模型仅验证基本内容,例如max-length或not-nulls,而任何不特定于数据模型的高级验证都在ViewModel中完成.这通常包括诸如确保项目是唯一的,或者用户有权将值设置为特定范围,或者甚至类似于仅针对特定操作存在验证的情况.

public class CustomerViewModel
{
    // Keeping these generic to reduce code here, but it
    // should include PropertyChange notification
    public AddressModel Address { get; set; }

    public CustomerViewModel()
    {
        Address = new AddressModel();
        Address.AddValidationDelegate(ValidateAddress);
    }

    // Validation Delegate to validate Adderess
    private string ValidateAddress(object sender, string propertyName)
    {
        // Do your ViewModel-specific validation here.
        // sender is your AddressModel and propertyName 
        // is the property on the address getting validated

        // For example:
        if (propertyName == "Street1" && string.IsNullOrEmpty(Address.Street1))
            return "Street1 cannot be empty";

        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我通常用于验证委托的代码:

#region IDataErrorInfo & Validation Members

#region Validation Delegate

public delegate string ValidationDelegate(
    object sender, string propertyName);

private List<ValidationDelegate> _validationDelegates = 
    new List<ValidationDelegate>();

public void AddValidationDelegate(ValidationDelegate func)
{
    _validationDelegates.Add(func);
}

public void RemoveValidationDelegate(ValidationDelegate func)
{
    if (_validationDelegates.Contains(func))
        _validationDelegates.Remove(func);
}

#endregion // Validation Delegate

#region IDataErrorInfo for binding errors

string IDataErrorInfo.Error { get { return null; } }

string IDataErrorInfo.this[string propertyName]
{
    get { return this.GetValidationError(propertyName); }
}

public string GetValidationError(string propertyName)
{
    string s = null;

    foreach (var func in _validationDelegates)
    {
        s = func(this, propertyName);
        if (s != null)
            return s;
    }

    return s;
}

#endregion // IDataErrorInfo for binding errors

#endregion // IDataErrorInfo & Validation Members
Run Code Online (Sandbox Code Playgroud)


Den*_*nis 1

在不同的视图/视图模型中使用会导致不同的验证行为。

因此,您有不同的视图模型。如果您无法从某些基本视图模型继承这些视图模型,请使用聚合:

public class Address {}

public class AddressViewModel1 : IDataErrorInfo
{
  private readonly Address address;
  // other stuff here
}

public class AddressViewModel2 : IDataErrorInfo
{
  private readonly Address address;
  // other stuff here
}
Run Code Online (Sandbox Code Playgroud)