| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder.Registrations; |
| | | 2 | | |
| | | 3 | | /// <summary>Records one context's seed settings and registers them in <see cref="Build"/>.</summary> |
| | | 4 | | internal sealed class DbSeederBuilder< TDbContext > : IDbSeederBuilder<TDbContext> |
| | | 5 | | where TDbContext : DbContext |
| | | 6 | | { |
| | | 7 | | private readonly IServiceCollection _services; |
| | 596 | 8 | | private readonly List<Action<SeedOptions>> _configure = []; |
| | | 9 | | private Assembly? _assembly; |
| | | 10 | | private Func<IServiceProvider, ISeedDatabaseProvider>? _providerFactory; |
| | | 11 | | private bool _built; |
| | | 12 | | |
| | | 13 | | |
| | 1192 | 14 | | internal DbSeederBuilder( IServiceCollection services ) => _services = services; |
| | | 15 | | |
| | | 16 | | |
| | | 17 | | public IDbSeederBuilder<TDbContext> FromAssembly( Assembly assembly ) |
| | | 18 | | { |
| | 593 | 19 | | ArgumentNullException.ThrowIfNull( assembly ); |
| | | 20 | | |
| | 593 | 21 | | _assembly = assembly; |
| | | 22 | | |
| | 593 | 23 | | return this; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public IDbSeederBuilder<TDbContext> Configure( Action<SeedOptions> configure ) |
| | | 27 | | { |
| | 581 | 28 | | ArgumentNullException.ThrowIfNull( configure ); |
| | | 29 | | |
| | 581 | 30 | | _configure.Add( configure ); |
| | | 31 | | |
| | 581 | 32 | | return this; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public IDbSeederBuilder<TDbContext> UseProvider( Func<IServiceProvider, ISeedDatabaseProvider> providerFactory ) |
| | | 36 | | { |
| | 593 | 37 | | ArgumentNullException.ThrowIfNull( providerFactory ); |
| | | 38 | | |
| | 593 | 39 | | _providerFactory = providerFactory; |
| | | 40 | | |
| | 593 | 41 | | return this; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public IServiceCollection Build() |
| | | 45 | | { |
| | 602 | 46 | | if ( _built ) { throw new InvalidOperationException( $"AddDbSeeder<{typeof( TDbContext ).Name}>().Build() was al |
| | | 47 | | |
| | 599 | 48 | | if ( _assembly is null ) { throw new InvalidOperationException( $"AddDbSeeder<{typeof( TDbContext ).Name}>() nee |
| | | 49 | | |
| | 596 | 50 | | if ( _providerFactory is null ) { throw new InvalidOperationException( $"AddDbSeeder<{typeof( TDbContext ).Name} |
| | | 51 | | |
| | 590 | 52 | | if ( _services.Any( d => d.ServiceType == typeof( SeedContextRegistration ) && d.ImplementationInstance is SeedC |
| | | 53 | | { |
| | 3 | 54 | | throw new InvalidOperationException( $"AddDbSeeder<{typeof( TDbContext ).Name}>() is registered twice." ); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | |
| | 587 | 58 | | var options = new SeedOptions(); |
| | | 59 | | |
| | 2917 | 60 | | foreach ( var configure in _configure ) { configure( options ); } |
| | | 61 | | |
| | | 62 | | |
| | 587 | 63 | | var configuration = new SeedConfiguration( typeof( TDbContext ), _assembly, options, _providerFactory ); |
| | | 64 | | |
| | 581 | 65 | | _services.AddSingleton( new SeedContextRegistration( configuration ) ); |
| | 581 | 66 | | _services.AddScoped<ISeedMigrator<TDbContext>>( serviceProvider => new SeedMigrator<TDbContext>( configuration, |
| | 581 | 67 | | configuration. |
| | 581 | 68 | | () => SeedCont |
| | | 69 | | |
| | 581 | 70 | | _built = true; |
| | | 71 | | |
| | 581 | 72 | | return _services; |
| | | 73 | | } |
| | | 74 | | } |