Using MapFrom
for Recursive and Custom Mapping
The MapFrom
helper allows you to reuse existing mappings inside another mapping configuration.
It's especially useful for nested objects and collections, and provides functionality similar to AutoMapper's context.Mapper.Map(...)
.
๐ง What does it do?
It gives you access to the Konverter instance within a ForMember
call, so you can call:
konverter.Map<TSource, TDestination>(source)
inside a mapping delegate.
โ How to use
Option 1: Using using static
To enable direct use of MapFrom
without the Map.
prefix:
using static KonverterMap.Map;
Konverter.Instance.CreateMap<Filial, FilialDTO>()
.ForMember(dest => dest.Cidade,
MapFrom<Filial, Cidade, CidadeDTO>(src => src.Cidade))
.ForMember(dest => dest.Empresa,
MapFrom<Filial, Empresa, EmpresaDTO>(src => src.Empresa));
Option 2: Without using static
Use the full prefix when not importing the helper statically:
Konverter.Instance.CreateMap<Filial, FilialDTO>()
.ForMember(dest => dest.Cidade,
Map.MapFrom<Filial, Cidade, CidadeDTO>(src => src.Cidade))
.ForMember(dest => dest.Empresa,
Map.MapFrom<Filial, Empresa, EmpresaDTO>(src => src.Empresa));
๐ ReverseMap compatibility
If you call .ReverseMap()
and youโve already configured:
CreateMap<Cidade, CidadeDTO>().ReverseMap();
CreateMap<Empresa, EmpresaDTO>().ReverseMap();
The MapFrom
references will work automatically in both directions.
๐งช Full working example
Konverter.Instance.CreateMap<Cidade, CidadeDTO>().ReverseMap();
Konverter.Instance.CreateMap<Empresa, EmpresaDTO>().ReverseMap();
Konverter.Instance.CreateMap<Filial, FilialDTO>()
.ForMember(dest => dest.Cidade,
Map.MapFrom<Filial, Cidade, CidadeDTO>(src => src.Cidade))
.ForMember(dest => dest.Empresa,
Map.MapFrom<Filial, Empresa, EmpresaDTO>(src => src.Empresa))
.ReverseMap();
This enables recursive, clean, and safe mapping for nested objects.