I have a situation where I can call/read SourceType.ExtractAsync() only once per instance but need to share the result across custom value resolvers.
I expected to use the ResolutionContext.Items for these types of tasks and be able to pass data/state from resolver to resolver. However, it's a read-only property.
That's why I've moved the reading part into a BeforeMap action and stored the result for reuse in a static property. However, I am unsure if this can lead to concurrency issues.
How should I exchange state between custom resolvers?
public class MessageProfiles : Profile
{
public static List<string> SharedData = new();
public MessageProfiles()
{
CreateMap<SourceType, DestinationType>()
.BeforeMap((s, _) => SharedData = s.ExtractAsync().Result) // can only be read once!
// ...
.ForMember(d => d.PropertyA, o => o.MapFrom<CustomResolverA>())
.ForMember(d => d.PropertyB, o => o.MapFrom<CustomResolverB>())
;
}
}
public class CustomResolverA : IValueResolver<SourceType, DestinationType, PropertyA>
{
public PropertyA Resolve(SourceType source, DestinationType destination, PropertyA destMember, ResolutionContext context)
{
// Consume MessageProfiles.SharedData
// ...
}
}
public class CustomResolverB : IValueResolver<SourceType, DestinationType, PropertyB>
{
public PropertyB Resolve(SourceType source, DestinationType destination, PropertyB, ResolutionContext context)
{
// Consume MessageProfiles.SharedData
// ...
}
}