I have to write a lot of validators, some of them are validating queries dto, and some are used for validation and other purposes. My project consists of modules encapsulated in a plug-in (a very cool solution! thx Service Stack Team!), the problem is that I sometimes forget to register the validator because the validator is created in one file and registered in another.
The problem can be solved as follows, but not all validators need to be registered so I don’t use this solution.
Plugins.Add(new ValidationFeature {
ScanAppHostAssemblies = false
});
I use attributes to register validators, but I implement this logic every time.
public class AutoregisterValidator : Attribute { }
public class ValidatorAutoregistrationFeature : IPlugin {
public void Register(IAppHost appHost) {
if (appHost.GetPlugin<ValidationFeature>() != null) {
var container = appHost.GetContainer();
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assemblie in assemblies) {
foreach (var classType in assemblie.GetTypes()) {
var autoregistration = classType.GetCustomAttribute<AutoregisterValidator>();
if (autoregistration != null && !container.Exists(classType)) {
container.RegisterValidator(classType);
}
}
}
}
}
}
Please tell me how can I do this with the built-in ServiceStack features?
Thx.