简体   繁体   English

在两个不同模型上共享验证逻辑

[英]Shared validation logic over two different models

Update: I've been fiddling around with a solution and I'm not quite sure if we can even do it this way. 更新:我一直在摆弄一个解决方案,但我不确定我们是否可以这样做。 I will update the question as soon as I have a better understanding of what I want :-) 一旦我对自己想要的有了更好的了解,我将立即更新问题:-)


Our current project uses WPF along with the MVVM approach. 我们当前的项目使用WPF和MVVM方法。 Our application will have several editors, each with its own model. 我们的应用程序将有几个编辑器,每个编辑器都有自己的模型。

The view will look like this: 该视图将如下所示:

总览

Each editor has its own model/viewmodel/model. 每个编辑器都有自己的模型/视图模型/模型。 The parent view's (which holds all sub editors) model contains the data from which all sub editors can be feeded. 父视图(包含所有子编辑器)模型包含可从其馈送所有子编辑器的数据。

Since the user should be able to save its current state at any time, we need to be able to validate the existing data as well (because the user might have entered invalid data). 由于用户应该能够随时保存其当前状态,因此我们还需要能够验证现有数据(因为用户可能输入了无效数据)。 So we need the validation in two places: 因此,我们需要在两个地方进行验证:

  • when the user enters data 用户输入数据时
  • when the data is loaded again after starting the application 启动应用程序后再次加载数据时

Since we do not want to convert the data into each sub editor model just to get the validation result, the parent model should be able to be validated as well. 由于我们不想将数据转换为每个子编辑器模型只是为了获得验证结果,因此父模型也应该也可以被验证。

The only way I found to achieve this, is to introduce interfaces which basically describes the structure of the data within the class which is to be validated. 我发现实现此目标的唯一方法是引入接口,该接口基本上描述了要验证的类中的数据结构。 It might look like this: 它可能看起来像这样:

interface IValidateModel
{
  string foo;
}

class Editor1Model : IValidateModel
{
  string foo;
  int someOtherProperty;
}

class Editor2Model: IValidateModel
{
  string foo;
  byte fooBar;      
}

class ParentModel
{
  Sub subProp;
  // some other parent model properties
}

Once disadvantage is that now the way the data is structured is defined by the interface. 缺点之一是现在由接口定义数据的结构方式。

Does anyone have some experience with that problem or does even have a better solution for this? 有没有人对此问题有经验,甚至有更好的解决方案?

If you want to validate the user's input, then you should use ValidationRules on the bindings. 如果要验证用户的输入,则应在绑定上使用ValidationRules

You define them by inheriting from ValidationRule and overwriting Validate() : 您可以通过继承ValidationRule并覆盖Validate()定义它们:

public class TestRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        string test = value as string;

        if (text == null)
        {
            return new ValidationResult(false, "only strings are allowed");
        }
        else
        {
            return new ValidationResult(true, null);
        }
    }
}

In XAML you specify the namespace of the validation rules: 在XAML中,您指定验证规则的名称空间:

<Window xmlns:vr="clr-namespace:MyApp.ValidationRules"></Window>

And then you can use it on bindings like this: 然后您可以将其用于这样的绑定:

<TextBox>
    <TextBox.Text>
        <Binding Path="MyPath">
            <Binding.ValidationRules>
                <vr:TestRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM