Skip to content
chaowlert edited this page Feb 2, 2020 · 1 revision

Async support

PM> Install-Package Mapster.Async

This plugin allows you to perform async operation for mapping

Setup

Use AfterMappingAsync to setup async operation

config.NewConfig<Poco, Dto>()
    .AfterMappingAsync(async (poco, dto) =>
    {
        var userManager = MapContext.Current.GetService<UserManager>();
        var user = await userManager.FindByIdAsync(poco.UserId);
        dto.UserName = user.Name;
    });
Mapping

Then map asynchronously with AdaptToTypeAsync.

var dto = await poco.BuildAdapter()
    .AdaptToTypeAsync<Dto>();

Or like this, if you use mapper instance.

var dto = await _mapper.From(poco)
    .AdaptToTypeAsync<Dto>();
Clone this wiki locally