Glue Code for Validation

Wouldn’t it be nice to be able to just call

Validating a DTO
1
var report = myDto.Validate();

without sacrificing extensibillity, testabillity, etc? What about at this,

An extension method that calls into the IoC container
1
2
3
4
5
6
7
8
9
10
11
public static class ObjectExtensions
{
  public static ValidationReport Validate(this Subject subject)
  {
      return Container.GetInstance<IValidator<Subject>>().Validate(subject);
  }
  public static ValidationReport Validate(this Subject subject, string validatorName)
  {
      return Container.GetInstance<IValidator<Subject>>(validatorName).Validate(subject);
  }
}

where Container is just a simple static gateway for an IoC-container?

Comments