Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Want to pass parameters to a Validator yet be Efficient

$
0
0
Hi

I think it depends on the scenario and what you're trying to achieve.

If your validator needs to make use of some sort of service object to perform validation, then I would tend to inject this through the constructor using an IoC container. Once instantiated, a validator is thread-safe and makes no side-effects, so you can have a single validator, but you'd need to ensure that any dependencies are thread-safe too.

If the parameter that you need to pass is specific to a particular validation request, then you can either:
  • pass it in as a property of the object being validated
  • pass it in via the constructor, but you'd need to instantiate a new validator every time. There is a performance hit for doing this (because of the expression tree compilation), but I wouldn't really worry about this - there are typically much slower things in an application (such as db requests) which are far more noticeable. If you think this might be an issue, then I'd suggest running some profiling tools.
Jeremy

Viewing all articles
Browse latest Browse all 1917

Trending Articles