Most of the StackOverflow posts about how implementing very custom Validation logic in AbstractValidators (that access the database, check properties, check access), is frowned upon.
So I’ve implemented a custom IBusinessRuleValidator pipeline using SimpleInjectors container.RegisterCollection(typeof(IBusinessRuleValidator<>)…).
This all works great when hitting play and calling services with the /swagger-ui. I have a custom, Internal only ValidationService:
[Restrict(RequestAttributes.None)]
public class ValidationService : Service
{
public void Validate<T>(T entity)
{
var validators = TryResolve<IEnumerable<BusinessRuleValidator<T>>>().ToList();
// These will be the basic things every validator needs
// And we don't want to have to inject these into every single validators constructor
foreach (var validator in validators)
{
validator.User = SessionAs<CustomAuthUserSession>();
validator.AuthRepository = AuthRepository;
validator.DbFactory = ResolveService<IDbConnectionFactory>();
}
var errors = validators.SelectMany(x => x.Validate(entity)).ToList();
if (errors.Any())
{
throw new BusinessRuleException(errors);
}
}
}
But my first test fails at the validator.User = SessionAs(); line of code with the following error:
Message: System.NotImplementedException : This AppHost does not support accessing the current Request via a Singleton
In which the top lines of the stack trace are:
HostContext.GetCurrentRequest()
SessionFeature/GetOrCreateSession[T](ICacheClient cache, IRequest httpRequest, IResponse httpResponse)
Am I misunderstanding or misusing something here?