-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoMapper Example.linq
71 lines (59 loc) · 1.63 KB
/
AutoMapper Example.linq
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
<Query Kind="Program">
<Connection>
<ID>45a17493-a2d7-4707-bdd5-3f99883f6519</ID>
<Server>SPARKY</Server>
<Database>AdventureWorks</Database>
<ShowServer>true</ShowServer>
</Connection>
<NuGetReference Version="4.0.4">AutoMapper</NuGetReference>
</Query>
void Main()
{
/*
Uses Adventure Works DB
In-Linqpad connection
Just a simple illustration of setting up an automap
*/
AutoMapper.Mapper.Reset();
DoMapps();
LINQPad.User.Person y = Persons.Where(w => w.BusinessEntityID ==291).Take(1).First();
PersonDTO z;
z = AutoMapper.Mapper.Map<LINQPad.User.Person, PersonDTO>(y);
z.Dump();
}
void DebugMap(object s, object d)
{
(new {src=s.GetType().Name, dest=d.GetType().Name }).Dump();
}
private void DoMapps()
{
AutoMapper.Mapper.CreateMap<LINQPad.User.Person, PersonDTO>()
.BeforeMap((s,d) =>DebugMap(s,d) );
AutoMapper.Mapper.CreateMap<LINQPad.User.BusinessEntity, BusinessEntityDTO>()
.BeforeMap((s,d) =>DebugMap(s,d) );
AutoMapper.Mapper.CreateMap<LINQPad.User.BusinessEntityContact, BusinessEntityContactDTO>()
.BeforeMap((s,d) =>DebugMap(s,d) );
}
public class PersonDTO
{
public Guid RowGuid;
public string LastName;
public string FirstName;
public string Title;
public string Suffix;
public BusinessEntityDTO BusinessEntity;
public IList<BusinessEntityContactDTO> BusinessEntityContacts;
}
public class BusinessEntityDTO
{
public Guid RowGuid;
public PersonDTO Person;
public DateTime ModifiedDate;
public IList<BusinessEntityContactDTO> BusinessEntityContacts;
}
public class BusinessEntityContactDTO
{
public Guid RowGuid;
public BusinessEntityDTO BusinessEntity;
public PersonDTO Person;
}