Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Mapster.Async.Tests/AsyncTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using MapsterMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

Expand Down Expand Up @@ -87,6 +88,22 @@ public async Task NestedAsync()
dtoOwnership.Owner.Name.ShouldBe("John Doe");
}

[TestMethod]
public async Task SimplyAsync()
{
TypeAdapterConfig<Poco, Dto>.NewConfig()
.AfterMappingAsync(async dest => { dest.Name = await GetName(); });

var poco = new Poco { Id = "foo" };
var dto = await poco.AdaptAsync<Dto>();
dto.Name.ShouldBe("bar");

IMapper instance = new Mapper();

var destination = await instance.MapAsync<Dto>(poco);
destination.Name.ShouldBe("bar");
}

private static async Task<string> GetName()
{
await Task.Delay(1);
Expand Down
42 changes: 41 additions & 1 deletion src/Mapster.Async/TypeAdapterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MapsterMapper;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand Down Expand Up @@ -101,5 +102,44 @@ public static async Task<TDestination> AdaptToAsync<TDestination>(this IAdapterB
}
}


/// <summary>
/// Map asynchronously to destination type.
/// </summary>
/// <typeparam name="TDestination">Destination type to map.</typeparam>
/// <param name="builder"></param>
/// <returns>Type of destination object that mapped.</returns>
public static async Task<TDestination> AdaptAsync<TDestination>(this object? source)
{
return await source.BuildAdapter().AdaptToTypeAsync<TDestination>();
}


/// <summary>
/// Map asynchronously to destination type.
/// </summary>
/// <typeparam name="TDestination">Destination type to map.</typeparam>
/// <param name="builder"></param>
/// <param name="config">Configuration</param>
/// <returns>Type of destination object that mapped.</returns>
public static async Task<TDestination> AdaptAsync<TDestination>(this object? source, TypeAdapterConfig config)
{
return await source.BuildAdapter(config).AdaptToTypeAsync<TDestination>();
}

}

public static class IMapperAsyncExtentions
{
/// <summary>
/// Map asynchronously to destination type.
/// </summary>
/// <typeparam name="TDestination">Destination type to map.</typeparam>
/// <param name="builder"></param>
/// <returns>Type of destination object that mapped.</returns>
public static async Task<TDestination> MapAsync<TDestination>(this IMapper mapper, object? source)
{
return await mapper.From(source).AdaptToTypeAsync<TDestination>();
}
}
}
Loading