-
-
Notifications
You must be signed in to change notification settings - Fork 158
RFC: De-Couple data access from EF #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Been getting familiar with the project and that's one of the first things that came to mind. Can I use this with a custom business/data layer? That would be really nice. |
@donniefitz2 currently, there are 2 approaches for implementing custom business logic:
However, this may be a little convoluted for non-EF DALs. The hope with the proposed changes is to lean on a public class TodoItemService : IResourceService<TodoItem> {
private readonly IMyTodoItemRepository _todoItemRepository;
public TodoItemService(IMyTodoItemRepository todoItemRepository) {
_todoItemRepository = todoItemRepository;
}
public async Task<List<TodoItem>> GetAsync() {
return await _todoItemRepository.GetTodoItems();
}
public async Task<TodoItem> GetAsync(int id) {
return await _todoItemRepository.GetTodoItemById(id);
}
// ...
} The new default layer scheme would be:
But you could, of course, change the dependency graph however you need to, beyond the
|
I like the idea of the resource interface. That keeps the controllers clean and allows the BL/DAL stuff to exist in a separate layer. |
@jaredcnance I really like that idea! Actually I am currently exactly at a point where I want to expose data that is in the backend fetched from another REST service. Maybe I'll try a first draft in my example project next week. |
@JanMattner thanks for the feedback! It is greatly appreciated. I'm not sure how to implement in a non-breaking way without adding significant surface area to the code, such as a new The problem with the services.AddJsonApi<AppDbContext>(opt => {
opt.BuildContextGraph(builder => {
builder.WithResource<MyResource>("my-resources");
});
}); |
Sounds good. In order to decouple it completely, we must get rid of the DbContext generic type. So rather something like: services.AddJsonApi(opt => {
opt.BuildContextGraph(builder => {
builder.WithDbContext<AppDbContext>();
builder.WithResource<MyResource>("my-resources");
});
}); => Everything inside the DbContext can be added and further more custom ressources not bound to any DbContext. For backwards compatibility, we can still leave the currently public methods as is and call this builder internally. |
Yeah, I like that and agree that we shouldn't require a DbContext |
Problem
Currently, the controller leans on
IEntityRepository
directly which is tightly coupled toIQueryable
. However, this doesn't make much sense for ORMs, like Dapper, that do not useIQueryable
. Also, it is entirely possible that resources may be retrieved from non SQL storage solutions, such as another HTTP API or RPC.Tightly Coupled Areas:
Proposed Solution
IResourceService
. Reduces controller methods to something like the following. Would allow any intermediate service to perform the data access.ContextGraphBuilder
toEntityContextGraphBuilder
, since it uses the EFDbContext
.ContextGraph
be used without theDbContext
generic? If not, perform renameEntity...
IContextGraph
implementations or an API to build a generalized context graphConsiderations
The text was updated successfully, but these errors were encountered: