For my objects, I am using Csla, which has a BrokenRulesCollection property. I would like to convert that to my own DTO which has StatusMessages property.
I created my own resolver:
public class BrokenRulesCollectionResolver : ValueResolver<Csla.Validation.BrokenRulesCollection, StatusMessageList>
{
protected override StatusMessageList ResolveCore(Csla.Validation.BrokenRulesCollection source)
{
var messageList = new StatusMessageList();
messageList.ReadBrokenRules(source);
return messageList;
}
}
And in the mapping, I’m letting it know which resolver to use:
Mapper.CreateMap<DomainObjects.Members.IMemberRegistration, DTO.Members.MemberRegistrationForm>()
.ForMember(src => src.StatusMessages, opt => opt.ResolveUsing <BrokenRulesCollectionResolver>());
However, when I try to do the mapping:
return Mapper.Map<DomainObjects.Members.IMemberRegistration, DTO.Members.MemberRegistrationForm>(memberRegistration);
I get the following error:
Value supplied is of type Csla.Validation.BrokenRulesCollection but expected Favs.DomainObjects.Members.MemberRegistration.
Change the value resolver source type, or redirect the source value supplied to the value resolver using FromMember.
Any suggestions?
Edit:
As a follow up, I have also tried to create a convert but still get the same message:
public class BrokenRulesCollectionConverter : ITypeConverter<Csla.Validation.BrokenRulesCollection, StatusMessageList>
{
public StatusMessageList Convert(ResolutionContext context)
{
var test = new StatusMessageList();
test.ReadBrokenRules((Csla.Validation.BrokenRulesCollection)context.SourceValue);
return test;
}
}
And configure it as follows:
Mapper.CreateMap<Csla.Validation.BrokenRulesCollection, StatusMessageList>()
.ConvertUsing<BrokenRulesCollectionConverter>();
The instance that AutoMapper passes into
ResolveCorehere is not theBrokenRulesCollection– AutoMapper doesn’t know which property of theIMemberRegistrationto get that from. When you use a custom resolver, it gets an instance of the same object you are trying to map.It should work if you rewrite your first class like so:
Note – I’m assuming that the member you want to map on
IMemberRegistrationis a property calledBrokenRules. Change this to whatever applies.Edit – you can also do what the message suggests and use
FromMember:Again, this assumes the property is named
BrokenRules. You have to tell AutoMapper, it can’t guess in this case.