Simplify DbContext by extracting aggregate configuration into dedicated classes #676
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary & Motivation
Remove the need for centralized configuration in the
DbContext
when defining aggregates in a self-contained system. Previously, theDbContext
required aDbSet<T>
property for each aggregate (table) and explicit configuration inOnModelCreating
for strongly typed IDs and other EF configurations like.HasOne<T>()
,.WithMany()
,.OnDelete()
,.ToJson()
, etc. Over time, this made theDbContext
in a self-contained system big and hard to maintainWith this change, all entity configurations are moved to dedicated classes implementing
IEntityTypeConfiguration<T>
, keeping each aggregate's configuration within its vertical slice. TheSharedKernelDbContext
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 defineDbSet<T>
properties explicitly, making theDbContext
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 inOnModelCreating
in the self-contained system’sDbContext
. Example:All properties for tables should be removed from DbContext. Any direct usage of these should be replaced by DbSet in the repository. Example:
Checklist