In my current project I have the following requirement:
There are certain classes (representing commands) that should be validated. The validator for each command should be contained in a nested class within that command and implement FluentValidation.IValidator.
The example command class should thus look like this:
public class MyCommand : ICommand
{
// some properties
public class Validator : AbstractValidator<MyCommand>
{
// definitions of validation rules
}
}
So the way to go should probably be like this:
load up the assembly into ArchLoader
filter all the relevant classes (in this case, classes that implement ICommand interface)
for each filtered class get its nested classes
check that for each filtered class at least one nested class satisfies some predicate
Checking whether a given type is nested is pretty simple, but I can't find a way to easily obtain any information about the relationship between nesting and nested types (both from nested and nesting type's perspective), and thus I'm stuck on step 3.
I've thrown together a query that extracts all the commands and their potential validators by joining them on IType.FullName (using MoreLINQ's LeftJoin, example below), but that's much less intuitive and requires much more work than simply querying a type for its nested types. Is there a way to get this kind of information without resorting to such hacks?
[Fact]
public void All_commands_must_have_validators()
{
static string ExtractValidatedClass(Class validatorClass)
{
if (validatorClass.IsNested)
return validatorClass.FullName.Substring(0, validatorClass.FullName.LastIndexOf("+"));
else
return null;
}
var architecture = new ArchLoader().LoadAssemblies(
typeof(MyProject.MarkerInterface).Assembly,
typeof(FluentValidation.IValidator).Assembly)
.Build();
var commandType = architecture.GetInterfaceOfType(typeof(ICommmand));
var validatorType = architecture.GetInterfaceOfType(typeof(FluentValidation.IValidator));
var commands = architecture.Classes.Where(x => x.IsAssignableTo(commandType));
var validators = architecture.Classes.Where(x => x.IsAssignableTo(validatorType));
var commandsAndValidators = commands.LeftJoin(validators,
command => command.FullName,
validator => ExtractValidatedClassName(validator),
command => new { command, validator = (Class?)null },
(command, validator) => new { command, validator })
.ToList();
commandsAndValidators.Should().AllSatisfy(x => x.validator.Should().NotBeNull());
}
In my current project I have the following requirement:
There are certain classes (representing commands) that should be validated. The validator for each command should be contained in a nested class within that command and implement
FluentValidation.IValidator
. The example command class should thus look like this:So the way to go should probably be like this:
ArchLoader
ICommand
interface)Checking whether a given type is nested is pretty simple, but I can't find a way to easily obtain any information about the relationship between nesting and nested types (both from nested and nesting type's perspective), and thus I'm stuck on step 3.
I've thrown together a query that extracts all the commands and their potential validators by joining them on
IType.FullName
(using MoreLINQ'sLeftJoin
, example below), but that's much less intuitive and requires much more work than simply querying a type for its nested types. Is there a way to get this kind of information without resorting to such hacks?