docs/en/framework/architecture/best-practices/repositories.md
//[doc-seo]
{
"Description": "Discover best practices for implementing repository classes in your applications, grounded in Domain-Driven Design principles."
}
This document offers best practices for implementing Repository classes in your modules and applications based on Domain-Driven-Design principles.
Ensure you've read the Repositories document first.
IIdentityUserRepository) and create its corresponding implementations for each aggregate root.
IRepository<IdentityUser, Guid>) from the application code.IQueryable<TEntity> features in the application code (domain, application... layers).For the example aggregate root:
public class IdentityUser : AggregateRoot<Guid>
{
//...
}
Define the repository interface as below:
public interface IIdentityUserRepository : IBasicRepository<IdentityUser, Guid>
{
//...
}
IRepository<TEntity, TKey> interface. Because it inherits the IQueryable and the repository should not expose IQueryable to the application.IBasicRepository<TEntity, TKey> (as normally) or a lower-featured interface, like IReadOnlyRepository<TEntity, TKey> (if it's needed).cancellationToken parameter to every method of the repository. Example:Task<IdentityUser> FindByNormalizedUserNameAsync(
[NotNull] string normalizedUserName,
CancellationToken cancellationToken = default
);
bool includeDetails = true parameter (default value is true) for every repository method which returns a single entity. Example:Task<IdentityUser> FindByNormalizedUserNameAsync(
[NotNull] string normalizedUserName,
bool includeDetails = true,
CancellationToken cancellationToken = default
);
This parameter will be implemented for ORMs to eager load sub collections of the entity.
bool includeDetails = false parameter (default value is false) for every repository method which returns a list of entities. Example:Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync(
string normalizedRoleName,
bool includeDetails = false,
CancellationToken cancellationToken = default
);
includeDetails option to add all details of the entity when needed.