Microsoft.EntityFrameworkCore dependency in Application layer #483
Replies: 6 comments
-
That's quite deliberate. I believe Jason covers why in his talk. That dependency is there because it's the database agnostic part of Entity Framework. The MS SQL server dependency is in Infrastructure. Sure, you can abstract it away behind a repository, but then you have a repository wrapping a repository ( |
Beta Was this translation helpful? Give feedback.
-
Hi @ronnieholm, thank you for the reply and it makes perfect sense. If my application had a mix of EF and Dapper, would the recommended implementation be to abstract the Dapper bits behind a repository? Thanks again. |
Beta Was this translation helpful? Give feedback.
-
It definitely breaks Dependency Rule from the original The Clean Architecture article by Robert C. Martin! |
Beta Was this translation helpful? Give feedback.
-
@seoriy I totally agree with you on that one however, this is a choice for the architect to make and Jason has backed his choice in all his talks. Talking about the clean architecture book, Uncle bob also made an exception by saying
Jason said that's he is ok with coupling with EntityFramework since he used it for a very very long time. However, you can make the change easily in the template. It's not referenced everywhere, except for the DbSet dependency. |
Beta Was this translation helpful? Give feedback.
-
I think he was talking about a bit other cases. I.e. if you use .NET Framework to write application, it's obvious, that you use BCL even to describe domain logic, so you may use System.Collections.Generic.List<> for domain logic, without treating it as a violation of The Dependency Rule. Same for IDisposable, LINQ, etc.
Also
It's only his personal choise. Someone else may want to use this template, but replacing EF Core with Dapper, for example... |
Beta Was this translation helpful? Give feedback.
-
@seoriy Yes, the conclusion was for the chapter, the section above it was for setting the exception. And yes, everything is relative. As I said before, you can change this template to match your own definition. And again, this is not an absolute solution/template everything is flexible, you can build on it, change it, make it perfect for your own decisions. |
Beta Was this translation helpful? Give feedback.
-
Hi. I was curious if we should have a dependency on the database ORM within the Application layer? Specifically we're referencing Microsoft.EntityFrameworkCore in Application > TodoLists > Queries > GetTodos > GetTodosQuery.cs. Seems like this should be abstracted away from the business logic, maybe opting for Repository pattern to inject specific functionality.
private readonly ITodoRepository todosRepository;
public GetTodosQueryHandler(ITodosRepository todosRepository)
{
this.todosRepository = todosRepository;
}
public async Task<TodosVm> Handle(GetTodosQuery request, CancellationToken cancellationToken)
{
// ...
Lists = await todosRepository.GetTodoListsAsync(cancellationToken);
}
Appreciate any advice/guidance.
Beta Was this translation helpful? Give feedback.
All reactions