Skip to content
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

Simplify DbContext by extracting aggregate configuration into dedicated classes #676

Merged
merged 3 commits into from
Jan 19, 2025

Conversation

tjementum
Copy link
Member

@tjementum tjementum commented Jan 19, 2025

Summary & Motivation

Remove the need for centralized configuration in the DbContext when defining aggregates in a self-contained system. Previously, the DbContext required a DbSet<T> property for each aggregate (table) and explicit configuration in OnModelCreating for strongly typed IDs and other EF configurations like .HasOne<T>(), .WithMany(), .OnDelete(), .ToJson(), etc. Over time, this made the DbContext in a self-contained system big and hard to maintain

With this change, all entity configurations are moved to dedicated classes implementing IEntityTypeConfiguration<T>, keeping each aggregate's configuration within its vertical slice. The SharedKernelDbContext now automatically discovers and registers these configurations, eliminating the need for central configuration.

Additionally, the DbContext now uses a convention-based approach to create tables, leveraging Humanizer for pluralizing table names. This removes the need to define DbSet<T> properties explicitly, making the DbContext a lightweight class used only in repositories without requiring customization for new aggregates.

Downstream Projects

Extract an AggregateConfiguration file for each aggregate, where the content should match the current configuration in OnModelCreating in the self-contained system’s DbContext. Example:

namespace PlatformPlatform.AccountManagement.Features.Signups.Domain;

public sealed class SignupConfiguration : IEntityTypeConfiguration<Signup>
{
    public void Configure(EntityTypeBuilder<Signup> builder)
    {
        builder.MapStronglyTypedUuid<Signup, SignupId>(a => a.Id);
        builder.MapStronglyTypedNullableId<Signup, TenantId, string>(u => u.TenantId);
    }
}

All properties for tables should be removed from DbContext. Any direct usage of these should be replaced by DbSet in the repository. Example:

// Before:
return accountManagementDbContext.Signups.Where(s => ...);

// After:
return DbSet.Where(s => ...);

Checklist

  • I have added tests, or done manual regression tests
  • I have updated the documentation, if necessary

@tjementum tjementum added the Enhancement New feature or request label Jan 19, 2025
@tjementum tjementum self-assigned this Jan 19, 2025
@tjementum tjementum merged commit 92889b8 into main Jan 19, 2025
15 of 17 checks passed
@tjementum tjementum deleted the pp-309-extract-entity-framwork-configurations branch January 19, 2025 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant