Skip to content
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

feat: add DbContext factory support (#49) #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dmitrii-kiselev
Copy link

Closes #49

@dmitrii-kiselev
Copy link
Author

Hi @TanvirArjel,

Please take a look at my pull request when you have some free time.
It will add some critical missing features.

Best Regards,
Dmitrii Kiselev

// For multiple DbContext
services.AddQueryRepository<YourDbContext1>();
services.AddQueryRepository<YourDbContext2>();

Copy link
Owner

@TanvirArjel TanvirArjel Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitrii-kiselev I am still not sure about why services.AddQueryRepository<YourDbContext1>(); will not work worked in Blazor but services.AddQueryRepositoryFactory<YourDbContext>(); will work. Could you please provide a runnable blazor demo app where services.AddQueryRepository<YourDbContext1>(); is not working and that's why we need to use services.AddQueryRepositoryFactory<YourDbContext>();

Thanks!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @TanvirArjel,

Thank you for your feedback.
I will prepare a detailed example for you.

For now, I will attach a brief description:
https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/#using-a-dbcontext-factory-eg-for-blazor

Since the Repository<TDbContext> and QueryRepository<TDbContext> classes are internal, there is currently no way to create multiple parallel repositories with independent DbContext.

EF Core doesn't support multiple parallel operations being run on the same context instance. You should always wait for an operation to complete before beginning the next operation. This is typically done by using the await keyword on each async operation.
https://learn.microsoft.com/en-us/ef/core/miscellaneous/async

We can experiment with dependency injection using a Transient lifetime.
We can use IDbContextFactory<TDbContext>, but the created context instance cannot be integrated into the repository due to implementation scope.
Even by making the Repository<TDbContext> and QueryRepository<TDbContext> classes public, we will have to create our factory to follow the clean architecture, so it is better to implement this in the library.

So the best option is a factory that allows us to create transient instances and destroy them by using statement.

Best Regards,
Dmitrii Kiselev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use GenericRepository with AddDbContextFactory
2 participants