question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any optimization for When + Unless on same condition?

See original GitHub issue

System Details

  • FluentValidation version: 8.0

Issue Description

Hi,

I’m looking to define several rules for when a condition is met, and several other rules for when that same condition is not met.

To do that, my code is currently:

WhenAsync(CheckConditionAsync, () => { /* rules */ });
UnlessAsync(CheckConditionAsync, () => { /* rules */ });

Checking the condition is an I/O bound job and therefore asynchronous, so for performance reasons I’d like to avoid doing that twice. Is there some way of doing that with the current version of FluentValidation?

If there isn’t, but maybe also if there is, could it be possible to extend the overloads of When to include a 3rd parameter that would take another action for when the condition is not met? Or maybe to follow the fluent paradigm, make When return some interface that can chain an Otherwise method.

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Nov 14, 2018

Looks like this will be easier than I thought, will aim to get it into 8.1

First part of this is implemented in b897d3b. Now calls to When, WhenAsync, Unless and UnlessAsync on the root validator will cache the result of the predicate the first time it’s executed, so subsequent rules will recieve the cached value.

Will next look to see about chaining on an Otherwise method.

1reaction
JeremySkinnercommented, Oct 29, 2018

I’ll try and get to it for 8.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Optimizing conditional code inside nested for-loops
I would always go for the cleaner code over optimization (because it will be close to none if not none). The compiler will...
Read more >
Multiple if condition optimization
Often I find conditional statements such as: if (life_max < max): if (expand): do life_max = max else: if (life_max > max) and...
Read more >
Optimization problem with if condition as constraint
Assume finite bounds L≤bn−cn≤U. Introduce binary variables xn, yn, and zn, and impose the following linear constraints: xn+yn+zn=1for all ...
Read more >
If and Only If. Performance Cost of Using If Statements…
I was looping over every element in a 2D array and increasing the count if the value was odd. Quite simple, and in...
Read more >
Introduction to Optimization, and Optimality Conditions for ...
A necessary condition for local optimality is a statement of the form: “if. ¯ x must satisfy . . . ” Such a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found