/// <summary> /// Registers IbanNet services. /// </summary> /// <param name="services">The service collection.</param> /// <param name="preserveStaticValidator"><see langword="true" /> to preserve the static validator in <see cref="Iban.Validator" />, or <see langword="false" /> to replace with the configured instance.</param> /// <returns>The service collection extended with IbanNet services.</returns> public static IServiceCollection AddIbanNet(this IServiceCollection services, bool preserveStaticValidator) { if (services is null) { throw new ArgumentNullException(nameof(services)); } services.AddOptions(); services.TryAddTransient <DependencyResolverAdapter, ServiceProviderDependencyResolverAdapter>(); services.TryAddTransient <IIbanParser, IbanParser>(); services.TryAddSingleton <IIbanValidator>(s => { IbanValidatorOptions options = s.GetRequiredService <IOptions <IbanValidatorOptions> >().Value; var validator = new IbanValidator(options); if (!preserveStaticValidator) { Iban.Validator = validator; } return(validator); }); return(services); }
public void Given_rule_is_configured_via_factory_and_serviceCollection_it_should_add_instance_to_rule_collection() { var configuredRule = new TestValidationRule(); IServiceProvider providedServiceProvider = null; IIbanNetOptionsBuilder returnedBuilder = _builder .WithRule(s => { providedServiceProvider = s; return(configuredRule); }); // Act IServiceProvider services = _serviceCollection.BuildServiceProvider(); // Assert IbanValidatorOptions opts = services.GetService <IOptions <IbanValidatorOptions> >().Value; opts.Should() .HaveRule <IIbanValidationRule>() .And.HaveCount(1) .And.Subject.Single() .Should() .BeSameAs(configuredRule); providedServiceProvider.Should().NotBeNull(); returnedBuilder.Should().BeSameAs(_builder); }
public void Given_rule_is_configured_via_factory_and_serviceCollection_it_should_add_instance_to_rule_collection() { var configuredRule = new TestValidationRule(); IComponentContext providedComponentContext = null; IIbanNetOptionsBuilder returnedBuilder = _builder .WithRule(ctx => { providedComponentContext = ctx; return(configuredRule); }); // Act IContainer container = _containerBuilder.Build(); // Assert IbanValidatorOptions opts = container.Resolve <IbanValidatorOptions>(); opts.Should() .HaveRule <IIbanValidationRule>() .And.HaveCount(1) .And.Subject.Single() .Should() .BeSameAs(configuredRule); providedComponentContext.Should().NotBeNull(); returnedBuilder.Should().BeSameAs(_builder); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultValidationRuleResolver" />. /// </summary> public DefaultValidationRuleResolver(IbanValidatorOptions options) { _options = options ?? throw new ArgumentNullException(nameof(options)); _structureValidationFactory = new CachedStructureValidationFactory( new CompositeStructureValidationFactory( (options.Registry ?? throw new ArgumentException(Resources.ArgumentException_Registry_is_required, nameof(options))).Providers ) ); }
public DefaultValidationRuleResolverTests() { var registryMock = new Mock <IIbanRegistry>(); registryMock.Setup(m => m.Providers).Returns(new List <IIbanRegistryProvider>()); _options = new IbanValidatorOptions { Registry = registryMock.Object }; _sut = new DefaultValidationRuleResolver(_options); }
private void VerifyCalled(Action <IbanValidatorOptionsAssertions> should) { Func <Action <DependencyResolverAdapter, IbanValidatorOptions>, bool> assert = configureAction => { var opts = new IbanValidatorOptions(); DependencyResolverAdapter adapter = new Mock <DependencyResolverAdapter>().Object; configureAction(adapter, opts); should(opts.Should()); return(true); }; Subject.Verify(m => m.Configure(It.Is <Action <DependencyResolverAdapter, IbanValidatorOptions> >(action => assert(action)))); }
protected override void Load(ContainerBuilder builder) { base.Load(builder); builder.RegisterSource(new RuleRegistrationSource()); IRegistrationBuilder <IbanValidatorOptions, ConcreteReflectionActivatorData, SingleRegistrationStyle> optionsRegistration = builder .RegisterType <IbanValidatorOptions>() .AsSelf() .InstancePerDependency(); foreach (Action <IActivatingEventArgs <IbanValidatorOptions> > handler in _ibanValidatorOptionsHandlers) { optionsRegistration.OnActivating(handler); } builder .RegisterType <AutofacDependencyResolverAdapter>() .As <DependencyResolverAdapter>() .IfNotRegistered(typeof(DependencyResolverAdapter)) .InstancePerDependency(); builder .RegisterType <IbanParser>() .As <IIbanParser>() .IfNotRegistered(typeof(IIbanParser)) .InstancePerDependency(); builder .Register(context => { IbanValidatorOptions options = context.Resolve <IbanValidatorOptions>(); var validator = new IbanValidator(options); if (!_preserveStaticValidator) { Iban.Validator = validator; } return(validator); }) .As <IIbanValidator>() .IfNotRegistered(typeof(IIbanValidator)) .SingleInstance(); }
public static IbanValidatorOptionsAssertions Should(this IbanValidatorOptions instance) { return(new IbanValidatorOptionsAssertions(instance)); }