1

I have a 'database' class named User:

class SqlUser { public int Id {get;set;} }

and a DTO for it:

class User { public int UserId {get;set;} }

Is there a way to map this automatically using Mapster? It does not work by default. If it does require configuration, how do I pass the TypeAdapterConfiguration<T,U> object to Adapt method? It only accept a non-generic TypeAdapterConfiguration class.

Dmitry Arestov
  • 1,427
  • 12
  • 24

1 Answers1

1

You don't need to pass it to Adapt method. One option is to call NewConfig and Map:

TypeAdapterConfig<SqlUser, User>.NewConfig()
    .Map(dest => dest.UserId, src => src.Id);
var adapt = new SqlUser { Id = 1 }.Adapt<User>();

Or use Prefix/Replace approach:

TypeAdapterConfig<SqlUser, User>.NewConfig()
    .NameMatchingStrategy(NameMatchingStrategy.ConvertDestinationMemberName(name => name.Replace("User", "")));
Guru Stron
  • 102,774
  • 10
  • 95
  • 132