次の行を置き換える必要があります:
var validator = DependencyResolver.Current.GetService<IValidator<T>>();
の
public class ValidationFactory : IValidationFactory
{
public void Validate<T>(T entity) where T : class, IEntity
{
var validator = DependencyResolver.Current.GetService<IValidator<T>>();
var result = validator.Validate(entity);
if (result.Count() > 0)
throw new BusinessServicesException(result);
}
}
それを機能させるには、System.Web.Mvcを参照する必要があります。
Unityを使用して正しいバリデーターをフックする他の解決策はありますか?
インターフェース
public interface IValidator<T> where T : class, IEntity
{
IEnumerable<ValidationResult> Validate(T entity);
}
public interface IValidationFactory
{
void Validate<T>(T entity) where T : class, IEntity;
}
1つの特定のバリデーター:
public class CanCreateOrUpdateUserValidator : IValidator<User>
{
private readonly IUnitOfWork unitOfWork;
public CanCreateOrUpdateUserValidator(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public IEnumerable<ValidationResult> Validate(User entity)
{
if (entity == null)
{
yield return new ValidationResult("");
}
else
{
// more logic
}
}
}
Unity登録:
container.RegisterType<IValidationFactory, ValidationFactory>(new ContainerControlledLifetimeManager());
container.RegisterType<IValidator<User>, CanCreateOrUpdateUserValidator>(new ContainerControlledLifetimeManager());
よろしくお願いします