本文整理汇总了C#中Mapper.Map方法的典型用法代码示例。如果您正苦于以下问题:C# Mapper.Map方法的具体用法?C# Mapper.Map怎么用?C# Mapper.Map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mapper
的用法示例。
在下文中一共展示了Mapper.Map方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldMapColumnsToExpectedTypeBasedOnSpecifiedFieldOrder
public void ShouldMapColumnsToExpectedTypeBasedOnSpecifiedFieldOrder()
{
var mapper = new Mapper<SpecifiedOrderTargetType>(new ErrorCollector());
var target = mapper.Map(new[] {"4", "hello"});
Assert.Equal("hello", target.AString);
Assert.Equal(4, target.AnInt);
}
示例2: Map_WhenInvoked_ShouldWork
public void Map_WhenInvoked_ShouldWork()
{
// Arrange
var mapper = new Mapper<Schedule, ScheduleModel>()
// Straigt mapping
.Map(e => e.Title, e => e.Title)
// Mapping properties with different names
.Map((e, i) => e.Tasks[i].Title, (e, i) => e.Tasks[i].DisplayName)
// Mapping string <-> decimal, string-to-primitive-type converter is picked up automatically
.Map((e, i) => e.Tasks[i].Budget, (e, i) => e.Tasks[i].Budget)
// Mapping array of convertible types supported too
.Map((e, i, j) => e.Tasks[i].Points[j], (e, i, j) => e.Tasks[i].Points[j])
// Two-level nesting is supported too
.Map((e, i, j) => e.Tasks[i].Responsible[j].Id, (e, i, j) => e.Tasks[i].ResponsibleUsers[j].Id)
.Map((e, i, j) => e.Tasks[i].Responsible[j].DisplayName, (e, i, j) => e.Tasks[i].ResponsibleUsers[j].Display);
var schedule = fixture.Create<Schedule>();
// Act
// From entity to model
var scheduleModel = mapper.Map(schedule);
// And from model to entity
var newSchedule = mapper.Map(scheduleModel);
// Assert
newSchedule.Title.Should().Be(schedule.Title);
newSchedule.Tasks.Should().NotBeNull();
newSchedule.Tasks.Length.Should().Be(schedule.Tasks.Length);
for (var i = 0; i < newSchedule.Tasks.Length; i++)
{
newSchedule.Tasks[i].Budget.Should().Be(schedule.Tasks[i].Budget);
newSchedule.Tasks[i].Title.Should().Be(schedule.Tasks[i].Title);
newSchedule.Tasks[i].Points.Length.Should().Be(schedule.Tasks[i].Points.Length);
for (var j = 0; j < newSchedule.Tasks[i].Points.Length; j++)
newSchedule.Tasks[i].Points[j].Should().BeCloseTo(schedule.Tasks[i].Points[j], 1000);
newSchedule.Tasks[i].Responsible.Count.Should().Be(schedule.Tasks[i].Responsible.Count);
for (var j = 0; j < newSchedule.Tasks[i].Responsible.Count; j++)
{
newSchedule.Tasks[i].Responsible[j].DisplayName.Should().Be(newSchedule.Tasks[i].Responsible[j].DisplayName);
newSchedule.Tasks[i].Responsible[j].Id.Should().Be(newSchedule.Tasks[i].Responsible[j].Id);
}
}
}
示例3: Should_Create_Assign
public void Should_Create_Assign()
{
Helpers.ActionLambda<Entity, Model>(_map)(_entity, _model);
_model.subEntities.First().name.ShouldEqual("Dillon");
var mapper = new Mapper<Entity, Model>(null);
Expression<Func<IList<Entity>, IList<Model>>> fark = e => e.Select(q => mapper.Map(q, new Model())).ToList();
}
示例4: ReturnsNullIfSourceValueIsNull
public void ReturnsNullIfSourceValueIsNull()
{
var config = new MapperConfiguration(_ => { });
IMapper mapper = new Mapper(config);
var result = mapper.Map((NameValueCollection)null, new NameValueCollection());
result.ShouldBeNull();
}
示例5: ReturnsEmptyCollectionWhenSourceCollectionIsEmpty
public void ReturnsEmptyCollectionWhenSourceCollectionIsEmpty()
{
var config = new MapperConfiguration(_ => { });
IMapper mapper = new Mapper(config);
var result = mapper.Map(new NameValueCollection(), (NameValueCollection)null);
result.ShouldBeEmpty();
}
示例6: ShouldRecordErrorIfNotEnoughColumnsToExtractFrom
public void ShouldRecordErrorIfNotEnoughColumnsToExtractFrom()
{
var collector = new ErrorCollector();
Assert.False(collector.ContainsErrors);
var mapper = new Mapper<DefaultOrderTargetType>(collector);
mapper.Map(new[] {"hello", "4"});
Assert.True(collector.ContainsErrors);
Console.WriteLine(collector.ToString()); // if you're interested
}
示例7: ShouldRecordErrorIfUnableToParseTheInput
public void ShouldRecordErrorIfUnableToParseTheInput()
{
var collector = new ErrorCollector();
Assert.False(collector.ContainsErrors);
var mapper = new Mapper<SpecifiedOrderTargetType>(collector);
mapper.Map(new[] {"4,787a", "hello" });
Assert.True(collector.ContainsErrors);
Console.WriteLine(collector.ToString()); // if you're interested
}
示例8: ShouldMapColumnsToExpectedTypeBasedOnDefaultFieldOrder
public void ShouldMapColumnsToExpectedTypeBasedOnDefaultFieldOrder()
{
var mapper = new Mapper<DefaultOrderTargetType>(new ErrorCollector());
var target = mapper.Map(new[] {"hello", "4", "23-Jun-2012"});
Assert.Equal("hello", target.AString);
Assert.Equal(4, target.AnInt);
// ok; mapping datetimes like this is a bit dodgy because ParseExact isn't being used under the hood
// but for the purposes of the narrow scope of this example project i hope its ok.
Assert.Equal(new DateTime(2012, 6, 23), target.ADateTime);
}
示例9: ReturnsMappedObjectWithExpectedValuesWhenSourceCollectionHasOneItem
public void ReturnsMappedObjectWithExpectedValuesWhenSourceCollectionHasOneItem()
{
var config = new MapperConfiguration(_ => { });
IMapper mapper = new Mapper(config);
var sourceValue = new NameValueCollection() { { "foo", "bar" } };
var result = mapper.Map(sourceValue, new NameValueCollection());
1.ShouldEqual(result.Count);
"foo".ShouldEqual(result.AllKeys[0]);
"bar".ShouldEqual(result["foo"]);
}
示例10: InputCastAsObjectMapsToSameCode
public void InputCastAsObjectMapsToSameCode()
{
// Arrange
var firstValue = "firstValue";
var firstCode = 0;
var lookup = new Dictionary<string, int>
{
{firstValue, firstCode}
};
var mapper = new Mapper<string, int>(lookup);
// Act
var firstResult = mapper.Map(firstValue);
var secondResult = mapper.Map((object)firstValue);
// Assert
Assert.AreEqual(firstCode, firstResult);
Assert.IsInstanceOfType(secondResult, typeof(int));
Assert.AreEqual(firstCode, (int)secondResult);
}
示例11: When_Mapping_Then_Mapped
public void When_Mapping_Then_Mapped()
{
// Arrange
sut = new Mapper();
var from = new List<string> {"one", "two", "three"};
// Act
var res = sut.Map<List<string>>(from);
// Assert
Assert.NotNull(res);
CollectionAssert.AreEqual(from, res);
}
示例12: Map_WhenInvokedForNullArray_ShouldNotCreateDest
public void Map_WhenInvokedForNullArray_ShouldNotCreateDest()
{
// Arrange
var mapper = new Mapper<SArr, DArr>()
.Map(s => s.Counter, s => s.Number)
.Map((s, i) => s.Src[i].Name, (s, i) => s.Dst[i].Name);
var src = fixture.Create<DArr>();
src.Dst = null;
// Act
var dst = mapper.Map(src);
// Assert
Assert.Equal(src.Number, dst.Counter);
Assert.Null(dst.Src);
}
示例13: Maps
public void Maps()
{
var mapper = new Mapper<Person, PersonDto>();
mapper.AddMapping(x => x.Name, y => y.FullName);
mapper.AddMapping(x => x.Age, y => y.AgeInYears);
var person = new Person
{
Name = "Chuck Norris",
Age = 433,
Title = "Texas Ranger"
};
PersonDto personDto = mapper.Map(person);
personDto.FullName.Should().Be("Chuck Norris");
personDto.AgeInYears.Should().Be(433);
}
示例14: MappingDoesNotWorkBetweenTypes
public void MappingDoesNotWorkBetweenTypes()
{
//Arrange
var sut = new Mapper();
//Act
var result = sut.Map<Event>(new
{
Participants = new List<Participant>()
{
new Participant() {
Name = "test"
}
}
});
//Assert
Assert.Null(result.Participants);
}
示例15: MappingFromSameTypeIncludesNestedTypes
public void MappingFromSameTypeIncludesNestedTypes()
{
//Arrange
var sut = new Mapper();
//Act
var result = sut.Map<Event>(new Event
{
Participants = new List<Participant>()
{
new Participant() {
Name = "test"
}
}
});
//Assert
Assert.Equal(1, result.Participants.Count());
}