本文整理汇总了C#中MemberMapper.CreateMapProposal方法的典型用法代码示例。如果您正苦于以下问题:C# MemberMapper.CreateMapProposal方法的具体用法?C# MemberMapper.CreateMapProposal怎么用?C# MemberMapper.CreateMapProposal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemberMapper
的用法示例。
在下文中一共展示了MemberMapper.CreateMapProposal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MappingConditionIsRespectedForNestedCollectionMembers
//[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);
}
示例2: MappingConditionIsRespectedForNestedMembers
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);
}
示例3: DoesNotThrowWhenTypesAreIncompatibleButPropertyGetsIgnoredOnNestedMember
public void DoesNotThrowWhenTypesAreIncompatibleButPropertyGetsIgnoredOnNestedMember()
{
var mapper = new MemberMapper();
mapper.Options.Strictness.ThrowWithoutCorrespondingSourceMember = true;
mapper.CreateMapProposal<IncompatibleSourceWithNested, IncompatibleDestinationWithNested>()
.ForMember(s => s.Foo.Test).Ignore().FinalizeMap();
}
示例4: GetMapWithoutSupplyingParameterTypeThrowsMapNotFoundException
public void GetMapWithoutSupplyingParameterTypeThrowsMapNotFoundException()
{
var mapper = new MemberMapper();
mapper.CreateMapProposal<SourceType, DestinationType, int>((src, i) => new DestinationType
{
ID = i
}).FinalizeMap();
mapper.GetMap<SourceType, DestinationType>();
}
示例5: TryGetMapWithoutSupplyingParameterTypeReturnsFalse
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));
}
示例6: Test
//[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);
}
示例7: CustomConstructorIsRespected
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);
}
示例8: CustomMappingAddedLaterIsRespected
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);
}
示例9: NavigationPropertiesThrowsWhenNullWithSafetyDisabled
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);
}
示例10: ConversionFunctionWithConditionWorks
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);
}
示例11: MapCreation
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();
}
示例12: NavigationPropertiesDoNotThrowWhenNull_2
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);
}
示例13: IgnoringMembers
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 = "User",
Password = "1234"
};
mapper.Map<UserDto, User>(new UserDto { Username = "Name", Password = "Secret" }, 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();
}
});
}
示例14: PropertiesCanBeIgnored
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);
}
示例15: NonMatchingMembesAreMappedWhenCustomMappingIsPresent
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);
}