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

How to apply the .ToListAsync();? #71

Open
MiBuena opened this issue Dec 8, 2021 · 4 comments
Open

How to apply the .ToListAsync();? #71

MiBuena opened this issue Dec 8, 2021 · 4 comments

Comments

@MiBuena
Copy link

MiBuena commented Dec 8, 2021

In asynchronous programming there is a very useful async method: ToListAsync();
However, it comes from EntityFramework. So basically if we use the template here and call .All(); method in the Service and after that call .ToListAsync() in the service - then we will create a dependency between the Service and EF.

Perhaps I should add a wrapper method for this in the generic repository like I have done it here:

Or maybe there is a better way? Let me know your thoughts!

https://github.com/MiBuena/ListGeneratorAsync/blob/19093c731337d7b1b2a91963e047873142f4d9de/ListGeneration.Data/Repositories/EfRepository.cs#L30

@NikolayIT
Copy link
Owner

Yes, you are absolutely right. ToListAsync is missing in the current EfRepository implementation.
Anyway you can use it after calling the .All() method.

@MiBuena
Copy link
Author

MiBuena commented Dec 8, 2021

Hello, Niki! Could you please clarify:

Shall I use it in the Service like this: _itemsRepository.All().ToListAsync(); ?

Or leave it as a wrapper method in the repository and calling it like this?

@NikolayIT
Copy link
Owner

I suggest you using it like await repo.All().ToListAsync() in case you want all records and of course await repo.All().Where(x => ...).ToListAsync() in case you want some filtering.

@MiBuena
Copy link
Author

MiBuena commented Dec 8, 2021

Ok, but if I put it like this, 2 problems arrise:

  1. I will have to add the following using the Service: using Microsoft.EntityFrameworkCore;
    .ToListAsync() requires it. This I believe creates a dependency between the Service and EF?

  2. After that I get an Exception in the Unit test - "The source IQueryable doesn't implement IAsyncEnumerable<ListGenerator.Shared.Dtos.ItemNameDto>. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations."

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

No branches or pull requests

2 participants