Skip to content

refactor(data): improve seeding with UseAsyncSeeding #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025

Conversation

nanotaboada
Copy link
Owner

@nanotaboada nanotaboada commented Apr 7, 2025

Summary by CodeRabbit

  • New Features

    • Introduced asynchronous database seeding to automatically initialize player records.
  • Refactor

    • Improved the database connection setup by dynamically constructing the database file path.
    • Enhanced the logging configuration and overall error handling during initialization for smoother operation.
  • Chores

    • Removed redundant functionality related to player data mapping.

Copy link

coderabbitai bot commented Apr 7, 2025

Walkthrough

The pull request updates the Web API sample to utilize asynchronous database seeding. The Program.cs file now constructs the SQLite connection string dynamically and modifies logging configuration. The synchronous seeding method in the application builder extension has been changed to an asynchronous method with updated Entity Framework Core calls and improved error handling. A new utility class, DbContextUtils, has been introduced to facilitate asynchronous seeding of player data. Additionally, an extension method for mapping player properties has been removed.

Changes

File(s) Change Summary
Dotnet.Samples.AspNetCore.WebApi/Program.cs Added a new using directive; updated the SQLite connection string to use a dynamic path; modified logging configuration and middleware ordering; enabled asynchronous data seeding with DbContextUtils.SeedAsync; removed commented seeding code.
Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs Converted the synchronous seeding method to an asynchronous method (SeedDbContextAsync) with calls to EnsureCreatedAsync, AnyAsync, AddRangeAsync, and SaveChangesAsync. Added error handling.
Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs Introduced a new static class DbContextUtils with methods SeedAsync and SeedPlayersAsync for asynchronous database seeding.
Dotnet.Samples.AspNetCore.WebApi/Utilities/PlayerDataExtensions.cs Deleted file that contained the MapFrom extension method for mapping player properties.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application Startup
    participant IB as IApplicationBuilder (SeedDbContextAsync)
    participant DB as PlayerDbContext
    participant Utils as DbContextUtils
    participant Data as SeedPlayersAsync

    App->>IB: Initialize async seeding
    IB->>DB: GetRequiredService(PlayerDbContext)
    DB-->>IB: Instance retrieved
    IB->>DB: EnsureCreatedAsync()
    IB->>DB: AnyAsync(Players)
    alt Players exist
        DB-->>IB: Players found, skipping seeding
    else No players
        IB->>Utils: Call SeedAsync(DbContext, cancellationToken)
        Utils->>Data: Invoke SeedPlayersAsync(PlayerDbContext, cancellationToken)
        Data->>DB: AddRangeAsync(new players)
        Data->>DB: SaveChangesAsync()
        Data-->>Utils: Seeding complete
        Utils-->>IB: Seeding complete
    end
    IB-->>App: Seeding process finished
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (3)

6-7: Consider adding XML documentation.

Adding XML documentation comments to the class would enhance maintainability and provide better IntelliSense support for developers using this utility.

+/// <summary>
+/// Provides utility methods for database context operations, particularly for seeding data.
+/// </summary>
 public static class DbContextUtils

14-24: Good implementation of async database seeding.

The method correctly uses async/await pattern and properly propagates the cancellation token to all async operations. It also efficiently checks if seeding is needed before adding data.

Consider adding XML documentation for this method as well:

+/// <summary>
+/// Seeds the player database with initial player data if the Players table is empty.
+/// </summary>
+/// <param name="dbContext">The player database context.</param>
+/// <param name="cancellationToken">A token to cancel the operation if needed.</param>
+/// <returns>A task representing the asynchronous seeding operation.</returns>
 private static async Task SeedPlayersAsync(
     PlayerDbContext dbContext,
     CancellationToken cancellationToken
 )

19-23: Consider adding error handling.

While the async operations are properly implemented, consider adding error handling to gracefully manage potential exceptions that might occur during database operations.

 if (!await dbContext.Players.AnyAsync(cancellationToken))
 {
-    await dbContext.Players.AddRangeAsync(PlayerData.CreateStarting11(), cancellationToken);
-    await dbContext.SaveChangesAsync(cancellationToken);
+    try
+    {
+        await dbContext.Players.AddRangeAsync(PlayerData.CreateStarting11(), cancellationToken);
+        await dbContext.SaveChangesAsync(cancellationToken);
+    }
+    catch (Exception ex)
+    {
+        // Consider logging the exception here
+        throw new InvalidOperationException("Failed to seed player data. See inner exception for details.", ex);
+    }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 33f5b67 and 72faac0.

📒 Files selected for processing (4)
  • Dotnet.Samples.AspNetCore.WebApi/Program.cs (3 hunks)
  • Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs (1 hunks)
  • Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (1 hunks)
  • Dotnet.Samples.AspNetCore.WebApi/Utilities/PlayerDataExtensions.cs (0 hunks)
💤 Files with no reviewable changes (1)
  • Dotnet.Samples.AspNetCore.WebApi/Utilities/PlayerDataExtensions.cs
🧰 Additional context used
🧬 Code Definitions (1)
Dotnet.Samples.AspNetCore.WebApi/Program.cs (1)
Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (1)
  • DbContextUtils (6-25)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (10)
Dotnet.Samples.AspNetCore.WebApi/Program.cs (4)

5-5: LGTM - Added namespace for new utilities.

The new using directive properly imports the utilities namespace that contains the DbContextUtils class needed for the async database seeding implementation.


32-32: Good improvement to database path handling.

Using Path.Combine() is preferable over string concatenation for file paths as it properly handles directory separators across different operating systems, making the code more robust and cross-platform compatible.


37-40: Great upgrade to use async seeding with proper logging.

This change offers several improvements:

  1. Redirects EF Core logging through Serilog instead of directly to console
  2. Implements the new EF Core 9.0 async seeding capability
  3. Properly references the implementation in DbContextUtils

The comment with documentation link is also helpful for future maintainers.


91-93: Improved middleware organization.

Moving the Serilog request logging setup after the environment check but before other middleware ensures consistent request logging across all environments while maintaining a logical middleware order.

Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs (5)

9-11: LGTM: Method documentation and signature correctly updated for async pattern

The method signature has been properly updated to use the async pattern, including the Task return type and the "Async" suffix in the method name, which follows .NET naming conventions.


14-16: Improved service resolution with GetRequiredService

Good change to use GetRequiredService instead of what was likely GetService previously. This ensures that the required services exist and throws a meaningful exception if they don't, rather than having to handle null services later.


18-20: LGTM: Added error handling and proper async database creation

Good addition of try/catch block for error handling, and proper conversion to use EnsureCreatedAsync() instead of the synchronous version.


22-27: LGTM: Proper async implementation for database operations

Excellent conversion of database operations to their async equivalents:

  • AnyAsync() for checking existence
  • AddRangeAsync() for adding entities
  • SaveChangesAsync() for persisting changes

The logging message is also improved to clearly indicate successful seeding.


29-36: Robust exception handling with proper logging and rethrow

Good implementation of exception handling that:

  1. Logs the detailed exception with the logger
  2. Rethrows a more specific InvalidOperationException with a clear message
  3. Preserves the original exception as the inner exception

This maintains the stack trace while providing better context about what failed.

Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (1)

1-5: Good choice of namespaces and organization.

The imports and namespace declaration are appropriately organized, with the necessary dependencies included.

Copy link

codacy-production bot commented Apr 7, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
-0.48% (target: -1.00%) 0.00%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (33f5b67) 1594 954 59.85%
Head commit (59ae2fc) 1607 (+13) 954 (+0) 59.37% (-0.48%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#197) 36 0 0.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Copy link

codecov bot commented Apr 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (33f5b67) to head (59ae2fc).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #197   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines          158       158           
  Branches         9         9           
=========================================
  Hits           158       158           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nanotaboada nanotaboada linked an issue Apr 7, 2025 that may be closed by this pull request
@nanotaboada nanotaboada force-pushed the feature/use-async-seeding branch from 72faac0 to 59ae2fc Compare April 7, 2025 18:42
Copy link

sonarqubecloud bot commented Apr 7, 2025

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (2)

31-41: Consider adding logging and exception handling to SeedPlayersAsync.

The implementation is functionally correct but lacks logging capabilities that were present in the original ApplicationBuilderExtensions.SeedDbContextAsync method. This could make troubleshooting more difficult in production scenarios.

 private static async Task SeedPlayersAsync(
     PlayerDbContext dbContext,
-    CancellationToken cancellationToken
+    CancellationToken cancellationToken,
+    ILogger? logger = null
 )
 {
-    if (!await dbContext.Players.AnyAsync(cancellationToken))
+    try
     {
-        await dbContext.Players.AddRangeAsync(PlayerData.CreateStarting11(), cancellationToken);
-        await dbContext.SaveChangesAsync(cancellationToken);
+        if (!await dbContext.Players.AnyAsync(cancellationToken))
+        {
+            await dbContext.Players.AddRangeAsync(PlayerData.CreateStarting11(), cancellationToken);
+            await dbContext.SaveChangesAsync(cancellationToken);
+            logger?.LogInformation("Successfully seeded database with initial player data.");
+        }
+    }
+    catch (Exception ex)
+    {
+        logger?.LogError(ex, "An error occurred while seeding the player data");
+        throw;
     }
 }

31-41: Add database transaction for data consistency.

For data seeding operations, it's a good practice to use a transaction to ensure all-or-nothing insertions, especially when adding multiple entities. While this may not be critical for a small seeding operation, it represents a good pattern for database modifications.

 private static async Task SeedPlayersAsync(
     PlayerDbContext dbContext,
     CancellationToken cancellationToken
 )
 {
     if (!await dbContext.Players.AnyAsync(cancellationToken))
     {
+        await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
+        try
+        {
             await dbContext.Players.AddRangeAsync(PlayerData.CreateStarting11(), cancellationToken);
             await dbContext.SaveChangesAsync(cancellationToken);
+            await transaction.CommitAsync(cancellationToken);
+        }
+        catch
+        {
+            // Transaction will automatically roll back if not committed
+            throw;
+        }
     }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 72faac0 and 59ae2fc.

📒 Files selected for processing (4)
  • Dotnet.Samples.AspNetCore.WebApi/Program.cs (3 hunks)
  • Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs (1 hunks)
  • Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (1 hunks)
  • Dotnet.Samples.AspNetCore.WebApi/Utilities/PlayerDataExtensions.cs (0 hunks)
💤 Files with no reviewable changes (1)
  • Dotnet.Samples.AspNetCore.WebApi/Utilities/PlayerDataExtensions.cs
🚧 Files skipped from review as they are similar to previous changes (1)
  • Dotnet.Samples.AspNetCore.WebApi/Program.cs
🧰 Additional context used
🧬 Code Definitions (1)
Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (2)
Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs (1)
  • Task (11-37)
Dotnet.Samples.AspNetCore.WebApi/Data/PlayerData.cs (1)
  • PlayerData (7-335)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (8)
Dotnet.Samples.AspNetCore.WebApi/Utilities/ApplicationBuilderExtensions.cs (6)

9-11: Good update to documentation and method signature.

The XML documentation and method signature have been appropriately updated to reflect the new asynchronous nature of the method. The naming convention also correctly follows the async suffix pattern.


14-16: Good use of GetRequiredService.

Switching to GetRequiredService is a positive change as it makes the code's expectations clearer and fails fast if dependencies are missing, rather than potentially causing null reference exceptions later.


18-20: Proper implementation of async database creation.

The try-catch block is a good addition for error handling, and the switch to EnsureCreatedAsync() appropriately follows the async pattern for database operations.


22-22: Correct use of async query operation.

Converting Any() to AnyAsync() is the right approach for asynchronous database operations, improving scalability by not blocking threads during I/O operations.


24-26: Well-implemented async database operations with proper logging.

The switch to AddRangeAsync() and adding SaveChangesAsync() follows best practices for async EF Core operations. The addition of informational logging on successful seeding is also valuable for diagnostics.


29-36: Robust error handling implementation.

The exception handling has been well implemented with:

  1. Appropriate logging of the exception with context
  2. Proper re-throwing with additional context information
  3. Preserving the original exception as the inner exception

This will significantly improve troubleshooting of seeding issues.

Dotnet.Samples.AspNetCore.WebApi/Utilities/DbContextUtils.cs (2)

8-29: Well-structured method with proper documentation and type safety.

The implementation of SeedAsync follows best practices by:

  1. Including comprehensive XML documentation
  2. Using type pattern matching (is not) for safe casting
  3. Providing a detailed exception message
  4. Having a clear separation of concerns

This implementation addresses the previous review feedback about documenting the unused parameter and improving type safety.


36-36:

❓ Verification inconclusive

Consider including a database creation check.

The original ApplicationBuilderExtensions.SeedDbContextAsync method called EnsureCreatedAsync() before checking for existing data. While this might be handled elsewhere in the application, it's worth ensuring that the database exists before attempting to query or modify it.


🏁 Script executed:

#!/bin/bash
# Check if database creation is handled elsewhere in the code
rg -A 3 "Database\.EnsureCreated" --type cs

Length of output: 475


Ensure Database Creation Is Appropriately Handled

We've confirmed via the search that the call to await dbContext.Database.EnsureCreatedAsync() is already present in the ApplicationBuilderExtensions.SeedDbContextAsync method. This suggests that, in contexts where the seeding method is used, the database creation check is handled prior to invoking code in DbContextUtils.cs.

  • If the methods in DbContextUtils.cs are exclusively called after seeding (i.e., after ensuring the database is created), then no immediate change is needed.
  • However, if there’s any chance these utilities might be used independently, please consider either:
    • Adding an explicit EnsureCreatedAsync() call in DbContextUtils.cs, or
    • Documenting clearly that its callers must ensure the database exists.

@nanotaboada nanotaboada merged commit 97329a0 into master Apr 7, 2025
24 checks passed
@nanotaboada nanotaboada deleted the feature/use-async-seeding branch April 7, 2025 19:07
@coderabbitai coderabbitai bot mentioned this pull request Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor existing data seeding to make use of new UseAsyncSeeding
1 participant