Mappers - MapsterMapper/Mapster GitHub Wiki
You can simply call Adapt
method from anywhere.
var dest = src.Adapt<TSource, TDestination>();
or just
var dest = src.Adapt<TDestination>();
2 extension methods are doing the same thing. src.Adapt<TDestination>
will cast src
to object. Therefore, if you map value type, please use src.Adapt<TSource, TDestination>
to avoid boxing and unboxing.
In some cases, you need an instance of a mapper (or a factory function) to pass into a DI container. Mapster has
the IMapper
and Mapper
to fill this need:
IMapper mapper = new Mapper();
And usage Map
method to perform mapping.
var result = mapper.Map<TDestination>(source);
In most case Adapt
method is enough, but sometimes we need builder to support fancy scenario. Basic example, is to pass run-time value.
var dto = poco.BuildAdapter()
.AddParameters("user", this.User.Identity.Name)
.AdaptToType<SimpleDto>();
Or if you use mapper instance, you can create builder by method From
.
var dto = mapper.From(poco)
.AddParameters("user", this.User.Identity.Name)
.AdaptToType<SimpleDto>();
See Mapster.Tool for generating your specific mapper class, rather than using the provided mappers.