0

I have two complex objects each has a list in them. I am using AutoMapper to map them together, for example:

ConfigurationStore
    .CreateMap<IProcessDefinition, ProcessDefinition>()
    .ForMember(p => p.ProcessDefinitionDomainId, opt => opt.MapFrom(t => t.DomainId))
    .ForMember(p => p.Schemas, opt => opt.MapFrom(t => t.Schemas))
    .ForMember(p => p.ConfigurationValues, opt => opt.MapFrom(t => t.Configs))
    .ForMember(p => p.Libraries, opt => opt.MapFrom(t => t.Libraries));

In the given example t.Schemas is a list of implementation of ISchemas which is a class with name and value members, both are of type string. On the other hand, p.Schemas is just a list of strings. I have tried resolver approach for so, described here Using AutoMapper to map the property of an object to a string.

It fails because resolver only works with root objects. I have also tried registering a custom between both the schemas, which didn't work either.

t.Schemas = List<string>
p.Schemas = List<ISchema>

public interface ISchema
{
    string name;
    string class;
}

Please let me know if you have any advice for me, much appreciate it.

Community
  • 1
  • 1
OBL
  • 1,347
  • 10
  • 24
  • 45
  • One tiny thing - you don't need MapFrom on properties whose name are the same. You don't need the Schemas or Libraries pieces (it's kinda the whole point of AutoMapper). – Jimmy Bogard Jun 13 '14 at 18:12

1 Answers1

1

You can create another map which transforms an ISchema into a string, using ConvertUsing:

ConfigurationStore.CreateMap<ISchema, string>()
.ConvertUsing(s => s.name);;
Joanvo
  • 5,677
  • 2
  • 25
  • 35