Architecture Decision Record
ADR-002: NavigationPopulators for Cross-Container Loading
Status
Accepted
Revised 2026-10-06: four engines including PostgreSQL; Include support is classified by source identity on relational engines, with every Cosmos DB navigation unsupported.
Context
The application supports multiple database backends (SQL Server, PostgreSQL, SQLite, Cosmos DB: the DataSource enum at MMCA.Common/Source/Core/MMCA.Common.Application/Interfaces/Infrastructure/Persistence/IDataSourceService.cs:9-22). EF Core's .Include() works on a relational engine (SQL Server, PostgreSQL, SQLite) when both ends live in the same physical database, and never on Cosmos DB, which is non-relational (MMCA.Common/Source/Core/MMCA.Common.Infrastructure/Persistence/DataSources/Engines/CosmosDataSourceEngine.cs:44). Navigation properties are tagged with [Navigation] (the attribute's IsCollection flag distinguishes a child collection from an FK reference). Whether a given navigation can actually be loaded via Include is not a static attribute value: it is classified at runtime by NavigationMetadataProvider, which asks IDataSourceService.HaveIncludeSupport(declaringEntityType.FullName, targetEntityType.FullName) with the two entity full names (MMCA.Common/Source/Core/MMCA.Common.Application/Services/Query/NavigationMetadataProvider.cs:96, against the string overload at MMCA.Common/Source/Core/MMCA.Common.Application/Interfaces/Infrastructure/Persistence/IDataSourceService.cs:70, which also returns false when either entity has no registered DataSourceKey: MMCA.Common/Source/Core/MMCA.Common.Infrastructure/Persistence/DataSources/DataSourceService.cs:35-38). Include is used only when both ends resolve to the same DataSourceKey and that engine is relational (MMCA.Common/Source/Core/MMCA.Common.Infrastructure/Persistence/DataSources/DataSourceService.cs:31-32); any other navigation (every Cosmos DB navigation, same container or not, and any cross-database navigation under database-per-service) is classified unsupported and routed to manual population instead.
Decision
Each entity that has unsupported navigations gets a INavigationPopulator<TEntity> implementation. A DeclarativeNavigationPopulator<TEntity> base class (added in MMCA.Common) allows populators to be defined as a list of INavigationDescriptor<TEntity> declarations rather than imperative if-check boilerplate.
Two descriptor types exist:
ChildNavigationDescriptor: for child collection navigations (e.g.,Event.Rooms)FKNavigationDescriptor: for FK reference navigations (e.g.,Product.Category). The parent's key is read byParentKeySelector, aFunc<TEntity, object?>that answersnullwhen the parent has no reference, so a nullable value-type key and a nullable reference key read the same way;ChildKeySelectorselects the matching key on the related entity, usually its primary key;AssignActionis anAction<TEntity, TChild?>that receives the one match ornull; andTChildIdis constrained only tonotnull, soint,Guidandstringkeys all fit (MMCA.Common/Source/Core/MMCA.Common.Application/Services/Navigation/FKNavigationDescriptor.cs:16-19,:33,:39,:42).
Both delegate to NavigationLoader, which batch-loads related entities in a single WHERE FK IN (...) query to avoid N+1 problems. Every load is bounded: the parents are already capped by the query pipeline, and one load reads at most NavigationLoader.MaxChildRowsPerLoad child rows, ten per parent at the pipeline's parent ceiling, 10,000 rows (MMCA.Common/Source/Core/MMCA.Common.Application/Services/Navigation/NavigationLoader.cs:45). The read is ordered by the child key and asks for one row past the ceiling, so an overflow is detected server-side without materializing the rest (NavigationLoader.cs:239-240), and an overflow throws InvalidOperationException naming the relationship instead of returning a silently truncated collection (NavigationLoader.cs:207-212). The remedy is to request fewer parents per page, or to read the children through their own paged endpoint.
Rationale
- Multi-DB support: The query pipeline automatically falls back from Include to NavigationPopulator when the data source reports navigations as unsupported.
- Batch efficiency: All related entities for all parents are loaded in one query per navigation, not per-parent.
- Declarative: The
DeclarativeNavigationPopulatorreplaces the repeated per-navigation if-checks each populator used to carry. Each populator is now just a list of descriptors.
Trade-offs
- Extra abstraction layer for SQL Server (where Include works fine). Mitigated: the populator is only called when the query pipeline's metadata says navigations are unsupported.
- Entities that need no manual loading still require a no-op populator (or use
NullNavigationPopulator). - A navigation whose children exceed
MaxChildRowsPerLoadfor one page of parents fails loudly rather than loading partially. That is deliberate: a truncated child collection would look complete to the caller.
Revision (2026-10-06)
- Context: the engine list now names all four
DataSourcevalues, adding PostgreSQL (IDataSourceService.cs:9-22). - Context: Include support is stated as the code computes it, same
DataSourceKeyon a relational engine (DataSourceService.cs:31-32). Every Cosmos DB navigation is unsupported, not only cross-container ones (CosmosDataSourceEngine.cs:44), and the "certain SQLite configurations" case is dropped: SQLite is relational like SQL Server and PostgreSQL (SqliteDataSourceEngine.cs:41,SQLServerDataSourceEngine.cs:44,PostgreSQLDataSourceEngine.cs:42). - Rationale: the "~30-40 lines per entity" figure is dropped as overstated; the Store adoption commit saved fewer lines per populator than that.
- Anchors and type names (
NavigationMetadataProvider,DeclarativeNavigationPopulator,ChildNavigationDescriptor,FKNavigationDescriptor,NavigationLoader,NullNavigationPopulator) were re-verified against current MMCA.Common source.
Revision (2026-12-03): bounded child loads and the reshaped FK descriptor
MMCA.Common v1.331.0. The descriptor and loader layer stays as decided.
- Child loads are capped at
MaxChildRowsPerLoad(10,000) rows per load, ordered by the child key, and throwInvalidOperationExceptionpast the cap (NavigationLoader.cs:45,:207-212,:239-240). Afields=projection is applied by the query pipeline after the populator ran, so these loads always read whole child entities (NavigationLoader.cs:34-35). FKNavigationDescriptortakesChildKeySelector, aFunc<TEntity, object?>ParentKeySelector, anAction<TEntity, TChild?>AssignAction, and anotnullTChildId(FKNavigationDescriptor.cs:16-19,:33,:39,:42); a non-null parent key of the wrong type throws on load (NavigationLoader.cs:218-225).