views:

73

answers:

1

I have a ViewModel which encapsulates a datasource which is a domain object. The domain has validation rules which are defined in the domain, but reused by the ViewModel to provide information to the user.

The viewmodel:

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

    public RatedValues DataSource { get; set; }

    ...
}

The domain object:

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

    public double? DeltaPressure
    {
        get; set;
    }

    ...
}

The domain object rules in the context of the operation we are going perform:

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.");
    }

...
}

And finally: the validation rules for the view model which reuses the validation rules in the domain:

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();
    }
}

In the validation class for the viewmodel I reuse the validation rules defined for the property "Head" and expose them as rules for the viewmodel property "Head". As you can see, there is quite a lot of code for something I will be doing quite often. Can I use any fluent validation framework functionality to set up this "validation property projection"?

A: 

I didn't find any way to do this in the framework, but I've created an extension methods which enables me to do what I want to do as a one-liner.

Example:

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);          
    }
}
Marius