As I understand your question, you want to use ValueResolver , which resolves several source properties into an intermediate data object, which is used to resolve several target properties. As an example, I assume the following types of sources, target, intermediate, and permissive:
// source class User { public string UserTitle { get; set; } } // target class UserViewModel { public string VM_Title { get; set; } public string VM_OtherValue { get; set; } } // intermediate from ValueResolver class UserTitleParserResult { public string TransferTitle { get; set; } } class TypeValueResolver : ValueResolver<User, UserTitleParserResult> { protected override UserTitleParserResult ResolveCore(User source) { return new UserTitleParserResult { TransferTitle = source.UserTitle }; } }
To use opt.ResolveUsing<TypeValueResolver>() , the target property is required. This means that you can set the mapping in which the corresponding target property is available.
So, for now, let's wrap the result in the appropriate container type:
class Container<TType> { public TType Value { get; set; } }
And create a mapping
Mapper.CreateMap<User, Container<UserViewModel>>() .ForMember(d => d.Value, c => c.ResolveUsing<TypeValueResolver>());
And another display
Mapper.CreateMap<UserTitleParserResult, UserViewModel>() .ForMember(d => d.VM_Title, c => c.MapFrom(s => s.TransferTitle)) .ForMember(d => d.VM_OtherValue, c => c.Ignore());
And another display
Mapper.CreateMap<User, UserViewModel>() .BeforeMap((s, d) => { Mapper.Map<User, Container<UserViewModel>>(s, new Container<UserViewModel> { Value = d }); }) .ForAllMembers(c => c.Ignore());
The latter mapping is a bit special, as it relies on a nested mapping to update the receiver with values ββfrom the source through separately configured matching rules. You can have several different carry mappings for different properties by adding the appropriate intermediate mappings and calls to the BeforeMap actual display type. Properties that are handled in other mappings must be ignored because AutoMapper is unaware of the mapping in BeforeMap
A small usage example:
var user = new User() { UserTitle = "User 1" }; // create by mapping UserViewModel vm1 = Mapper.Map<UserViewModel>(user); UserViewModel vm2 = new UserViewModel() { VM_Title = "Title 2", VM_OtherValue = "Value 2" }; // map source properties into existing target Mapper.Map(user, vm2);
I donβt know if this will help you. There may be better ways if you rephrase your question to describe your original problem, rather than what you think is the solution.