Description
Is your feature request related to a problem?
So in our project we want to design our application in a way that we can easily add different API interfaces like GraphQL or a RestAPI on our existing business logic.
To achieve this we moved as much logic as possible to regular Services which live in the DI container. This services are made using regular microsoft standards, like injecting the DbContext normally through the constructor.
Now we do have a small problem, for Queries we want the service to pass back a IQueryable of the DbSet in the DbContext and pass this to the Query class so we can use the projection option and only retrieve the fields that the query requests.
Problem is that when injecting the DbContext in the service the regular way it will start throwing an Cannot access a disposed context instance
error.
I have an example project here: https://github.com/bramvelderen-nlo/Playday.GraphQL
- The service: https://github.com/bramvelderen-nlo/Playday.GraphQL/blob/main/src/Playday.GraphQL.API/Graph/Scooters/ScooterService.cs
- The query class: https://github.com/bramvelderen-nlo/Playday.GraphQL/blob/main/src/Playday.GraphQL.API/Graph/Scooters/ScooterQueries.cs
- The program class: https://github.com/bramvelderen-nlo/Playday.GraphQL/blob/main/src/Playday.GraphQL.API/Program.cs
The solution you'd like
What i hope is possible is that i can just build my service layer the same way as i would for an MVC project and reuse the exact logic for the GraphQL resolvers. But right now that gives me the DbContext error. So is there already an existing solution here where i don't need to pass the DbContext from the Query class to the service and instead can just rely on the injected DbContext?
Product
Hot Chocolate