I have just been looking into this myself using unity and MVC 4.
What I have seen is that if you keep them as Transient objects. FluentValidation will create a new validation object for every property that is been validated. So some caching is required.
For my caching I have looked at Per Request caching of the validator. This works well as all the dependent components are Per Request. (Per Request is custom code which stores a child Unity Container on the HttpContext.Current.Items collections with a HTTP module that destroys/dispose the Child Container at the end of the request)
To choose between Per Request and Singleton instants of the validator comes down to how your using it and what type of dependencies it has and the capablities of the IoC Continer.
With unity you can create a singleton validator and inject the service factory using a function ie Func serviceFunc.
In my case each time I call the serviceFunc, the Unity ChildContiner 'service' is retrieved. Thus I can still have my 'validator' defined with a ContainerControlledLifetimeManager(singleton) and have the 'service' defined with HierarchicalLifetimeManager(Per Request).
A downside of this is each time the serviceFunc is invoked it needs to check and retrieves the service from the child container. This will be the most likely reason why I will do back to 'per request')
I have just updated my code to use serviceFunc and will test it today. I believe it will be trial an error to find the correct solution for your aplications.
Below is the validtion factory I'm using - instead of using the unity container (as most examples on the web do), I'm injecting the IDependencyResolver
into it and using that to resolve my validator objects.
public class ValidatorFactory : IValidatorFactory
{
private readonly IDependencyResolver _dependencyResolver;
// taken from the attribute Validation factory
public ValidatorFactory(IDependencyResolver dependencyResolver)
{
_dependencyResolver = dependencyResolver;
}
/// <summary>
/// Gets a validator for the appropriate type.
///
/// </summary>
public IValidator<T> GetValidator<T>()
{
return (IValidator<T>)this.GetValidator(typeof(T));
}
/// <summary>
/// Gets a validator for the appropriate type.
///
/// </summary>
public virtual IValidator GetValidator(Type type)
{
if (type == (Type)null)
return (IValidator)null;
var validatorAttribute = (ValidatorAttribute)Attribute.GetCustomAttribute((MemberInfo)type, typeof(ValidatorAttribute));
if (validatorAttribute == null || validatorAttribute.ValidatorType == (Type) null)
{
return (IValidator) null;
}
else
{
return _dependencyResolver.GetService(validatorAttribute.ValidatorType) as IValidator;
}
}
}