| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ServiceCollectionExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 10 | | using MyNet.Fakers.Contacts; |
| | | 11 | | using MyNet.Fakers.Geography; |
| | | 12 | | using MyNet.Fakers.Identity; |
| | | 13 | | using MyNet.Fakers.Internet; |
| | | 14 | | using MyNet.Fakers.Media; |
| | | 15 | | using MyNet.Fakers.Text; |
| | | 16 | | using MyNet.Globalization; |
| | | 17 | | using MyNet.Text; |
| | | 18 | | |
| | | 19 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 20 | | namespace MyNet.Fakers; |
| | | 21 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 22 | | |
| | | 23 | | public static class ServiceCollectionExtensions |
| | | 24 | | { |
| | | 25 | | extension(IServiceCollection services) |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// <para> |
| | | 29 | | /// Adds the MyNet.Fakers framework with all dependencies configured. |
| | | 30 | | /// This method handles: |
| | | 31 | | /// - Random number generation |
| | | 32 | | /// - Localization infrastructure |
| | | 33 | | /// - Provider registration for all cultures |
| | | 34 | | /// - All faker implementations |
| | | 35 | | /// - Configuration options. |
| | | 36 | | /// </para> |
| | | 37 | | /// <para>Call this once during application startup. It integrates with <see cref="Globalization.ServiceCollecti |
| | | 38 | | /// </summary> |
| | | 39 | | /// <returns>The updated IServiceCollection instance.</returns> |
| | | 40 | | public IServiceCollection AddFakers() |
| | | 41 | | { |
| | | 42 | | // Ensure localization infrastructure is registered (with fallback to defaults if not already present) |
| | 6 | 43 | | services.AddLocalization(); |
| | | 44 | | |
| | | 45 | | // Random number generation |
| | 6 | 46 | | services.AddRandomGenerator(); |
| | | 47 | | |
| | | 48 | | // Contacts |
| | 6 | 49 | | services.TryAddSingleton<IMailFaker, MailFaker>(); |
| | 6 | 50 | | services.TryAddSingleton<IPhoneFaker, PhoneFaker>(); |
| | 6 | 51 | | services.AddLocalizationService<IPhoneFakerProvider>((_, x) => ResxPhoneFakerProvider.Create(x)); |
| | | 52 | | |
| | | 53 | | // Geography |
| | 6 | 54 | | services.TryAddSingleton<ICountryFaker, CountryFaker>(); |
| | 6 | 55 | | services.TryAddSingleton<IStreetFaker, StreetFaker>(); |
| | 6 | 56 | | services.TryAddSingleton<IAddressFaker, AddressFaker>(); |
| | 6 | 57 | | services.AddLocalizationService<IAddressFakerProvider>((_, x) => ResxAddressFakerProvider.Create(x)); |
| | | 58 | | |
| | | 59 | | // Identity |
| | 6 | 60 | | services.TryAddSingleton<IIdentityFaker, IdentityFaker>(); |
| | 6 | 61 | | services.TryAddSingleton<INameFaker, NameFaker>(); |
| | 6 | 62 | | services.AddLocalizationService<INameFakerProvider>((_, x) => ResxNameFakerProvider.Create(x)); |
| | | 63 | | |
| | | 64 | | // Internet |
| | 6 | 65 | | services.TryAddSingleton<IDomainFaker, DomainFaker>(); |
| | 6 | 66 | | services.AddLocalizationService<IDomainFakerProvider>((_, x) => ResxDomainFakerProvider.Create(x)); |
| | | 67 | | |
| | | 68 | | // Media |
| | 6 | 69 | | services.TryAddSingleton<IColorFaker, ColorFaker>(); |
| | | 70 | | |
| | | 71 | | // Text |
| | 6 | 72 | | services.TryAddSingleton<ITextFaker, TextFaker>(); |
| | | 73 | | |
| | | 74 | | // Main IFakeDataGenerator facade |
| | 6 | 75 | | services.TryAddSingleton<IFakeDataGenerator, FakeDataGenerator>(); |
| | | 76 | | |
| | 6 | 77 | | return services; |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | extension(IServiceProvider serviceProvider) |
| | | 82 | | { |
| | | 83 | | /// <summary> |
| | | 84 | | /// Configures the static <see cref="Static.Faker"/> class to use the <see cref="IFakeDataGenerator"/> instance |
| | | 85 | | /// </summary> |
| | | 86 | | /// <returns>The updated service provider.</returns> |
| | | 87 | | public IServiceProvider UseFakers() |
| | | 88 | | { |
| | 0 | 89 | | Static.Faker.Configure(serviceProvider.GetRequiredService<IFakeDataGenerator>()); |
| | | 90 | | |
| | 0 | 91 | | return serviceProvider; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |