The EF core includes functionalities that you may want to use for complex queries. For example AsNoTracking()
, SumAsync
, MaxAsync
or finding changed/modified entities etc. are available in Microsoft.EntityFrameworkCore
assembly
In clean architecture the ApplicationCore
layer does not allow direct access to DBContext, and data can be accessed only via common repository interface and specifications
I understand the idea of separation of concern and abstracting persistent layer from domain layer. And in future if you change the DB you only need to change persistent layer. However in reality, in enterprise level application how often you change the database. Once its in production it becomes harder to simply replace it.
public class ToDoItemSearchService : IToDoItemSearchService
{
private readonly IRepository<Project> _repository;
public ToDoItemSearchService(IRepository<Project> repository)
{
_repository = repository;
}
public async Task<Result<List<ToDoItem>>> GetAllIncompleteItemsAsync(int projectId, string searchString)
{
var project = await _repository.GetBySpecAsync(projectSpec);
// how to load entities without tracking
}
}
How to access EF functionalities? Should the repository expose those? For example
public interface IRepository<TEntity>
{
List<TEntity> GetBySpecAsync>(...);
List<TEntity> GetBySpecAsyncWithNoTracking(...)
}