| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="GeneratorServiceCollectionExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 9 | | using MyNet.Generator; |
| | | 10 | | using MyNet.Generator.Facade; |
| | | 11 | | using MyNet.Text.Randomize; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 14 | | namespace MyNet.Text; |
| | | 15 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 16 | | |
| | | 17 | | public static class GeneratorServiceCollectionExtensions |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Registers random generator services in DI and aligns static facades with the same singleton instance. |
| | | 21 | | /// </summary> |
| | | 22 | | public static IServiceCollection AddRandomGenerator(this IServiceCollection services) |
| | | 23 | | { |
| | 4 | 24 | | services.TryAddSingleton<IRandomSource, SystemRandomSource>(); |
| | 4 | 25 | | services.TryAddSingleton<IRandomGenerator, DefaultRandomGenerator>(); |
| | 4 | 26 | | services.TryAddSingleton<ITextRandomGenerator>(TextRandomGenerator.Current); |
| | | 27 | | |
| | 4 | 28 | | services.TryAddSingleton(provider => |
| | 4 | 29 | | { |
| | 4 | 30 | | RandomGenerator.Current = provider.GetRequiredService<IRandomGenerator>(); |
| | 4 | 31 | | return RandomGenerator.Current; |
| | 4 | 32 | | }); |
| | | 33 | | |
| | 4 | 34 | | return services; |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |