你的模型类不应该是依赖注入的一部分。他们也不应该对自己的验证负责(尽管用验证属性来装饰它们——这只是元数据——就可以了)。
而是定义一个适当的抽象来进行验证。例如,定义这个抽象:
public interface IValidator<T>
{
ValidationResult Validate(T instance);
}
通过这种方式,您可以针对特定类型对 IValidator<T> 接口进行零个、一个或多个实现,并且您可以使用 Autofac 非常有效地注册这些。
当一个类型没有验证时,你可以让容器传回一个默认的-empty-实现:
// Implementation of the Null Object pattern
public class EmptyValidator<T> : IValidator<T>
{
public ValidationResult Validate(T instance)
{
return ValidationResult.ValidResult;
}
}
当一个类型定义了多个验证器时,您可以将它们包装在一个组合中:
// Implementation of the Composite pattern
public class CompositeValidator<T> : IValidator<T>
{
private readonly IEnumerable<Validator<T>> col;
public CompositeValidator(IEnumerable<Validator<T>> col)
{
this.col = col;
}
public ValidationResult Validate(T instance)
{
ValidationResult total = ValidationResult.ValidResult;
foreach (var validator in this.col)
{
var result = validator.Validate(instance);
total = ValidationResult.Append(total, result);
}
return total;
}
}
不要将IValidator<T> 直接注入到您的Web API 控制器中,而是创建一个环绕您的IRepository<T> 接口的装饰器。这样您就可以添加验证行为,而无需更改您的存储库。这样的实现可能如下所示:
public class ValidationRepositoryDecorator<T>
: IRepository<T>
{
private readonly IRepository<T> decorated;
private readonly IValidator<T> validator;
public ValidationRepositoryDecorator(
IRepository<T> decorated,
IValidator<T> validator)
{
this.decorated = decorated;
this.validator = validator;
}
public void Save(T instance)
{
var result = this.validator.Validate(instance);
if (!results.IsValid)
new ValidationException(result);
this.decorated.Save(instance);
}
}
Autofac 允许你为你注册装饰器。