Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

DomainService, use of .Include() #3006


User avatar
0
bilalhaidar created

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)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    As far as I know, you don't need to call GetAllIncluding using await, you can directly execute it.

  • User Avatar
    0
    bilalhaidar created

    But my AppService/DomainService are async. So I should await on something.

    Any idea? Thanks

  • User Avatar
    0
    bilalhaidar created

    any updates, please?

  • User Avatar
    0
    ismcagdas created
    Support Team

    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>.

  • User Avatar
    0
    bilalhaidar created

    Sometimes it's the only method call.

  • User Avatar
    0
    ismcagdas created
    Support Team

    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.

  • User Avatar
    0
    bilalhaidar created

    And in case I want a list?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    In that case you can use ToListAsync(). I think most of the methods has Asyc versions.

  • User Avatar
    0
    bilalhaidar created

    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

  • User Avatar
    0
    alirizaadiyahsi created

    Hi,

    You can use it like following

    var result = await _userRepository.GetAllIncluding().ToListAsync();