public void ComplexTypeMappingRespectsExistingMappingForOtherTypes() { var mapper = new MemberMapper(); var proposed = mapper.CreateMapProposal(typeof(NestedSourceType), typeof(NestedDestinationType), options: (ctx, option) => { if (ctx.Source.Name == "Name") { option.IgnoreMember(); } }); proposed.FinalizeMap(); var source = new ComplexSourceType { ID = 5, Complex = new NestedSourceType { ID = 10, Name = "test" } }; var destination = mapper.Map <ComplexSourceType, ComplexDestinationType>(source); Assert.AreEqual(destination.ID, 5); Assert.IsNotNull(destination.Complex); Assert.AreNotEqual(destination.Complex.Name, source.Complex.Name); }
//[TestMethod] public void MappingConditionIsRespectedForNestedCollectionMembers() { var mapper = new MemberMapper(); int i = 10; mapper.CreateMapProposal<SourceTypeCollection, DestinationTypeCollection>() .ForMember(dest => dest.Nested).OnlyIf(src => i == 0) .FinalizeMap(); var source = new SourceTypeCollection { Nested = new List<NestedSourceType> { new NestedSourceType { Foo = "Bla" } } }; var result = mapper.Map<SourceTypeCollection, DestinationTypeCollection>(source); Assert.IsNull(result.Nested); //i = 0; result = mapper.Map<SourceTypeCollection, DestinationTypeCollection>(source); Assert.AreEqual("Bla", result.Nested.Single().Foo); }
public void MappingConditionIsRespectedForNestedMembers() { var mapper = new MemberMapper(); mapper.CreateMapProposal <SourceType, DestinationType>() .ForMember(dest => dest.Nested).OnlyIf(src => src.ID == 0) .FinalizeMap(); var source = new SourceType { ID = 10, Name = "X", Nested = new NestedSourceType { Foo = "Bla" } }; var result = mapper.Map <SourceType, DestinationType>(source); Assert.IsNull(result.Nested); source.ID = 0; result = mapper.Map <SourceType, DestinationType>(source); Assert.AreEqual("Bla", result.Nested.Foo); }
public static void MapCreation() { // There's a variety of ways to create a map. First, we need an IMemberMapper, which unless you write your own, is MemberMapper: var mapper = new MemberMapper(); // The first way is implicit. If there's no map defined yet, calling Map will create it and ThisMember will do its best to come up // with a map for the entire object graph. var customer = mapper.Map <CustomerDto, Customer>(new CustomerDto()); // The second way is explicit, which allows you to modify the map mapper.CreateMap <CustomerDto, Customer>(source => new Customer { FirstName = source.FirstName.ToLower() }); mapper.CreateMap <Customer, CustomerDto>(source => new CustomerDto { FullName = source.FirstName + " " + source.LastName }); // The third way is more explicit, allowing you to modify the map in several steps until you 'finalize' it. mapper.CreateMapProposal <CustomerDto, Customer>(source => new Customer { LastName = source.FirstName }) .ForMember(c => c.ID) .OnlyIf(c => c.ID > 0) .ForMember(c => c.FirstName).MapAs(c => c.LastName) .FinalizeMap(); }
//[TestMethod] public void MappingConditionIsRespectedForNestedCollectionMembers() { var mapper = new MemberMapper(); int i = 10; mapper.CreateMapProposal <SourceTypeCollection, DestinationTypeCollection>() .ForMember(dest => dest.Nested).OnlyIf(src => i == 0) .FinalizeMap(); var source = new SourceTypeCollection { Nested = new List <NestedSourceType> { new NestedSourceType { Foo = "Bla" } } }; var result = mapper.Map <SourceTypeCollection, DestinationTypeCollection>(source); Assert.IsNull(result.Nested); //i = 0; result = mapper.Map <SourceTypeCollection, DestinationTypeCollection>(source); Assert.AreEqual("Bla", result.Nested.Single().Foo); }
public void ConversionFunctionWithConditionAndCustomMappingWorks() { var mapper = new MemberMapper(); mapper.CreateMapProposal <Source, Destination>( customMapping: src => new Destination { Foo = src.Foo * 3 }, options: (ctx, options) => { options.Convert <int, int>(s => s * 2); }) .ForMember(s => s.Foo) .OnlyIf(s => s.Foo == 3) .FinalizeMap(); var result = mapper.Map(new Source { Foo = 2 }, new Destination()); Assert.AreEqual(0, result.Foo); result = mapper.Map(new Source { Foo = 3 }, new Destination()); Assert.AreEqual(18, result.Foo); }
public void MappingConditionIsRespectedForNestedMembers() { var mapper = new MemberMapper(); mapper.CreateMapProposal<SourceType, DestinationType>() .ForMember(dest => dest.Nested).OnlyIf(src => src.ID == 0) .FinalizeMap(); var source = new SourceType { ID = 10, Name = "X", Nested = new NestedSourceType { Foo = "Bla" } }; var result = mapper.Map<SourceType, DestinationType>(source); Assert.IsNull(result.Nested); source.ID = 0; result = mapper.Map<SourceType, DestinationType>(source); Assert.AreEqual("Bla", result.Nested.Foo); }
public void HasMapWithoutCreateMapReturnsFalse() { var mapper = new MemberMapper(); var result = mapper.CreateMapProposal <SourceType, DestinationType>(); Assert.IsFalse(mapper.HasMap(typeof(SourceType), typeof(DestinationType))); }
public void DoesNotThrowWhenTypesAreIncompatibleButPropertyGetsIgnoredOnNestedMember() { var mapper = new MemberMapper(); mapper.Options.Strictness.ThrowWithoutCorrespondingSourceMember = true; mapper.CreateMapProposal<IncompatibleSourceWithNested, IncompatibleDestinationWithNested>() .ForMember(s => s.Foo.Test).Ignore().FinalizeMap(); }
public void DoesNotThrowWhenTypesAreIncompatibleButPropertyGetsIgnoredOnNestedMember() { var mapper = new MemberMapper(); mapper.Options.Strictness.ThrowWithoutCorrespondingSourceMember = true; mapper.CreateMapProposal <IncompatibleSourceWithNested, IncompatibleDestinationWithNested>() .ForMember(s => s.Foo.Test).Ignore().FinalizeMap(); }
public void GetMapWithoutSupplyingParameterTypeThrowsMapNotFoundException() { var mapper = new MemberMapper(); mapper.CreateMapProposal<SourceType, DestinationType, int>((src, i) => new DestinationType { ID = i }).FinalizeMap(); mapper.GetMap<SourceType, DestinationType>(); }
public void GetMapWithoutSupplyingParameterTypeThrowsMapNotFoundException() { var mapper = new MemberMapper(); mapper.CreateMapProposal <SourceType, DestinationType, int>((src, i) => new DestinationType { ID = i }).FinalizeMap(); mapper.GetMap <SourceType, DestinationType>(); }
public void FieldsAreMapped() { var mapper = new MemberMapper(); var proposition = mapper.CreateMapProposal <SourceFields, DestinationFields>(); var expectation = new ExpectedMappings <SourceFields, DestinationFields>(); expectation.Add(t => t.ID, t => t.ID); Assert.IsTrue(ContainsMappingFor(proposition, expectation)); }
public void ComplexListGetsMappedFromIEnumerable() { var mapper = new MemberMapper(); var proposition = mapper.CreateMapProposal(typeof(IEnumerableComplexSourceType), typeof(ListComplexDestinationType)); var expectation = new ExpectedMappings <IEnumerableComplexSourceType, ListComplexDestinationType>(); expectation.Add(t => t.Source, t => t.Source); Assert.IsTrue(ContainsMappingFor(proposition, expectation)); }
public void ListIntPropertyGetsMappedToArray() { var mapper = new MemberMapper(); var proposition = mapper.CreateMapProposal(typeof(ListSourceType), typeof(ArrayDestinationType)); var expectation = new ExpectedMappings <ListSourceType, ArrayDestinationType>(); expectation.Add(t => t.Source, t => t.Source); Assert.IsTrue(ContainsMappingFor(proposition, expectation)); }
public void PassingInInstanceOfClassToMapWorks() { var mapper = new MemberMapper(); mapper.CreateMapProposal <ConstructorParameterClass, ConstructorClass>().WithConstructorFor((c, c1) => new ConstructorClass(c)); var result = mapper.Map(new ConstructorParameterClass { Foo = 23 }, new ConstructorClass(new ConstructorParameterClass())); Assert.AreEqual(23, result.Foo); }
public void TryGetMapWithoutSupplyingParameterTypeReturnsFalse() { var mapper = new MemberMapper(); mapper.CreateMapProposal<SourceType, DestinationType, int>((src, i) => new DestinationType { ID = i }).FinalizeMap(); MemberMap<SourceType, DestinationType> map; Assert.IsFalse(mapper.TryGetMap<SourceType, DestinationType>(out map)); }
public void TryGetMapWithoutSupplyingParameterTypeReturnsFalse() { var mapper = new MemberMapper(); mapper.CreateMapProposal <SourceType, DestinationType, int>((src, i) => new DestinationType { ID = i }).FinalizeMap(); MemberMap <SourceType, DestinationType> map; Assert.IsFalse(mapper.TryGetMap <SourceType, DestinationType>(out map)); }
//[TestMethod] public void Test() { var mapper = new MemberMapper(); mapper.Options.Safety.PerformNullChecksOnCustomMappings = false; int i = 2; var map = mapper.CreateMapProposal<SourceType, DestinationType>(customMapping: (src) => new { //ID = src.IDs.Count + 100 + i, ID = (from x in Enumerable.Range(0, 100) select x).Sum() + i, Name = src.Name.Length.ToString() + " " + src.Name }).FinalizeMap(); i++; //var map = mapper.CreateMap(typeof(SourceType), typeof(DestinationType)).FinalizeMap(); var source = new SourceType { ID = 1, IDs = new List<SourceElement> { new SourceElement { X = 10, Collection = new List<Foo> { new Foo { Z = "string" }, new Foo { Z = "string1" }, new Foo { Z = "string2" } } } }, Name = "X" }; var result = mapper.Map<SourceType, DestinationType>(source); }
//[TestMethod] public void Test() { var mapper = new MemberMapper(); mapper.Options.Safety.PerformNullChecksOnCustomMappings = false; int i = 2; var map = mapper.CreateMapProposal <SourceType, DestinationType>(customMapping: (src) => new { //ID = src.IDs.Count + 100 + i, ID = (from x in Enumerable.Range(0, 100) select x).Sum() + i, Name = src.Name.Length.ToString() + " " + src.Name }).FinalizeMap(); i++; //var map = mapper.CreateMap(typeof(SourceType), typeof(DestinationType)).FinalizeMap(); var source = new SourceType { ID = 1, IDs = new List <SourceElement> { new SourceElement { X = 10, Collection = new List <Foo> { new Foo { Z = "string" }, new Foo { Z = "string1" }, new Foo { Z = "string2" } } } }, Name = "X" }; var result = mapper.Map <SourceType, DestinationType>(source); }
public void NavigationPropertiesDoNotThrowWhenNull() { var mapper = new MemberMapper(); mapper.CreateMapProposal <Customer, SimpleCustomerDto>(customMapping: src => new { AddressLine = src.Address.Foo.Bar }).FinalizeMap(); var customer = new Customer { }; var dto = mapper.Map <Customer, SimpleCustomerDto>(customer); }
public void NavigationPropertiesThrowsWhenNullWithSafetyDisabled() { var mapper = new MemberMapper(); mapper.Options.Safety.PerformNullChecksOnCustomMappings = false; mapper.CreateMapProposal <Customer, SimpleCustomerDto>(customMapping: src => new { AddressLine = src.Address.Foo.Bar }).FinalizeMap(); var customer = new Customer { }; var dto = mapper.Map <Customer, SimpleCustomerDto>(customer); }
public void ExpectedMembersAreMapped() { var mapper = new MemberMapper(); mapper.CreateMapProposal(typeof(SourceType), typeof(DestinationType)).FinalizeMap(); var source = new SourceType { ID = 5, Name = "Source" }; var destination = new DestinationType(); destination = mapper.Map(source, destination); Assert.AreEqual(destination.ID, 5); Assert.AreEqual(destination.Name, "Source"); }
public void CustomConstructorIsRespected() { var mapper = new MemberMapper(); mapper.CreateMapProposal<SourceType, DestinationType>() .WithConstructorFor<NestedDestinationType>((src, dest) => new NestedDestinationType(1)) .FinalizeMap(); var source = new SourceType { Foo = new NestedSourceType { ID = 10 } }; var result = mapper.Map<SourceType, DestinationType>(source); Assert.AreEqual(1, result.Foo.OtherID); Assert.AreEqual(10, result.Foo.ID); }
public void CustomConstructorIsRespected() { var mapper = new MemberMapper(); mapper.CreateMapProposal <SourceType, DestinationType>() .WithConstructorFor <NestedDestinationType>((src, dest) => new NestedDestinationType(1)) .FinalizeMap(); var source = new SourceType { Foo = new NestedSourceType { ID = 10 } }; var result = mapper.Map <SourceType, DestinationType>(source); Assert.AreEqual(1, result.Foo.OtherID); Assert.AreEqual(10, result.Foo.ID); }
public void CustomMappingAddedLaterIsRespected() { var mapper = new MemberMapper(); mapper.CreateMapProposal <Customer, SimpleCustomerDto>() .ForMember(c => c.AddressLine).MapAs(c => c.Address.Street + " " + c.Address.HouseNumber) .FinalizeMap(); var source = new Customer { Address = new Address { Street = "Street", HouseNumber = "12" } }; var result = mapper.Map <Customer, SimpleCustomerDto>(source); Assert.AreEqual("Street 12", result.AddressLine); }
public void CustomMappingAddedLaterIsRespected() { var mapper = new MemberMapper(); mapper.CreateMapProposal<Customer, SimpleCustomerDto>() .ForMember(c => c.AddressLine).MapAs(c => c.Address.Street + " " + c.Address.HouseNumber) .FinalizeMap(); var source = new Customer { Address = new Address { Street = "Street", HouseNumber = "12" } }; var result = mapper.Map<Customer, SimpleCustomerDto>(source); Assert.AreEqual("Street 12", result.AddressLine); }
public void NonMatchingMembesAreMappedWhenCustomMappingIsPresent() { var mapper = new MemberMapper(); mapper.CreateMapProposal <Customer, SimpleCustomerDto>(customMapping: src => new { AddressLine = src.Address.City }).FinalizeMap(); var customer = new Customer { Address = new Address { City = "test" } }; var dto = mapper.Map <Customer, SimpleCustomerDto>(customer); Assert.AreEqual("test", dto.AddressLine); }
public void NestedPropertyMembersCanBeIgnored() { var mapper = new MemberMapper(); mapper.CreateMapProposal <SourceType, DestinationType>() .ForMember(src => src.Nested.Foo).Ignore() .FinalizeMap(); var source = new SourceType { ID = 10, Name = "X", Nested = new NestedSourceType { Foo = "Bla" } }; var result = mapper.Map <SourceType, DestinationType>(source); Assert.IsNull(result.Nested.Foo); }
public void NestedCollectionPropertiesCanBeIgnored() { var mapper = new MemberMapper(); mapper.CreateMapProposal <SourceTypeCollection, DestinationTypeCollection>() .ForMember(dest => dest.Nested).Ignore() .FinalizeMap(); var source = new SourceTypeCollection { Nested = new List <NestedSourceType> { new NestedSourceType { Foo = "Bla" } } }; var result = mapper.Map <SourceTypeCollection, DestinationTypeCollection>(source); Assert.IsNull(result.Nested); }
public static void IgnoringMembers() { // Ignoring members can be done in a variety of ways. In the class above, we've placed the [IgnoreMember] attribute on Password, // indicating it should never be mapped. var mapper = new MemberMapper(); // You can set this option to ignore the IgnoreMember attribute: mapper.Options.Conventions.IgnoreMemberAttributeShouldBeRespected = false; // But we won't mapper.Options.Conventions.IgnoreMemberAttributeShouldBeRespected = true; var existingUser = new User { Username = "******", Password = "******" }; mapper.Map <UserDto, User>(new UserDto { Username = "******", Password = "******" }, existingUser); // The password of existingUser will be unchanged mapper.Options.Conventions.IgnoreMemberAttributeShouldBeRespected = false; mapper.CreateMapProposal <UserDto, User>() .ForMember(u => u.Password).Ignore() .FinalizeMap(); // Or you could use it like this: var map = mapper.CreateMapProposal <UserDto, User>(); var userCanSetPassword = false; if (!userCanSetPassword) { map.ForMember(u => u.Password).Ignore(); } map.FinalizeMap(); // The third way: mapper.CreateMap <UserDto, User>(options: (ctx, option) => { // You can make this as complicated as you want if (ctx.Destination.Name == "Password") { option.IgnoreMember(); } // For example, check for the presence of an attribute // that determines if a user has rights to map this property var attr = ctx.Destination .PropertyOrFieldType .GetCustomAttributes(typeof(MappingRequiresPermissionAttribute), false) .FirstOrDefault() as MappingRequiresPermissionAttribute; if (attr != null && !attr.HasPermission) { option.IgnoreMember(); } }); }
static void Main(string[] args) { var res = MapRecursive(new RecursiveSourceClass { ID = 1, Child = new RecursiveSourceClass { ID = 2, Child = new RecursiveSourceClass { ID = 3, Foo = new SourceType { ID = 2, Name = "Foo1", Bar = new SourceType { Foo = new RecursiveSourceClass { ID = 4, Child = new RecursiveSourceClass { ID = 5 } } } } }, Foo = new SourceType { ID = 1, Name = "Foo" } } }, new RecursiveDestinationClass()); //Expressions.CreateMethod(null); //int ix= 1; //Expression<Func<int>> xp = () => ix * ix; //var func = Expressions.CreateMethod((src, dest) => new //{ // String = src.Value.ToString(), // Val = src.String.Length //}) as Func<Source, Destination, Destination>; //var src1 = new Source //{ // Value = 1 //}; //var dest1 = func(src1, new Destination()); var mapper = new MemberMapper(); int i = 2; var map = mapper.CreateMapProposal <SourceType, DestinationType>(customMapping: (src) => new { //ID = src.IDs.Count + 100 + i, ID = (from x in Enumerable.Range(0, 100) select x).Sum() + i, Name = src.Name.Length.ToString() + " " + src.Name }).FinalizeMap(); i++; //var map = mapper.CreateMap(typeof(SourceType), typeof(DestinationType)).FinalizeMap(); var source = new SourceType { ID = 1, IDs = new List <SourceElement> { new SourceElement { X = 10, Collection = new List <Foo> { new Foo { Z = "string" }, new Foo { Z = "string1" }, new Foo { Z = "string2" } } } }, Name = "X" }; var result = mapper.Map <SourceType, DestinationType>(source); //map.FinalizeMap(); //new ProposedMap<SourceType, DestinationType>().AddExpression(source => source.ID, destination => destination.ID); }
public void PassingInInstanceOfClassToMapWorks() { var mapper = new MemberMapper(); mapper.CreateMapProposal<ConstructorParameterClass, ConstructorClass>().WithConstructorFor((c, c1) => new ConstructorClass(c)); var result = mapper.Map(new ConstructorParameterClass { Foo = 23 }, new ConstructorClass(new ConstructorParameterClass())); Assert.AreEqual(23, result.Foo); }
public static void MapCreation() { // There's a variety of ways to create a map. First, we need an IMemberMapper, which unless you write your own, is MemberMapper: var mapper = new MemberMapper(); // The first way is implicit. If there's no map defined yet, calling Map will create it and ThisMember will do its best to come up // with a map for the entire object graph. var customer = mapper.Map<CustomerDto, Customer>(new CustomerDto()); // The second way is explicit, which allows you to modify the map mapper.CreateMap<CustomerDto, Customer>(source => new Customer { FirstName = source.FirstName.ToLower() }); mapper.CreateMap<Customer, CustomerDto>(source => new CustomerDto { FullName = source.FirstName + " " + source.LastName }); // The third way is more explicit, allowing you to modify the map in several steps until you 'finalize' it. mapper.CreateMapProposal<CustomerDto, Customer>(source => new Customer { LastName = source.FirstName }) .ForMember(c => c.ID) .OnlyIf(c => c.ID > 0) .ForMember(c => c.FirstName).MapAs(c => c.LastName) .FinalizeMap(); }
public static void IgnoringMembers() { // Ignoring members can be done in a variety of ways. In the class above, we've placed the [IgnoreMember] attribute on Password, // indicating it should never be mapped. var mapper = new MemberMapper(); // You can set this option to ignore the IgnoreMember attribute: mapper.Options.Conventions.IgnoreMemberAttributeShouldBeRespected = false; // But we won't mapper.Options.Conventions.IgnoreMemberAttributeShouldBeRespected = true; var existingUser = new User { Username = "******", Password = "******" }; mapper.Map<UserDto, User>(new UserDto { Username = "******", Password = "******" }, existingUser); // The password of existingUser will be unchanged mapper.Options.Conventions.IgnoreMemberAttributeShouldBeRespected = false; mapper.CreateMapProposal<UserDto, User>() .ForMember(u => u.Password).Ignore() .FinalizeMap(); // Or you could use it like this: var map = mapper.CreateMapProposal<UserDto, User>(); var userCanSetPassword = false; if (!userCanSetPassword) { map.ForMember(u => u.Password).Ignore(); } map.FinalizeMap(); // The third way: mapper.CreateMap<UserDto, User>(options: (ctx, option) => { // You can make this as complicated as you want if (ctx.Destination.Name == "Password") { option.IgnoreMember(); } // For example, check for the presence of an attribute // that determines if a user has rights to map this property var attr = ctx.Destination .PropertyOrFieldType .GetCustomAttributes(typeof(MappingRequiresPermissionAttribute), false) .FirstOrDefault() as MappingRequiresPermissionAttribute; if (attr != null && !attr.HasPermission) { option.IgnoreMember(); } }); }
static void Benchmark() { var mapper = new MemberMapper(); var map = mapper.CreateMapProposal <ComplexSourceType, ComplexDestinationType>(customMapping: src => new ComplexDestinationType { ID = Enumerable.Range(0, 100000).Count() }).FinalizeMap(); var source = new ComplexSourceType { ID = 5, Complex = new NestedSourceType { ID = 10, Name = "test" } }; source = null; f = (src, dest) => { dest.ID = src.ID; if (src.Complex != null) { var complexSource = src.Complex; var complexDestination = new NestedDestinationType(); complexDestination.ID = Enumerable.Range(0, 100000).Count(); complexDestination.Name = complexSource.Name; dest.Complex = complexDestination; } return(dest); }; var sw = Stopwatch.StartNew(); const int iterations = 100; for (int i = 0; i < iterations; i++) { Foo = new ComplexDestinationType(); Foo.ID = source.ID; if (source.Complex != null) { var complexSource = source.Complex; var complexDestination = new NestedDestinationType(); complexDestination.ID = Enumerable.Range(0, 100000).Count(); complexDestination.Name = complexSource.Name; Foo.Complex = complexDestination; } } sw.Stop(); Console.WriteLine("Manual " + sw.Elapsed); sw.Restart(); for (int i = 0; i < iterations; i++) { Foo = f(source, new ComplexDestinationType()); } sw.Stop(); Console.WriteLine("Func " + sw.Elapsed); sw.Restart(); for (int i = 0; i < iterations; i++) { Foo = mapper.Map <ComplexSourceType, ComplexDestinationType>(source); } sw.Stop(); Console.WriteLine("Map " + sw.Elapsed); var func = (Func <ComplexSourceType, ComplexDestinationType, ComplexDestinationType>)map.MappingFunction; var destination = new ComplexDestinationType(); sw.Restart(); for (int i = 0; i < iterations; i++) { Foo = func(source, new ComplexDestinationType()); } sw.Stop(); Console.WriteLine("Map 1 " + sw.Elapsed); }
public void NavigationPropertiesDoNotThrowWhenNull_2() { var mapper = new MemberMapper(); mapper.CreateMapProposal<Customer, SimpleCustomerDto>(customMapping: src => new { AddressLine = src.Address.Foo.Bar }).FinalizeMap(); var customer = new Customer { Address = new Address() }; var dto = mapper.Map<Customer, SimpleCustomerDto>(customer); }
public void ComplexTypeMappingRespectsExistingMapping() { var mapper = new MemberMapper(); var proposed = mapper.CreateMapProposal(typeof(ComplexSourceType), typeof(ComplexDestinationType), options: (ctx, option) => { if (ctx.Source.Name == "Name") { option.IgnoreMember(); } }); proposed.FinalizeMap(); var source = new ComplexSourceType { ID = 5, Complex = new NestedSourceType { ID = 10, Name = "test" } }; var destination = mapper.Map<ComplexSourceType, ComplexDestinationType>(source); Assert.AreEqual(destination.ID, 5); Assert.IsNotNull(destination.Complex); Assert.AreNotEqual(destination.Complex.Name, source.Complex.Name); }
public void HasMapWithoutCreateMapReturnsFalse() { var mapper = new MemberMapper(); var result = mapper.CreateMapProposal<SourceType, DestinationType>(); Assert.IsFalse(mapper.HasMap(typeof(SourceType), typeof(DestinationType))); }
public void ExpectedMembersAreMapped() { var mapper = new MemberMapper(); mapper.CreateMapProposal(typeof(SourceType), typeof(DestinationType)).FinalizeMap(); var source = new SourceType { ID = 5, Name = "Source" }; var destination = new DestinationType(); destination = mapper.Map(source, destination); Assert.AreEqual(destination.ID, 5); Assert.AreEqual(destination.Name, "Source"); }
static void Main(string[] args) { var res = MapRecursive(new RecursiveSourceClass { ID = 1, Child = new RecursiveSourceClass { ID = 2, Child = new RecursiveSourceClass { ID = 3, Foo = new SourceType { ID = 2, Name = "Foo1", Bar = new SourceType { Foo = new RecursiveSourceClass { ID = 4, Child = new RecursiveSourceClass { ID = 5 } } } } }, Foo = new SourceType { ID = 1, Name = "Foo" } } }, new RecursiveDestinationClass()); //Expressions.CreateMethod(null); //int ix= 1; //Expression<Func<int>> xp = () => ix * ix; //var func = Expressions.CreateMethod((src, dest) => new //{ // String = src.Value.ToString(), // Val = src.String.Length //}) as Func<Source, Destination, Destination>; //var src1 = new Source //{ // Value = 1 //}; //var dest1 = func(src1, new Destination()); var mapper = new MemberMapper(); int i = 2; var map = mapper.CreateMapProposal<SourceType, DestinationType>(customMapping: (src) => new { //ID = src.IDs.Count + 100 + i, ID = (from x in Enumerable.Range(0, 100) select x).Sum() + i, Name = src.Name.Length.ToString() + " " + src.Name }).FinalizeMap(); i++; //var map = mapper.CreateMap(typeof(SourceType), typeof(DestinationType)).FinalizeMap(); var source = new SourceType { ID = 1, IDs = new List<SourceElement> { new SourceElement { X = 10, Collection = new List<Foo> { new Foo { Z = "string" }, new Foo { Z = "string1" }, new Foo { Z = "string2" } } } }, Name = "X" }; var result = mapper.Map<SourceType, DestinationType>(source); //map.FinalizeMap(); //new ProposedMap<SourceType, DestinationType>().AddExpression(source => source.ID, destination => destination.ID); }
public void NestedCollectionPropertiesCanBeIgnored() { var mapper = new MemberMapper(); mapper.CreateMapProposal<SourceTypeCollection, DestinationTypeCollection>() .ForMember(dest => dest.Nested).Ignore() .FinalizeMap(); var source = new SourceTypeCollection { Nested = new List<NestedSourceType> { new NestedSourceType { Foo = "Bla" } } }; var result = mapper.Map<SourceTypeCollection, DestinationTypeCollection>(source); Assert.IsNull(result.Nested); }
public void NavigationPropertiesThrowsWhenNullWithSafetyDisabled() { var mapper = new MemberMapper(); mapper.Options.Safety.PerformNullChecksOnCustomMappings = false; mapper.CreateMapProposal<Customer, SimpleCustomerDto>(customMapping: src => new { AddressLine = src.Address.Foo.Bar }).FinalizeMap(); var customer = new Customer { }; var dto = mapper.Map<Customer, SimpleCustomerDto>(customer); }
public void NonMatchingMembesAreMappedWhenCustomMappingIsPresent() { var mapper = new MemberMapper(); mapper.CreateMapProposal<Customer, SimpleCustomerDto>(customMapping: src => new { AddressLine = src.Address.City }).FinalizeMap(); var customer = new Customer { Address = new Address { City = "test" } }; var dto = mapper.Map<Customer, SimpleCustomerDto>(customer); Assert.AreEqual("test", dto.AddressLine); }
public void ConversionFunctionWithConditionWorks() { var mapper = new MemberMapper(); mapper.CreateMapProposal<Source, Destination>(options: (ctx, options) => { options.Convert<int, int>(s => s * 2); }) .ForMember(s => s.Foo) .OnlyIf(s => s.Foo == 3) .FinalizeMap(); var result = mapper.Map(new Source { Foo = 2 }, new Destination()); Assert.AreEqual(0, result.Foo); result = mapper.Map(new Source { Foo = 3 }, new Destination()); Assert.AreEqual(6, result.Foo); }
public void PropertiesCanBeIgnored() { var mapper = new MemberMapper(); mapper.CreateMapProposal<SourceType, DestinationType>() .ForMember(src => src.Name).Ignore() .FinalizeMap(); var source = new SourceType { ID = 10, Name = "X", Nested = new NestedSourceType { Foo = "Bla" } }; var result = mapper.Map<SourceType, DestinationType>(source); Assert.IsNull(result.Name); Assert.AreEqual("Bla", result.Nested.Foo); }