Last active
October 12, 2016 07:57
-
-
Save dmdymov/d94a094be72c86fc64b1ceb91ac65673 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AutoMapper; | |
using AutoMapper.EquivilencyExpression; | |
using AutoMapper.Mappers; | |
using System; | |
using System.Collections.Generic; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Mapper.Initialize(x => | |
{ | |
// If you remove this line, mapping will be succesfull in both cases. | |
x.AddProfile<CollectionProfile>(); | |
x.AddProfile<AwesomeProfile>(); | |
}); | |
var dtos = new List<ThingDto>() | |
{ | |
new ThingDto() { ID = 1, Title = "test0" }, | |
new ThingDto() { ID = 2, Title = "test2" } | |
}; | |
var items = new List<Thing>() | |
{ | |
new Thing() { ID = 1, Title = "test1" }, | |
new Thing() { ID = 3, Title = "test3" }, | |
}; | |
var result0 = CreateResultString(Mapper.Map<List<ThingDto>, List<Thing>>(dtos, items)); | |
var result1 = CreateResultString(Mapper.Map<List<Thing>>(dtos)); | |
Console.WriteLine(result0); | |
Console.WriteLine(result1); | |
Console.ReadLine(); | |
// AutoMapper 4.1.1 and AutoMapper.Collection 1.1.2 output: | |
// test0 test2 | |
// test0 test2 | |
// | |
// AutoMapper 5.1.1 and AutoMapper.Collection 2.1.0 output: | |
// test0 test2 | |
// Things are null | |
} | |
static string CreateResultString(List<Thing> things) | |
{ | |
return things != null ? string.Join(" ", things) : "Things are null"; | |
} | |
} | |
public class AwesomeProfile : Profile | |
{ | |
protected override void Configure() | |
{ | |
CreateMap<ThingDto, Thing>() | |
.EqualityComparision((dto, entity) => dto.ID == entity.ID); | |
} | |
} | |
public class Thing | |
{ | |
public int ID { get; set; } | |
public string Title { get; set; } | |
public override string ToString() { return Title; } | |
} | |
public class ThingDto | |
{ | |
public int ID { get; set; } | |
public string Title { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment