| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="Faker.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Threading; |
| | | 11 | | using MyNet.Fakers.Contacts; |
| | | 12 | | using MyNet.Fakers.Geography; |
| | | 13 | | using MyNet.Fakers.Identity; |
| | | 14 | | using MyNet.Fakers.Internet; |
| | | 15 | | using MyNet.Fakers.Media; |
| | | 16 | | using MyNet.Fakers.Text; |
| | | 17 | | using MyNet.Generator.Facade; |
| | | 18 | | using MyNet.Globalization.Localization.Providers; |
| | | 19 | | using MyNet.Text.Randomize; |
| | | 20 | | |
| | | 21 | | namespace MyNet.Fakers.Static; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Provides a static class for generating fake data across various categories such as names, phones, mails, countries, |
| | | 25 | | /// </summary> |
| | | 26 | | public static class Faker |
| | | 27 | | { |
| | 0 | 28 | | private static IFakeDataGenerator _service = CreateDefault(); |
| | | 29 | | |
| | | 30 | | private static int _configured; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Configures the <see cref="Faker"/> class to use a custom implementation of the <see cref="IFakeDataGenerator"/>. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="generator">The custom implementation of the <see cref="IFakeDataGenerator"/> to be used by the <see |
| | | 36 | | /// <exception cref="ArgumentNullException">Thrown if the provided <paramref name="generator"/> is null.</exception> |
| | | 37 | | /// <exception cref="InvalidOperationException">Thrown if the <see cref="Faker"/> class has already been configured. |
| | | 38 | | public static void Configure(IFakeDataGenerator generator) |
| | | 39 | | { |
| | 0 | 40 | | ArgumentNullException.ThrowIfNull(generator); |
| | | 41 | | |
| | 0 | 42 | | if (Interlocked.Exchange(ref _configured, 1) == 1) |
| | 0 | 43 | | throw new InvalidOperationException("Faker has already been configured."); |
| | | 44 | | |
| | 0 | 45 | | _service = generator; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the current instance of the <see cref="IFakeDataGenerator"/> being used by the <see cref="Faker"/> class. T |
| | | 50 | | /// </summary> |
| | 0 | 51 | | public static INameFaker Names => _service.Names; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the current instance of the <see cref="IPhoneFaker"/> being used by the <see cref="Faker"/> class. This ins |
| | | 55 | | /// </summary> |
| | 0 | 56 | | public static IPhoneFaker Phones => _service.Phones; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets the current instance of the <see cref="IMailFaker"/> being used by the <see cref="Faker"/> class. This inst |
| | | 60 | | /// </summary> |
| | 0 | 61 | | public static IMailFaker Mails => _service.Mails; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets the current instance of the <see cref="ICountryFaker"/> being used by the <see cref="Faker"/> class. This i |
| | | 65 | | /// </summary> |
| | 0 | 66 | | public static ICountryFaker Countries => _service.Countries; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets the current instance of the <see cref="IAddressFaker"/> being used by the <see cref="Faker"/> class. This i |
| | | 70 | | /// </summary> |
| | 0 | 71 | | public static IAddressFaker Addresses => _service.Addresses; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets the current instance of the <see cref="IStreetFaker"/> being used by the <see cref="Faker"/> class. This in |
| | | 75 | | /// </summary> |
| | 0 | 76 | | public static IStreetFaker Streets => _service.Streets; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets the current instance of the <see cref="IIdentityFaker"/> being used by the <see cref="Faker"/> class. This |
| | | 80 | | /// </summary> |
| | 0 | 81 | | public static IIdentityFaker Identities => _service.Identities; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the current instance of the <see cref="ITextFaker"/> being used by the <see cref="Faker"/> class. This inst |
| | | 85 | | /// </summary> |
| | 0 | 86 | | public static ITextFaker Texts => _service.Texts; |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the current instance of the <see cref="IDomainFaker"/> being used by the <see cref="Faker"/> class. This in |
| | | 90 | | /// </summary> |
| | 0 | 91 | | public static IDomainFaker Domains => _service.Domains; |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the current instance of the <see cref="IColorFaker"/> being used by the <see cref="Faker"/> class. This ins |
| | | 95 | | /// </summary> |
| | 0 | 96 | | public static IColorFaker Colors => _service.Colors; |
| | | 97 | | |
| | | 98 | | private static FakeDataGenerator CreateDefault() |
| | | 99 | | { |
| | 0 | 100 | | var nameFaker = new NameFaker(RandomGenerator.Current, new CultureScopedServiceSource<INameFakerProvider>(ResxNa |
| | 0 | 101 | | var phoneFaker = new PhoneFaker(TextRandomGenerator.Current, RandomGenerator.Current, new CultureScopedServiceSo |
| | 0 | 102 | | var countryFaker = new CountryFaker(RandomGenerator.Current); |
| | 0 | 103 | | var textFaker = new TextFaker(RandomGenerator.Current); |
| | 0 | 104 | | var streetFaker = new StreetFaker(RandomGenerator.Current, new CultureScopedServiceSource<IAddressFakerProvider> |
| | 0 | 105 | | var addressFaker = new AddressFaker(TextRandomGenerator.Current, RandomGenerator.Current, new CultureScopedServi |
| | 0 | 106 | | var identityFaker = new IdentityFaker(RandomGenerator.Current, textFaker); |
| | 0 | 107 | | var domainFaker = new DomainFaker(RandomGenerator.Current, new CultureScopedServiceSource<IDomainFakerProvider>( |
| | 0 | 108 | | var colorFaker = new ColorFaker(RandomGenerator.Current); |
| | 0 | 109 | | var mailFaker = new MailFaker(RandomGenerator.Current, identityFaker, domainFaker); |
| | | 110 | | |
| | 0 | 111 | | return new FakeDataGenerator( |
| | 0 | 112 | | nameFaker, |
| | 0 | 113 | | phoneFaker, |
| | 0 | 114 | | mailFaker, |
| | 0 | 115 | | countryFaker, |
| | 0 | 116 | | addressFaker, |
| | 0 | 117 | | streetFaker, |
| | 0 | 118 | | identityFaker, |
| | 0 | 119 | | textFaker, |
| | 0 | 120 | | domainFaker, |
| | 0 | 121 | | colorFaker); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// A simple implementation of the <see cref="ICultureScopedServiceSource{TService}"/> interface that always returns |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="instance">The instance of the service to be returned.</param> |
| | | 128 | | /// <typeparam name="TService">The type of the service.</typeparam> |
| | | 129 | | private sealed class CultureScopedServiceSource<TService>(TService instance) : ICultureScopedServiceSource<TService> |
| | | 130 | | where TService : class, ICultureScoped |
| | | 131 | | { |
| | | 132 | | /// <inheritdoc/> |
| | 0 | 133 | | public TService Get(CultureInfo? culture = null) => instance; |
| | | 134 | | |
| | | 135 | | /// <inheritdoc/> |
| | | 136 | | public bool TryGet(CultureInfo? culture, [NotNullWhen(true)] out TService? service) |
| | | 137 | | { |
| | 0 | 138 | | service = instance; |
| | 0 | 139 | | return true; |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |