Display a list of one type, another

For my objects, I use Csla, which has the BrokenRulesCollection property. I would like to convert this to my own DTO, which has the 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 comparison, I make it clear 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 a mapping:

  return Mapper.Map<DomainObjects.Members.IMemberRegistration, DTO.Members.MemberRegistrationForm>(memberRegistration); 

I get the following error:

The given value is of type Csla.Validation.BrokenRulesCollection, but Favs .DomainObjects.Members.MemberRegistration is expected. Change the source type of the value resolver or redirect the original value supplied to the value converter using FromMember.

Any suggestions?

Edit:

As a continuation, I also tried to create a converter, but I 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>(); 
+4
source share
1 answer

The instance that AutoMapper passes to ResolveCore here is not a BrokenRulesCollection - AutoMapper does not know which IMemberRegistration property will receive this. When you use a custom converter, it gets an instance of the same object that you are trying to display.

It should work if you rewrite your first class as follows:

 public class BrokenRulesCollectionResolver : ValueResolver<DomainObjects.Members.IMemberRegistration, StatusMessageList> { protected override StatusMessageList ResolveCore( DomainObjects.Members.IMemberRegistration source) { var messageList = new StatusMessageList(); messageList.ReadBrokenRules(source.BrokenRules); return messageList; } } 

Note. I assume that the member you want to map to IMemberRegistration is a property called BrokenRules . Change it to anything you want.

Edit - you can also do what the message offers and use FromMember :

 Mapper.CreateMap<DomainObjects.Members.IMemberRegistration, DTO.Members.MemberRegistrationForm>() .ForMember(src => src.StatusMessages, opt => opt .ResolveUsing<BrokenRulesCollectionResolver>() .FromMember(r => r.BrokenRules)); 

Again, this assumes the property is called BrokenRules . You have to tell AutoMapper, in which case it cannot guess.

+7
source

Source: https://habr.com/ru/post/1302208/


All Articles