I expect the following test to fail, but it doesn't. How can I configure AutoMapper to be case sensitive?
public class AutomapperTests
{
[Fact]
public void CaseSensitiveTest()
{
Mapper.Initialize(cfg => cfg.AddMemberConfiguration().AddName<CaseSensitiveName>());
Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>());
Mapper.AssertConfigurationIsValid();
}
public class Source
{
public int Foo { get; set; }
}
public class Destination
{
public int FoO { get; set; }
}
}
I'm using version 5.1.1 of AutoMapper.
Take a look at the naming convention configurations: https://github.com/AutoMapper/AutoMapper/wiki/Configuration#naming-conventions
At the Profile or Mapper level you can specify the source and destination naming conventions:
Mapper.Initialize(cfg => {
cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
});
Or:
public class OrganizationProfile : Profile
{
public OrganizationProfile()
{
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
//Put your CreateMap... Etc.. here
}
}
Related
I was trying to implement a code to ignore a property (therefore mantaining the source value). I used the ignore method, which works most of the time. For some reason I noticed that sometimes the ignore sets the property value to null.
Do you know what could be problem?
I created the following code to reproduce the issue. I was expecting client.ContactDetails.First().Address to have the value "Old".
using AutoMapper;
class Program
{
static void Main(string[] args)
{
ClientMapperProfile clientMapperProfile = new ClientMapperProfile();
var configurationProvider = new MapperConfiguration(c => c.AddProfile(clientMapperProfile));
Mapper mapper = new Mapper(configurationProvider);
var client = new Client()
{
ContactDetails = new []
{
new ContactDetails()
{
Address= "Old"
}
}
};
var clientDto = new ClientDto()
{
ContactDetails = new []
{
new ContactDetailsDto()
{
Address = "New"
}
}
};
mapper.Map(clientDto,client);
Console.WriteLine(client.ContactDetails.First().Address);
}
}
public class Client
{
public ContactDetails[] ContactDetails { get; set; }
}
public class ContactDetails
{
public string Address { get; set; }
}
public class ClientDto
{
public ContactDetailsDto[] ContactDetails { get; set; }
}
public class ContactDetailsDto
{
public string Address { get; set; }
}
public class ClientMapperProfile : Profile
{
public ClientMapperProfile()
{
CreateMap<ClientDto, Client>();
CreateMap<ContactDetailsDto, ContactDetails>()
.ForMember(c => c.Address, opt => opt.Ignore());
}
}
Automapper with complex nested mapping. I am trying to map mydestinationArrayField and
dest1Array, here source objectlist to be copied to dest1array.
here are my classes for source and destination.
namespace AutomapperDemo
{
class Program
{
static void Main(string[] args)
{
try
{
SourceObject request = new SourceObject()
{
sourceTypeField = "1",
SourceObj1Field = new SourceObj1
{
SourceObj1Id = "1",
SourceObjListss = new List<SourceInnerObjList>
{
new SourceInnerObjList
{
SourceObjListItem1Id = 1
},
new SourceInnerObjList
{
SourceObjListItem1Id = 2
}
}
}
};
var mapper = CreateMapper();
DestinationObject destination = new DestinationObject();
destination = mapper.Map<DestinationObject>(request);
}
catch (Exception ex)
{
throw ex;
}
}
public static IMapper CreateMapper()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AllowNullDestinationValues = true;
cfg.CreateMap<SourceObject, DestinationObject>()
.ForMember(dest => dest.destinationTypeField, o => o.MapFrom(src => src.sourceTypeField))
.ForMember(dest => dest.destinationObjectArrayField, o => o.MapFrom(src => new destinationObjectArray()
{
mydestinationArrayField = src.SourceObj1Field.SourceObjListss.Select(x => x.SourceObjListItem1Id).FirstOrDefault().ToString(), //this gives error
//dest1Array = src.SourceObj1Field.SourceObjListss // here source objectlist to be copied to dest1array
}));
});
return config.CreateMapper();
}
}
}
namespace Automapper
{
public class SourceObject
{
public string sourceTypeField;
public SourceObj1 SourceObj1Field { get; set; }
}
public class SourceObj1
{
public string SourceObj1Id { get; set; }
public ICollection<SourceInnerObjList> SourceObjListss { get; set; }
}
public class SourceInnerObjList
{
public int SourceObjListItem1Id { get; set; }
public int SourceObjListItem2d { get; set; }
}
public class SourceInnerObj2List
{
public int? mycount { get; set; }
public int? yourcount { get; set; }
}
}
namespace Automapper
{
public class DestinationObject
{
public string destinationTypeField;
public destinationObjectArray[] destinationObjectArrayField;
}
public class destinationObjectArray
{
public string mydestinationArrayField;
public string myField1;
public destinationInnerObject1Array[] dest1Array;
public destinationInnerObject2Array[] dest2Array;
}
public class destinationInnerObject1Array
{
public string destinationInnerObjectItem11;
public string destinationInnerObjectItem21;
}
public class destinationInnerObject2Array
{
public string categoryTypeField;
public string valueField;
public string NumberField;
}
}
While executing the mapping i am getting "Missing type map configuration or unsupported mapping."
No matter how I configure ignores or custom mappings it seems to not like this nesting. Any Automapper experts out there who could tell me how a mapping with a complex object like this could be done.
It seems like your second ForMember doesn't work:
.ForMember(dest => dest.destinationObjectArrayField, o => o.MapFrom(src => new destinationObjectArray() //...
Because in you're defining your map to return destinationObjectArray()
and not destinationObjectArray[]!
So there is a map like:
destinationObjectArrayField -> destinationObjectArray()
and there is no map like:
destinationObjectArrayField -> destinationObjectArray[]
And Automapper is telling you that.
You should do something like this:
cfg.CreateMap<SourceObject, DestinationObject>()
.ForMember(dest => dest.destinationTypeField, o => o.MapFrom(src => src.sourceTypeField))
.ForMember(dest => dest.destinationObjectArrayField,
o => o.MapFrom(
src => src.SourceObj1Field
.SourceObjListss
.Select(x => new destinationObjectArray
{
myField1 = $"first_id: {x.SourceObjListItem2d} second_id: {x.SourceObjListItem1Id}"
})
.ToArray()
));
});
Also cleaning and formatting code is highly suggested, it's seems like you've simply got lost in it! IDE's can help with that.
Properties of type ICollection are not mapped when migrating from Automapper 9 to 10. I am using .NET 5.
I have the following setup:
private class Class1
{
public int Id { get; set; }
}
private class Class2
{
public int Id { get; set; }
public ICollection<Class1> ChildClasses { get; set; }
}
private class Class2Bis
{
public int Id { get; set; }
public ICollection<Class1> ChildClasses { get; set; }
}
private class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<Class2, Class2Bis>().ReverseMap();
}
}
I am injecting the mapper using a helper method that uses AutoMapper.Extensions.Microsoft.DependencyInjection module:
public static IServiceCollection AddMapper(this IServiceCollection services, params Assembly[] assemblies)
{
return services.AddAutoMapper(cfg =>
{
cfg.ForAllMaps((map, exp) => exp.MaxDepth(1));
cfg.AllowNullCollections = true;
cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;
},
assemblies);
}
The following test succeeds using Automapper 9 but fails using Automapper 10:
[Fact]
public void Should_Map_ClassesWithCollectionProperties()
{
var object2 = new Class2
{
Id = 1,
ChildClasses = new List<Class1>()
{
new Class1
{
Id = 1
}
}
};
var object2Bis = _mapper.Map<Class2Bis>(object2);
Assert.Equal(object2.ChildClasses.Count, object2Bis.ChildClasses.Count);
}
object2Bis has an empty ChildClasses property when using Automapper 10.
Removing cfg.ForAllMaps((map, exp) => exp.MaxDepth(1)); or setting MaxDepth to 2 (or higher number) makes the code work for me. There is next note in the 10.0 Upgrade Guide:
When reaching MaxDepth, destination collections are null/empty, they used to contain null values.
Hey I'm trying to map my generic class to concrete class but using it's interface.
My service returns me data which type is
IPaggedResults<Customer>
and I want to be able to map this to
IPaggedResults<CustomerDto>
It works if I invoke mapping with:
_mapper.Map<PaggedResults<CustomerDto>>
but I want use following syntax:
_mapper.Map<IPaggedResults<CustomerDto>>
public class PaggedResults<T> : IPaggedResults<T>
{
public IEnumerable<T> Results { get; protected set; }
public int TotalResults { get; protected set; }
public int TotalPages { get; protected set; }
public int ResultsPerPage { get; protected set; }
public PaggedResults(IEnumerable<T> results, int totalResults, int resultsPerPage)
{
Results = results;
TotalResults = totalResults;
TotalPages = totalResults / resultsPerPage;
ResultsPerPage = resultsPerPage;
}
}
public class CustomerDto
{
public int Id { get; set; }
public string Name { get; set; }
public string NIP { get; set; }
}
My mapper configuration:
public static IMapper Initialize()
=> new MapperConfiguration(cfg =>
{
cfg.CreateMap<CustomerCompany, CustomerDto>();
cfg.CreateMap(typeof(IPaggedResults<>), typeof(PaggedResults<>));
cfg.CreateMap(typeof(IPaggedResults<>), typeof(IPaggedResults<>)).As(typeof(PaggedResults<>));
}).CreateMapper();
Im'using Automapper by Jimmy Bogard.
I could achieve it through the following code:
Create an extension for IMapperConfigurationExpression
public static class IMapperConfigurationExpressionExtensions
{
public static void MapPaggedResults<TSource, TDestination>(this IMapperConfigurationExpression exp){
exp.CreateMap(typeof(PaggedResults<TSource>), typeof(IPaggedResults<TDestination>))
.ConstructUsing((source, ctx) => { return ctx.Mapper.Map<PaggedResults<TDestination>>(source) as IPaggedResults<TDestination>; });
}
}
Then use this configuration:
public static IMapper Initialize()
=> new MapperConfiguration(cfg =>
{
cfg.CreateMap(typeof(IPaggedResults<>), typeof(PaggedResults<>));
cfg.MapPaggedResults<CustomerCompany, CustomerDto>();
}).CreateMapper();
Then both results can be obtained:
var _mapper = Initialize();
IPaggedResults<CustomerCompany> source = new PaggedResults<CustomerCompany>(
new List<CustomerCompany>() { new CustomerCompany() {Id =42, Name = "SomeName", NIP = "someNIP" } }, 1, 1);
var resut = _mapper.Map<PaggedResults<CustomerDto>>(source);
var resut2 = _mapper.Map<IPaggedResults<CustomerDto>>(source);
I have two types. One in the business layer:
namespace Business
{
public class Car
{
private int _id;
private string _make;
private string _model;
public int id
{
get { return _id; }
set { _id = value; }
}
public string make
{
get { return _make; }
set { _make = value; }
}
public string model
{
get { return _model; }
set { _model = value; }
}
}
}
and the other in the Data layer (Entity Framework):
namespace Data
{
using System;
using System.Collections.Generic;
public partial class Car
{
public Car()
{
this.facttables = new HashSet<facttable>();
}
public int id { get; set; }
public string make { get; set; }
public string model { get; set; }
public virtual ICollection<facttable> facttables { get; set; }
}
}
Here is the code I get from the service layer:
namespace Data
{
public class VehicleDAO : IVehicleDAO
{
private static readonly ILog log = LogManager.GetLogger(typeof(VehicleDAO));
MapperConfiguration config;
public VehicleDAO ()
{
Mapper.Initialize(cfg => cfg.CreateMap<Business.Car, Data.Car>());
config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Business.Car, Data.Car>()
.ForMember(dto => dto.facttables, opt => opt.Ignore());
//.ForMember(d => d.id, opt => opt.MapFrom(c => c.id))
//.ForMember(d => d.make, opt => opt.MapFrom(c => c.make))
//.ForMember(d => d.model, opt => opt.MapFrom(c => c.model));
});
config.AssertConfigurationIsValid();
}
public Data.Car Select(int id)
{
Data.Car car;
using (VehicleEntities VehicleDatabase = new VehicleEntities())
{
car = VehicleDatabase.Cars.Where(c => c.id == id).ToList().Single();
Business.Car cars = AutoMapper.Mapper.Map<Business.Car>(car);
}
return car;
}
The exception is: {"Missing type map configuration or unsupported mapping.\r\n\r\nMapping types:\r\nCar_70BD8401A87DAAD8F5F0EC35BCAE5C9E6EE2D6CB5A1AFCE296B313D8AD87D2E9 -> Car\r\nSystem.Data.Entity.DynamicProxies.Car_70BD8401A87DAAD8F5F0EC35BCAE5C9E6EE2D6CB5A1AFCE296B313D8AD87D2E9 -> Business.Car"}. What is wrong? I have marked the line that causes the exception (third from last line).
Automapper only maps in the direction you created the mapping in. CreateMap<Business.Car, Data.Car> creates a mapping from Business.Car to Data.Car. It looks like you are trying to map from Data.Car to Business.Car, which means you need to CreateMap<Data.Car, Business.Car>
Mapper.Initialize(cfg => cfg.CreateMap<Data.Car, Business.Car>());
config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Data.Car, Business.Car>();
});
config.AssertConfigurationIsValid();