Suppose i have a source class:
public class Source
{
//Several properties that can be mapped to DerivedBase and its subclasses
}
And some destination classes:
public class DestinationBase
{
//Several properties
}
public class DestinationDerived1 : DestinationBase
{
//Several properties
}
public class DestinationDerived2 : DestinationBase
{
//Several properties
}
Then I wish the derived destination classes to inherit the automapper configuration of the baseclass because I do not want to have to repeat it, is there any way to achieve this?
Mapper.CreateMap<Source, DestinationBase>()
.ForMember(...)
// Many more specific configurations that should not have to be repeated for the derived classes
.ForMember(...);
Mapper.CreateMap<Source, DestinationDerived1 >()
.ForMember(...);
Mapper.CreateMap<Source, DestinationDerived2 >()
.ForMember(...);
When I write it like this it does not use the base mappings at all, and include doesn't seem to help me.
Edit:
This is what I get:
public class Source
{
public string Test { get; set; }
public string Test2 { get; set; }
}
public class DestinationBase
{
public string Test3 { get; set; }
}
public class DestinationDerived1 : DestinationBase
{
public string Test4 { get; set; }
}
public class DestinationDerived2 : DestinationBase
{
public string Test5 { get; set; }
}
Mapper.CreateMap<Source, DestinationBase>()
.ForMember(d => d.Test3, e => e.MapFrom(s => s.Test))
.Include<Source, DestinationDerived1>()
.Include<Source, DestinationDerived2>();
Mapper.CreateMap<Source, DestinationDerived1>()
.ForMember(d => d.Test4, e => e.MapFrom(s => s.Test2));
Mapper.CreateMap<Source, DestinationDerived2>()
.ForMember(d => d.Test5, e => e.MapFrom(s => s.Test2));
AutoMapper.AutoMapperConfigurationException :
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
Source -> DestinationDerived1 (Destination member list)
Test3
Include derived mappings into base mapping:
Mapper.CreateMap<Source, DestinationBase>()
.ForMember(d => d.Id, op => op.MapFrom(s => s.Id)) // you can remove this
.Include<Source, DestinationDerived1>()
.Include<Source, DestinationDerived2>();
Mapper.CreateMap<Source, DestinationDerived1>()
.ForMember(d => d.Name, op => op.MapFrom(s => s.Text))
.ForMember(d => d.Value2, op => op.MapFrom(s => s.Amount));
Mapper.CreateMap<Source, DestinationDerived2>()
.ForMember(d => d.Value, op => op.MapFrom(s => s.Amount));
Usage:
Mapper.AssertConfigurationIsValid();
var s = new Source() { Id = 2, Amount = 10M, Text = "foo" };
var d1 = Mapper.Map<DestinationDerived1>(s);
var d2 = Mapper.Map<DestinationDerived2>(s);
See Mapping inheritance on AutoMapper wiki.
UPDATE: Here is full code of classes which works as it should.
public class Source
{
public int Id { get; set; }
public string Text { get; set; }
public decimal Amount { get; set; }
}
public class DestinationBase
{
public int Id { get; set; }
}
public class DestinationDerived1 : DestinationBase
{
public string Name { get; set; }
public decimal Value2 { get; set; }
}
public class DestinationDerived2 : DestinationBase
{
public decimal Value { get; set; }
}
UPDATE (workaround of AutoMapper bug):
public static class Extensions
{
public static IMappingExpression<Source, TDestination> MapBase<TDestination>(
this IMappingExpression<Source, TDestination> mapping)
where TDestination: DestinationBase
{
// all base class mappings goes here
return mapping.ForMember(d => d.Test3, e => e.MapFrom(s => s.Test));
}
}
And all mappings:
Mapper.CreateMap<Source, DestinationBase>()
.Include<Source, DestinationDerived1>()
.Include<Source, DestinationDerived2>()
.MapBase();
Mapper.CreateMap<Source, DestinationDerived1>()
.MapBase()
.ForMember(d => d.Test4, e => e.MapFrom(s => s.Test2));
Mapper.CreateMap<Source, DestinationDerived2>()
.MapBase()
.ForMember(d => d.Test5, e => e.MapFrom(s => s.Test2));
For Automapper 8.0.
Current version has new method IncludeAllDerived
Here's working example:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source, DestinationBase>()
.ForMember(dest => dest.Test3, opt => opt.MapFrom(src => src.Test))
.IncludeAllDerived();
cfg.CreateMap<Source, DestinationDerived1>()
.ForMember(dest => dest.Test4, opt => opt.MapFrom(src => src.Test2));
cfg.CreateMap<Source, DestinationDerived2>()
.ForMember(dest => dest.Test5, opt => opt.MapFrom(src => src.Test2));
});
var mapper = config.CreateMapper();
var source = new Source { Test = "SourceTestProperty", Test2 = "SourceTest2Property" };
var d1 = mapper.Map<DestinationDerived1>(source);
var d2 = mapper.Map<DestinationDerived2>(source);
Assert.Equal("SourceTestProperty", d1.Test3);
Assert.Equal("SourceTest2Property", d1.Test4);
Assert.Equal("SourceTestProperty", d2.Test3);
Assert.Equal("SourceTest2Property", d2.Test5);
NB! For those who are having issues with derived interfaces. AutoMapper does not support registering against derived interfaces. Only classes are handled.
To make it work, you have to change your type reference for CreateMap to the class instead of interface.
Example:
interface Interface1 {}
class Class1: Interface1 {}
interface Interface2: Interface1 {}
class Class2: Class1, Interface2 {}
CreateMap<OtherClass, Interface1>().IncludeAllDerived();
CreateMap<OtherClass, Interface2>();
Any mapping against Interface2 will only use the first CreateMap. You will have to identify the second as
CreateMap<OtherClass, Class2>();
Related
Assuming I have a class structure like
public class Entity
{
public List<EntityChild> Children { get; set; }
}
public class EntityChild
{
public int Id { get; set; }
public string Name { get; set; }
}
and I want to map Entity using AutoMapper to a class EntityDto and reverse.
public class EntityDto
{
public List<int> EntityChildrenIds { get; set; }
}
I don't have any clue how to configure AutoMapper to map this properly in both directions. I know my Name property will be null when mapping from EntityDto to Entity but this would not be a problem.
For mapping both ways this configuration works for me:
var mapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Entity, EntityDto>()
.ForMember(dest => dest.EntityChildrenIds, opt => opt.MapFrom(src => src.Children))
.ReverseMap();
cfg.CreateMap<EntityChild, int>().ConvertUsing(child => child.Id);
cfg.CreateMap<int, EntityChild>().ConvertUsing(id => new EntityChild
{
Id = id
});
});
Since the properties have different names we need to configure that mapping.
Then just add general mappings from EntityChild to int and back again and we're done.
if .ReverseMap(), as mentioned by #knoop, didn't work maybe you should map it manually:
CreateMap<Entity, EntityDto>(MemberList.None)
.ForMember(dest => dest.EntityChildrenIds, opts => opts.MapFrom(src => MapChildrenIds(src.Children)));
CreateMap<EntityDto, Entity>(MemberList.None)
.ForMember(dest => dest.Children, opts => opts.MapFrom(src => MapChildren(src.EntityChildrenIds)));
private List<EntityChild> MapChildren(List<int> entityChildrenIds)
{
var listEntityChild = new List<EntityChild>();
foreach (var childId in entityChildrenIds)
listEntityChild.Add(new EntityChild { Id = childId });
return listEntityChild;
}
private List<int> MapChildrenIds(List<EntityChild> children)
{
return children.Select(x => x.Id).ToList();
}
I want map Origin.CityId and Origin.StateId properties of
Itinerary class to OriginCityId and OriginStateId properties
of ItineraryModel class.
Ex: Itinerary itinerary = Mapper.Map<Itinerary>(ItineraryModel);
My ViewModel
public class ItineraryModel : BaseModel
{
public int OriginCityId { get; set; }
public int OriginStateId { get; set; }
public bool Published { get; set; }
}
My Entity
public class Itinerary : BaseEntity
{
public City Origin { get; set; }
public bool Published { get; set; }
}
My mapping that tried do
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<ItineraryModel, Itinerary>()
.ForPath(x => x.Origin.CityId, opt => opt.MapFrom(src => src.OriginCityId))
.ForPath(x => x.Origin.StateId, opt => opt.MapFrom(src => src.OriginStateId))
.ReverseMap();
}
}
I would like to too .ReverseMap() but can't find right syntax.
You need to add two mappings for mapping to Itenerary
CreateMap<ItineraryModel, City>()
.ForMember(city => city.CityId, expression => expression.MapFrom(itineraryModel => itineraryModel.OriginCityId))
.ForMember(city => city.StateId, expression => expression.MapFrom(itineraryModel => itineraryModel.OriginStateId));
CreateMap<ItineraryModel, Itinerary>()
.ForMember(itinerary => itinerary.Origin, expression => expression.MapFrom(itineraryModel => itineraryModel));
Similarly you can define reverse mappings manually if needed.
BTW ReverseMap() is not recommended by author
https://jimmybogard.com/automapper-usage-guidelines/
I have DO and DTO classes:
public class DO
{
public int Property1 { get; set; }
public int Property2 { get; set; }
}
public class DTO
{
public int MappedProperty1 { get; set; }
public int MappedProperty2 { get; set; }
}
The mapping registration for the above classes are like below.
// From DO to DTO:
configuration.CreateMap<DO, DTO>()
.ForMember(d => d.MappedProperty1, o => o.MapFrom(s => s.Property1))
.ForMember(d => d.MappedProperty2, o => o.MapFrom(s => s.Property1 + s.Property2));
// From DTO to DO:
configuration.CreateMap<DTO, DO>()
.ForMember(d => d.Property1, o => o.MapFrom(s => s.MappedProperty1))
.ForMember(d => d.Property2, o => o.MapFrom(s => s.MappedProperty1 - s.MappedProperty2));
Elsewhere in the code I need to get the destination property names in the mapping. I'm currently doing this by:
var map= mapper.ConfigurationProvider.FindTypeMapFor<DTO, DO>();
var property = map.GetPropertyMaps().First(f => f.SourceMember == typeof(DTO).GetProperty("MappedProperty1"));
var name = property.DestinationProperty.Name;
This works fine with MappedProperty1. However, it does not work with MappedProperty2 as there are two properties participating in the mapping. Instead, I get the error message:
"Sequence contains no matching element".
I also tried with the following instead of First filter and in debug I can see that the count in the list is zero.
var map= mapper.ConfigurationProvider.FindTypeMapFor<DTO, DO>();
var property = map.GetPropertyMaps().Where(w => w.SourceMember == typeof(DTO).GetProperty("MappedProperty2")).ToList();
How can I achieve this source name resolution for multi-property mapping?
I have 2 objects that I need to map to each other. They look like
public class Example1
{
CustomType1 Prop { get; set; }
List<CustomType1> List { get; set; }
}
public class Example2
{
Customtype2 Prop { get; set; }
List<Customtype2> List { get; set; }
}
public class CustomType1
{
public string SomeString { get; set; }
}
public class Customtype2
{
public string FirstPartOfSomeString { get; set; }
public string SecondPartOfSomeString { get; set; }
}
I want to make one CustomResolver that maps CustomType1 to CustomType2 and then use that resolver on the list. For example,
Mapper.CreateMap<Example1, Example2>()
.ForMember(d => d.Prop, opt => opt.ResolveUsing(myCustomResolver))
.ForMember(d => d.List, opt => opt.ResolveUsing( /*use myCustomResolver on a list here*/));
I have tried using something like:
Mapper.CreateMap<Example1, Example2>()
.ForMember(d => d.Prop, opt => opt.ResolveUsing(myCustomResolver))
.ForMember(d => d.List, opt => opt.MapFrom(s => s.List.Select(myCustomResolver.Resolve).ToList()));
but I seem to be missing something. Is there a way to do this with AutoMapper?
Have you tried adding a mapping between the custom types instead of using a resolver?
AutoMapper is intelligent enough to re-use mappings for lists...
Mapper.CreateMap<CustomType1, CustomType2>()
.ForMember(x => FirstPartOfSomeString, opts => opts.MapFrom(x => x.SomeString.Substring(5)))
.ForMember(x => SecondPartOfSomeString, opts => opts.MapFrom(x => x.SomeString.Substring(5, 5)));
Mapper.CreateMap<Example1, Example2>();
I'm facing a problem with AutoMapper 2.1.267.0 when working with objects containing collections of derived classes.
I've isolated my problem in a simpler scenario with the following classes:
public class ClassABase
{
public string Name { get; set; }
}
public class ClassA : ClassABase
{
public string Description { get; set; }
}
public class ClassBBase
{
public string Title { get; set; }
}
public class ClassB : ClassBBase
{
public string Text { get; set; }
}
public class ContainerA
{
public IList<ClassA> ClassAList { get; set; }
public ClassA ClassA { get; set; }
}
public class ContainerB
{
public IList<ClassB> ClassBList { get; set; }
public ClassB ClassB { get; set; }
}
and these mappings
public class ClassABaseToClassBBase : Profile
{
protected override void Configure()
{
CreateMap<ClassABase, ClassBBase>()
.Include<ClassA, ClassB>()
.ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Name));
Mapper.AssertConfigurationIsValid();
}
}
public class ClassAToClassB : Profile
{
protected override void Configure()
{
CreateMap<ClassA, ClassB>()
.ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Description));
Mapper.AssertConfigurationIsValid();
}
}
public class ContainerAToContainerB : Profile
{
protected override void Configure()
{
CreateMap<ContainerA, ContainerB>()
.ForMember(dest => dest.ClassBList,
opt => opt.MapFrom(src => Mapper.Map<IList<ClassA>, IList<ClassB>>(src.ClassAList)))
.ForMember(dest => dest.ClassB, opt => opt.MapFrom(src => src.ClassA));
}
}
Here is the initialization
Mapper.Initialize(x =>
{
x.AddProfile<ClassABaseToClassBBase>();
x.AddProfile<ClassAToClassB>();
x.AddProfile<ContainerAToContainerB>();
});
and a diagram summarizing everything (Red arrows represent AutoMapper mappings)
I've mapped a ContainerA instance to ContainerB. This first scenario works properly. The destination object is fully filled as shown in the image bellow:
But if I add this mapping
public class ClassBBaseToClassB : Profile
{
protected override void Configure()
{
CreateMap<ClassBBase, ClassB>()
.ForMember(dest => dest.Text, opt => opt.Ignore());
Mapper.AssertConfigurationIsValid();
}
}
Mapper.Initialize(x =>
{
x.AddProfile<ClassABaseToClassBBase>();
x.AddProfile<ClassAToClassB>();
x.AddProfile<ClassBBaseToClassB>();
x.AddProfile<ContainerAToContainerB>();
});
the result is
The "Text" properties of all elements of the collection become null. As you can see in the image, the "Text" property of the containerB.ClassB object is still mapped correctly. This only occurs with collections. I don't know what's happening. Any clues?
Thanks
Hi,
Try to change the mapping between containers in this way :
public class ContainerAToContainerB : Profile
{
protected override void Configure()
{
CreateMap<ContainerA, ContainerB>()
.ForMember(dest => dest.ClassBList,opt=>opt.MapFrom(src=>src.ClassAList))
.ForMember(dest => dest.ClassB, opt => opt.MapFrom(src => src.ClassA));
}
}