Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 954887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:17:19+00:00 2026-05-16T00:17:19+00:00

I have a ViewModel which encapsulates a datasource which is a domain object. The

  • 0

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”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:17:20+00:00Added an answer on May 16, 2026 at 12:17 am

    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);          
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DatePicker working on a View which also has a ViewModel associated
I have senario in which one view and view has binding with multiple ViewModel.
I have a ViewModel class which has large number of properties(Say 50). Once the
I have my ViewModel on which I've defined my properties; one of them is
I have ViewModel, Which has a ObservableCollection[Employee] EmpCol , now that ViewModel is bind
I have a custom viewmodel which serialized using a JsonResult. The ViewModel has some
I am trying to implement Repository pattern in JavaScript. I have ViewModel which i
I have a viewmodel which is subscribing to the event NavigationCompletedEvent. This viewmodel need
I have a ViewModel which exposes the string property PageToolBarVisible which can be true
I have this block of xaml and I made a ViewModel which contains a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.