Hi, I always find difficulty in using .Include() in methods inside DomainServices especially when the method is defined as async.
I tend to use:
var shelter = await Task.FromResult(_repository.GetAllIncluding(s => s.ParentShelter).FirstOrDefault(s => s.Id == id));
Is it the right way? Or there is a better one?
Thanks Bilal
10 Answer(s)
-
0
Hi,
As far as I know, you don't need to call GetAllIncluding using await, you can directly execute it.
-
0
But my AppService/DomainService are async. So I should await on something.
Any idea? Thanks
-
0
any updates, please?
-
0
Hi Bilal,
Is there any other line than this one which is async ?
var shelter = await Task.FromResult(_repository.GetAllIncluding(s => s.ParentShelter).FirstOrDefault(s => s.Id == id));
If not, you don't have to define your method async.
Another solution would be converting GetAllIncluding to async but I think that is not possible because of Include does not support asyc. You can check source code of GetAllIncluding here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp.EntityFramework/EntityFramework/Repositories/EfRepositoryBaseOfTEntityAndTPrimaryKey.cs#L49">https://github.com/aspnetboilerplate/as ... Key.cs#L49</a>.
-
0
Sometimes it's the only method call.
-
0
Hi,
I think I have redirected you into a wrong direction, sorry for that. In your case you can use FirstOrDefaultAsync after calling GetAllIncluding(s => s.ParentShelter).
In that way, you won't need Task.FromResult wrapper.
-
0
And in case I want a list?
-
0
Hi,
In that case you can use ToListAsync(). I think most of the methods has Asyc versions.
-
0
Actually ToListAsync is not available in my .Core, can you check on your side?
But one thing I noticed now, the GetAllListAsync() method on the repository accepts a Func where I can provide my query and not only return all the list.
Thanks
-
0
Hi,
You can use it like following
var result = await _userRepository.GetAllIncluding().ToListAsync();