Open Closed

Abp v8.3 Microservice template Data seeding #8111


User avatar
0
ahmednfwela@bdaya-dev.com created

I generated a new microservice solution on the latest version of the CLI, but I noticed some patterns have changed from the old template (e.g. removal of DbMigrator project), mainly what I am asking about here is the per-service data seeder, e.g.:

public class MicroserviceNameDataSeeder : ITransientDependency
{
    private readonly ILogger<MicroserviceNameDataSeeder> _logger;

    public MicroserviceNameDataSeeder(
        ILogger<MicroserviceNameDataSeeder> logger)
    {
        _logger = logger;
    }

    public async Task SeedAsync(Guid? tenantId = null)
    {
        _logger.LogInformation("Seeding data...");
        
        //...
    }
}

As you can see, it does not use IDataSeeder anymore, is it a bad practice to use it here ? do we need to inject + call each data seed contributor manually from all the dependent modules ?

  • ABP Framework version: v8.3.0
  • ABP CLI: v0.9.1 (Beta)
  • UI Type: Angular
  • Database System: EF Core PostgreSQL
  • Tiered (for MVC) or Auth Server Separated (for Angular): Microservice solution
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

6 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can check the document to understand it better https://abp.io/docs/latest/solution-templates/microservice/database-configurations#database-migrations

  • User Avatar
    0
    ahmednfwela@bdaya-dev.com created

    I see, but I noticed that the given example in the identity service is different from the docs

    Generated from template, uses OnPreApplicationInitializationAsync

    public override async Task OnPreApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        using var scope = context.ServiceProvider.CreateScope();
        await scope.ServiceProvider
            .GetRequiredService<IdentityServiceRuntimeDatabaseMigrator>()
            .CheckAndApplyDatabaseMigrationsAsync();
    }
    

    Referenced in docs uses OnPostApplicationInitializationAsync:

    public override async Task
        OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        using var scope = context.ServiceProvider.CreateScope();
        await scope.ServiceProvider
            .GetRequiredService<IdentityServiceRuntimeDatabaseMigrator>()
            .CheckAndApplyDatabaseMigrationsAsync();
    }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    It,

    This seems to be a problem in the documentation, we will update the documentation

  • User Avatar
    0
    ahmednfwela@bdaya-dev.com created

    So which one is correct, OnPreApplicationInitializationAsync or OnPostApplicationInitializationAsync ? because the generated files contains a templates/service_nolayers template that uses OnPostApplicationInitializationAsync

    public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        using var scope = context.ServiceProvider.CreateScope();
        {{~ if config.database_provider == "ef" ~}}
        await scope.ServiceProvider
            .GetRequiredService<MicroserviceNameRuntimeDatabaseMigrator>()
            .CheckAndApplyDatabaseMigrationsAsync();
        {{~ else if config.database_provider == "mongodb" ~}}
        await scope.ServiceProvider
            .GetRequiredService<MicroserviceNameDataSeeder>()
            .SeedAsync();
        {{~ end~}}
    }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I will confirm it and get back to you.

  • User Avatar
    1
    liangshiwei created
    Support Team Fullstack Developer

    Please use OnPreApplicationInitializationAsync as a best practice. We will update them all

Made with ❤️ on ABP v9.1.0-preview. Updated on November 11, 2024, 11:11