MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.3k stars 328 forks source link

ProjectToTypeAsync? #526

Closed ftrMorri closed 1 year ago

ftrMorri commented 1 year ago

Is Mapster.Async available only for Adapt(), and not for projections?

What I would like is something like this

public async Task<List<SomeDto>> GetProjectionAsAsync(SomeRequest request)
{
    var data = _db.SomeData.AsQueryable();

    // Database operation should be async
    return await data.ProjectToTypeAsync<List<SomeDto>>();
}

I'm not sure if I understand the inner workings correctly, but wouldn't this still execute the database call + projection synchronously, and just the ToList() operation asynchronously?

return await data.ProjectToType<List<SomeDto>>().ToListAsync();

andrerav commented 1 year ago

Mapster already has some underlying issues that can cause problems when using ProjectToType() in certain situations that involve async code. These issues will need to be resolved first. It is advisable to materialize queries with ToList() first before calling ProjectToType().

wsloth commented 3 months ago

@andrerav I know this is a relatively old and already closed issue, but what are you specifically referring to when it comes to async issues in relation to Mapster? I noticed I'm still using ProjectToType in an async context in many places, but was unaware of it causing any issues.