Following up on this answer I got an error. I have three classes, and their respective DTOs:
public class ClassA
{
public string Name { get; set; }
public List<ClassB> Children { get; set; }
}
public class ClassB
{
public int Type { get; set; }
public ClassC Value { get; set; }
public List<ClassC> AltValues { get; set; }
}
public class ClassC
{
public string Name { get; set; }
}
public class ClassADto
{
public string Name { get; set; }
public List<ClassB> Children { get; set; }
}
public class ClassBDto
{
public int Type { get; set; }
public ClassCDto Value { get; set; }
public List<ClassCDto> AltValues { get; set; }
}
public class ClassCDto
{
public string Name { get; set; }
}
I have the following mapping configuration:
public class MyProfile : Profile
{
// the converter is registered in ServiceCollection
public MyProfile(IValueConverter<string, string> valueConverter)
{
this.CreateMap<ClassA, ClassADto>().ReverseMap();
this.CreateMap<ClassB, ClassBDto>()
.BeforeMap((source, destination, context) =>
{
if (context.TryGetItems(out var items))
{
items["ClassBDto"] = source;
}
})
.ReverseMap();
this.CreateMap<ClassC, ClassCDto>();
this.CreateMap<ClassCDto, ClassC>()
.ForMember(field => field.Name, opt => opt.ConvertUsing(valueConverter));
}
}
And the converter itself:
public class MyValueConverter : IValueConverter<string, string>
{
private readonly IDateParser dateParser;
public MyValueConverter(IDateParser parser)
{
this.parser = parser;
}
public string Convert(string sourceMember, ResolutionContext context)
{
if (/*this is where I have to check the Type property of the parent ClassBDto*/)
{
if (this.dateParser.TryParse(sourceMember, out var dt))
{
if (dt < SqlDateTime.MinValue.Value)
{
return string.Empty;
}
return dt.ToString("O");
}
}
return sourceMember;
}
}
But when mapping, I inspected the ResolutionContext and got an error:
Context.Items are only available when using a Map overload that takes Action! Consider using Context.TryGetItems instead.
I observed this error when I put a breakpoint on the var retVal = sourceMember; and observed the ResolutionContext context variable.
This is the screenshot of the observation:

Can someone tell me what I'm doing wrong?
Best Regards
As per documentation stated starting from version 12, it restricts the accessing of
Context.Items.Also, you are only allowed to provide the additional value to
Context.Optionswhen calling map as documentation.I don't see why you try to make it complicated, you can use the
.ForPathto pass and assign value from the parent to the child field as below:Considering your use case requiring accessing the value from the parent object, still
.ForPathis one of the choices but its limitation is that you need to move the whole logic into it as the demo shown previously.If you are looking for a solution that implements a resolver/converter class with your logic, it is not possible. Especially when working with the nested field, as the custom value converter/resolver, type converter doesn't support in
.ForPath.You should implement an
IMappingActionto handle such handling during theAfterMapaction.