Skip to content

Dependency injection

Martijn Bodeman edited this page May 10, 2023 · 24 revisions

The following IoC integration packages are available:

Microsoft.Extensions.DependencyInjection

Install package IbanNet.DependencyInjection.ServiceProvider

Add IbanNet services with defaults:

services.AddIbanNet();

Or add IbanNet services with configuration:

services
    .AddIbanNet(opts => opts
        .UseRegistryProvider(new MyCustomRegistryProvider())
        .WithRule<MyCustomRule>()
    );

Autofac

Install package IbanNet.DependencyInjection.Autofac

Register IbanNet services with defaults:

container.RegisterIbanNet();

Or register IbanNet services with configuration:

container
    .RegisterIbanNet(opts => opts
        .UseRegistryProvider(new MyCustomRegistryProvider())
        .WithRule<MyCustomRule>()
    );

Available services

Both integrations provide the following services:

Service lifetime

The validator is designed to be used as a singleton, to reduce initialization time required for each individual validation. When the validator is first used, it loads the country rules once and for each country code that a validation is executed for, the rules are prepared and cached. This allows higher throughput of validations which is especially beneficial in web API's.

Avoid creating an instance of IbanValidator upon each individual validation. If you need further proof, look at the benchmark results.