SHARE
TWEET

Untitled

a guest Jun 20th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [FluentValidation.Attributes.Validator(typeof(LeagueValidator))]
  2. public class LeagueViewModel
  3. {
  4.     public string LeagueName { get; set; }
  5.     public DateTime SeasonBeginDate { get; set; }
  6.     public DateTime SeasonEndDate { get; set; }
  7.  
  8.     public List<TeamViewModel> TeamViewModels { get; set; }
  9. }
  10.      
  11. public class LeagueValidator : AbstractValidator<LeagueViewModel>
  12. {
  13.     private readonly ILeagueService _leagueService;
  14.  
  15.     public LeagueValidator(ILeagueService leagueService)
  16.     {
  17.         _leagueService = leagueService;
  18.  
  19.         RuleFor(x => x.SeasonEndDate)
  20.             .NotNull()
  21.             .GreaterThan(x => x.SeasonBeginDate)
  22.             .WithMessage("Season End Date must be later than Season Begin Date.");
  23.     }
  24. }
  25.      
  26. public class LeagueValidatorFactory : ValidatorFactoryBase
  27. {
  28.     private readonly Container _container;
  29.  
  30.     public LeagueValidatorFactory(Container container)
  31.     {
  32.         _container = container;
  33.     }
  34.  
  35.     public override IValidator CreateInstance(Type validatorType)
  36.     {
  37.         return _container.GetInstance<LeagueValidator>();
  38.     }
  39. }
  40.      
  41. private static void RegisterValidators(Container container)
  42. {
  43.     DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
  44.  
  45.     var leagueValidatorProvider =
  46.         new FluentValidationModelValidatorProvider(new LeagueValidatorFactory(container));
  47.     leagueValidatorProvider.AddImplicitRequiredValidator = false;
  48.     ModelValidatorProviders.Providers.Add(leagueValidatorProvider);
  49.  
  50.     container.Register<LeagueValidator>();
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top