From da3e2cdc32aade5b6e30813179d05ad561fc6c82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 23 Aug 2023 17:37:54 +0300 Subject: [PATCH] Enhance the documentation part for readonly repositories. --- docs/en/Entity-Framework-Core.md | 8 ++++++++ docs/en/Repositories.md | 8 +++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/en/Entity-Framework-Core.md b/docs/en/Entity-Framework-Core.md index e2cfd21a05..1f8b9e22f0 100644 --- a/docs/en/Entity-Framework-Core.md +++ b/docs/en/Entity-Framework-Core.md @@ -594,6 +594,14 @@ Whenever you access to a property/collection, EF Core automatically performs an See also [lazy loading document](https://docs.microsoft.com/en-us/ef/core/querying/related-data/lazy) of the EF Core. +## Read-Only Repositories + +ABP Framework provides read-only [repository](Repositories.md) interfaces (`IReadOnlyRepository<...>` or `IReadOnlyBasicRepository<...>`) to explicitly indicate that your purpose is to query data, but not change it. If so, you can inject these interfaces into your services. + +Entity Framework Core read-only repository implementation uses [EF Core's No-Tracking feature](https://learn.microsoft.com/en-us/ef/core/querying/tracking#no-tracking-queries). That means the entities returned from the repository will not be tracked by the EF Core [change tracker](https://learn.microsoft.com/en-us/ef/core/change-tracking/), because it is expected that you won't update entities queried from a read-only repository. + +> This behavior works only if the repository object is injected with one of the read-only repository interfaces (`IReadOnlyRepository<...>` or `IReadOnlyBasicRepository<...>`). It won't work if you have injected a standard repository (e.g. `IRepository<...>`) then casted it to a read-only repository interface. + ## Access to the EF Core API In most cases, you want to hide EF Core APIs behind a repository (this is the main purpose of the repository pattern). However, if you want to access the `DbContext` instance over the repository, you can use `GetDbContext()` or `GetDbSet()` extension methods. Example: diff --git a/docs/en/Repositories.md b/docs/en/Repositories.md index 57115f903b..a93460bbdd 100644 --- a/docs/en/Repositories.md +++ b/docs/en/Repositories.md @@ -205,8 +205,6 @@ Methods: - `WithDetails()` 1 overload - `WithDetailsAsync()` 1 overload - - Where as the `IReadOnlyBasicRepository` provides the following methods: - `GetCountAsync()` @@ -217,11 +215,11 @@ They can all be seen as below: ![generic-repositories](images/generic-repositories.png) -#### Read Only Repositories behavior in EF Core +#### Read Only Repositories behavior in Entity Framework Core -EF Core will use No-tracking queries for read-only repositories. This means that the entities returned from the repository will not be tracked by the EF Core's change tracker. This is a performance optimization. +Entity Framework Core read-only repository implementation uses [EF Core's No-Tracking feature](https://learn.microsoft.com/en-us/ef/core/querying/tracking#no-tracking-queries). That means the entities returned from the repository will not be tracked by the EF Core [change tracker](https://learn.microsoft.com/en-us/ef/core/change-tracking/), because it is expected that you won't update entities queried from a read-only repository. If you need to track the entities, you can still uses [AsTracking()](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.entityframeworkqueryableextensions.astracking) extension method. -However, if you need to track the entities, You can still add [AsTracking](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.entityframeworkqueryableextensions.astracking) to make specific queries tracking. +> This behavior works only if the repository object is injected with one of the read-only repository interfaces (`IReadOnlyRepository<...>` or `IReadOnlyBasicRepository<...>`). It won't work if you have injected a standard repository (e.g. `IRepository<...>`) then casted it to a read-only repository interface. ### Generic Repository without a Primary Key