Layers : Infrastructure.Persistence
The Infrastructure.Persistence layer templates generate the Entity Framework Core data access implementation for each entity within a bounded context. The generator produces a DbContext, entity configurations, command/query/domain repository implementations, a design-time DbContext factory, and dependency injection helpers — all driven by the entity definitions configured in the Scaffolder.net UI.
Project File
The {{Solution.Name}}.Infrastructure.Persistence.{{BoundedContext.Name}}.csproj file defines the persistence project with references to the Application project (same bounded context) and the Shared.Infrastructure.Persistence project. It also includes the Microsoft.EntityFrameworkCore.Design package as a private asset for EF Core migrations and design-time tooling.
Assembly Reference
The {{Solution.Name}}InfrastructurePersistence{{BoundedContext.Name}}AssemblyReference class is an internal sealed marker class used to reference the persistence assembly for reflection-based service registration and entity configuration discovery.
Global Usings
The GlobalUsings.cs file is generated with global using directives for all entity command and query namespaces from the Application layer, domain entity and repository namespaces from the Domain layer, the DbContext namespace, and shared persistence abstractions. This eliminates the need for explicit using statements throughout the persistence project.
DbContexts
The {{Solution.Name}}{{BoundedContext.Name}}DbContext is an internal sealed class inheriting from DbContext and implementing IUnitOfWork. It defines DbSet properties for each entity (generated by InfrastructurePersistenceGenerator.GetDbSets) and applies all entity configurations from the persistence assembly via ApplyConfigurationsFromAssembly in OnModelCreating.
Dependency Injection
The ServiceCollectionExtension class provides the AddInfrastructurePersistenceServices method that registers the DbContext factory with domain event interception, the Unit of Work, command repositories, query repositories, domain repositories (all by scanning the persistence assembly), and domain services (by scanning the domain assembly).
Design Time DbContext Factories
The {{Solution.Name}}{{BoundedContext.Name}}DesignTimeDbContextFactory is an internal sealed class implementing IDesignTimeDbContextFactory<T>. It builds a configuration from appsettings.json and environment variables, retrieves the connection string, and creates a DbContext instance configured for SQL Server — enabling EF Core migrations and scaffolding commands.
Features : Commands
For each entity, the generator produces an internal sealed {{Entity.NameSingular}}CommandRepository class that extends CommandRepository<TDbContext, TEntity, TEntityId> and implements I{{Entity.NameSingular}}CommandRepository. It inherits the Add, Update, Remove, and GetEntityAsync methods from the base class, wired to the bounded context's DbContext.
Features : Configurations
{{Entity.NameSingular}}Configuration class implementing IEntityTypeConfiguration<T>. The generator configures :
- Primary key : Uses the Id property by default, or a custom primary key property when the Id is not the primary key. Supports composite primary keys.
- Indexes : Creates an index on the Id property (if Id is not the primary key) and unique indexes on all unique properties.
- Id property : Configures the Id as required, with
ValueGeneratedNeverif not auto-generated, orValueGeneratedOnAddif auto-generated (non-Guid, non-primary-key). - Scalar properties : Configures required/nullable, max length and Unicode for strings, and precision/scale for decimals.
- Foreign key relationships : Configures one-to-one or one-to-many relationships based on the entity's foreign key properties, with navigation properties and cascade delete behavior.
Features : Domains
{{Entity.NameSingular}}DomainRepository class that extends DomainRepository<TDbContext> and implements I{{Entity.NameSingular}}DomainRepository. It inherits from the base class and generates uniqueness check methods :
- ExistsWithIdAsync : Delegates to
ExistsWithPropertyNameAsyncfor the Id property (if Id is not auto-generated). - ExistsWith{{PropertyName}}Async : Delegates to
ExistsWithPropertyNameAsyncfor each unique property. - ExistsWith{{PropertyName}}ExcludingIdAsync : Delegates to
ExistsWithPropertyNameExcludingIdAsyncfor each unique property, used during edit to exclude the current entity.
Features : Queries
{{Entity.NameSingular}}QueryRepository class that extends QueryRepository<TDbContext> and implements I{{Entity.NameSingular}}QueryRepository. It creates short-lived DbContext instances via the factory and implements :
- GetListAsync : Queries all entities and projects them into
Get{{Entity.NameSingular}}ListQueryResponserecords. - GetDetailsAsync : Queries a single entity by Id and projects it into a
Get{{Entity.NameSingular}}DetailsQueryResponserecord. - GetEditAsync : Queries a single entity by Id and projects it into an
Edit{{Entity.NameSingular}}CommandRequestrecord for the edit form. - GetDeleteAsync : Queries a single entity by Id and projects it into a
Delete{{Entity.NameSingular}}CommandRequestrecord for the delete confirmation.