/
Profiles.cs
71 lines (60 loc) · 1.4 KB
/
Profiles.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using AutoMapper;
using NUnit.Framework;
using Should;
namespace AutoMapperSamples.Configuration
{
namespace Profiles
{
[TestFixture]
public class SimpleExample
{
public class Order
{
public decimal Amount { get; set; }
}
public class OrderListViewModel
{
public string Amount { get; set; }
}
public class OrderEditViewModel
{
public string Amount { get; set; }
}
public class MoneyFormatter : ValueFormatter<decimal>
{
protected override string FormatValueCore(decimal value)
{
return value.ToString("c");
}
}
public class ViewModelProfile : Profile
{
protected override void Configure()
{
CreateMap<Order, OrderListViewModel>();
ForSourceType<decimal>().AddFormatter<MoneyFormatter>();
}
}
[SetUp]
public void SetUp()
{
Mapper.Reset();
}
[Test]
public void Example()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<ViewModelProfile>();
cfg.CreateMap<Order, OrderEditViewModel>();
});
var order = new Order {Amount = 50m};
var listViewModel = Mapper.Map<Order, OrderListViewModel>(order);
var editViewModel = Mapper.Map<Order, OrderEditViewModel>(order);
listViewModel.Amount.ShouldEqual(order.Amount.ToString("c"));
editViewModel.Amount.ShouldEqual("50");
}
}
}
}