2

我有一个 ViewModel,它封装了一个域对象的数据源。域具有在域中定义的验证规则,但被 ViewModel 重用以向用户提供信息。

视图模型:

internal class RatedValuesViewModel : FluentDataErrorInfo
{
    public RatedValuesViewModel()
        : base(new RatedValuesViewModelValidator())
    {
    }

    public RatedValues DataSource { get; set; }

    ...
}

域对象:

class RatedValues
{
    public double? Head
    {
        get; set;
    }

    public double? DeltaPressure
    {
        get; set;
    }

    ...
}

我们将要执行的操作上下文中的域对象规则:

class GeneratePlotPumpCurvesRatedValuesValidationRules : AbstractValidator<RatedValues>
{
    public GeneratePlotPumpCurvesRatedValuesValidationRules()
    {
        Custom(itemToValidate => !AtLeastOneParameterSpecified(itemToValidate) ? new ValidationFailure(string.Empty, "You must specify at least one rated value.") : null);

        RuleFor(item => item.Head).Must(head => head == null).When(item => item.DeltaPressure != null).WithMessage("You can not specify both head and delta pressure.");
        RuleFor(item => item.DeltaPressure).Must(dp => dp == null).When(item => item.Head != null).WithMessage("You can not specify both head and delta pressure.");
    }

...
}

最后:重用域中验证规则的视图模型的验证规则:

class RatedValuesViewModelValidator : AbstractValidator<RatedValuesViewModel>
{
    private readonly GeneratePlotPumpCurvesRatedValuesValidationRules _generatePlotPumpCurvesRatedValuesValidationRules = new GeneratePlotPumpCurvesRatedValuesValidationRules();

    public RatedValuesViewModelValidator()
    {
        RuleFor(viewModel => viewModel.DataSource).SetValidator(_generatePlotPumpCurvesRatedValuesValidationRules);

        Custom(viewModel =>
                   {
                       if (viewModel.DataSource == null)
                           return null;

                       ValidationResult validationResult = _generatePlotPumpCurvesRatedValuesValidationRules.Validate(viewModel.DataSource, viewModel.DataSource.GetPropertyName(vm => vm.Head));

                       return !validationResult.IsValid ? new ValidationFailure(viewModel.GetPropertyName(vm => vm.Head), CombineErrors(validationResult.Errors)) : null;
                   } );
    }

    private static string CombineErrors(IEnumerable<ValidationFailure> errors)
    {
        StringBuilder combinedErrors = new StringBuilder();

        errors.ForEach(error => combinedErrors.AppendLine(error.ErrorMessage));

        return combinedErrors.ToString();
    }
}

在视图模型的验证类中,我重用为属性“头”定义的验证规则,并将它们公开为视图模型属性“头”的规则。正如你所看到的,对于我将经常做的事情,有很多代码。我可以使用任何流畅的验证框架功能来设置这个“验证属性投影”吗?

4

1 回答 1

0

我没有在框架中找到任何方法来做到这一点,但我创建了一个扩展方法,它使我能够做我想做的单线工作。

例子:

class PumpCurveViewModelValidationRules : AbstractValidator<PumpCurveViewModel>
{
    readonly ConstantPumpCurveParametersValidationRules _constantParametersValidator = new ConstantPumpCurveParametersValidationRules();

    public PumpCurveViewModelValidationRules()
    {
        this.AddPropertyProjectionRule<PumpCurveViewModelValidationRules, PumpCurveViewModel, ConstantPumpParameters>(c => c.Speed, vm => vm.CurveSpeed, vm => vm.DataSource.ConstantPumpParameters, _constantParametersValidator);          
    }
}
于 2010-07-29T10:58:00.833 回答