0
 public class Complex
{
    public A A { get; set; }
    public A B { get; set; }
}

public class A
{
    public int a1 { get; set; }
    public int a2 { get; set; }
}

public class B
{
    public int b1 { get; set; }
    public int b2 { get; set; }
}
//----------------Source Object End Here---------------------

public class Simple  <----[This Simple class has only properties of A class]
{
    public int aa1 { get; set; }
    public int aa2 { get; set; }
}
//----------------Destination Object End Here---------------------

CreateMap<A, Simple>()
    .ForMember(dest => dest.aa1, opt => opt.MapFrom(src => src.a1))
    .ForMember(dest => dest.aa2, opt => opt.MapFrom(src => src.a2))

// Mapper IS NOT AVAILABLE HERE AS I AM USING PROFILE BASED CONFIGURATION
CreateMap<Complex, Simple>()
    .ConvertUsing(src => Mapper.Map<A, Simple>(src.A)); <------Error at this line

//----------------Automammer config End Here---------------------

How to flatten from Complex to Simple? I don't wish to map Complex.A to Simple one by one again in the Complex to Simple config as it is already configured above.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Premchandra Singh
  • 14,156
  • 4
  • 31
  • 37

1 Answers1

0

Finally, I figured out with another overloaded method of ConvertUsing

CreateMap<Complex, Simple>()
.ConvertUsing((src,ctx) => {
     return ctx.Mapper.Map<Complex, Simple>(src.A)
}); 

I feel this overloaded method has quite a multiple possibilities and flexibility. I don't have further issue of accessing Mapper directly as mentioned in the question. This overloaded method has its own context parameter (ResolutionContext). We can use Mapper from this context parameter like ctx.Mapper.Map<Complex, Simple>(src.A)

Premchandra Singh
  • 14,156
  • 4
  • 31
  • 37