| | | 1 | | #region Usings |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | #endregion |
| | | 4 | | |
| | | 5 | | namespace ZhEaIsNsAaBn.DbSeeder; |
| | | 6 | | |
| | | 7 | | /// Everything <c>Build()</c> resolved for one context: options, migrations assembly, provider and the role cache. |
| | | 8 | | internal sealed class SeedConfiguration |
| | | 9 | | { |
| | 890 | 10 | | private readonly ConcurrentDictionary<Type, SeedEntityRoles> _roles = new(); |
| | | 11 | | private readonly Lazy<LayerResponse<IReadOnlyList<SeedMigrationFiles>>> _migrations; |
| | | 12 | | private readonly Func<IServiceProvider, ISeedDatabaseProvider> _providerFactory; |
| | | 13 | | |
| | | 14 | | |
| | | 15 | | // files: migration files (resource path → text) to use instead of the assembly's embedded ones — design tools and t |
| | | 16 | | // on a folder on disk. |
| | 890 | 17 | | public SeedConfiguration( |
| | 890 | 18 | | Type contextType, |
| | 890 | 19 | | Assembly assembly, |
| | 890 | 20 | | SeedOptions options, |
| | 890 | 21 | | Func<IServiceProvider, ISeedDatabaseProvider> providerFactory, |
| | 890 | 22 | | IReadOnlyDictionary<string, string>? files = null |
| | 890 | 23 | | ) |
| | | 24 | | { |
| | | 25 | | ContextType = contextType; |
| | 890 | 26 | | Assembly = assembly; |
| | 890 | 27 | | Options = options; |
| | 890 | 28 | | _providerFactory = providerFactory; |
| | 890 | 29 | | _migrations = new Lazy<LayerResponse<IReadOnlyList<SeedMigrationFiles>>>( () => files is null |
| | 890 | 30 | | ? SeedMigration |
| | 890 | 31 | | : SeedMigration |
| | | 32 | | |
| | | 33 | | |
| | | 34 | | // Fail fast on configured types (wrong field types, a property in two roles). |
| | 1849 | 35 | | foreach ( var settings in options.Entities.Values ) { _roles[ settings.ClrType ] = SeedEntityRoles.Resolve( sett |
| | 884 | 36 | | } |
| | | 37 | | |
| | | 38 | | |
| | | 39 | | public Type ContextType { get; } |
| | | 40 | | |
| | | 41 | | /// The history key: the context's full type name (also the manifests' <c>ContextType</c>). |
| | 1755 | 42 | | public string ContextKey => ContextType.FullName ?? ContextType.Name; |
| | | 43 | | |
| | | 44 | | public Assembly Assembly { get; } |
| | | 45 | | |
| | | 46 | | public SeedOptions Options { get; } |
| | | 47 | | |
| | | 48 | | /// The embedded migrations of this context, parsed once (they can't change at run time). |
| | 525 | 49 | | public LayerResponse<IReadOnlyList<SeedMigrationFiles>> Migrations => _migrations.Value; |
| | | 50 | | |
| | | 51 | | /// "Set twice" warnings of the configuration, for the CLI. |
| | 132 | 52 | | public IReadOnlyList<string> Warnings => _roles.Values.SelectMany( r => r.Warnings ).Order( StringComparer.Ordinal ) |
| | | 53 | | |
| | | 54 | | |
| | 0 | 55 | | internal Func<IServiceProvider, ISeedDatabaseProvider> ProviderFactory => _providerFactory; |
| | | 56 | | |
| | | 57 | | |
| | | 58 | | /// The same configuration reading its migrations from <paramref name="files"/> (resource path → text) instead of th |
| | 213 | 59 | | public SeedConfiguration WithFiles( IReadOnlyDictionary<string, string> files ) => new( ContextType, Assembly, Optio |
| | | 60 | | |
| | 429 | 61 | | public ISeedDatabaseProvider CreateProvider( IServiceProvider serviceProvider ) => _providerFactory( serviceProvider |
| | | 62 | | |
| | 2307 | 63 | | public SeedEntityRoles RolesFor( Type clrType ) => _roles.GetOrAdd( clrType, t => SeedEntityRoles.Resolve( t, Option |
| | | 64 | | } |