Skip to content

Commit a14c730

Browse files
author
Dmitry Panyushkin
authored
Update README.md
1 parent 694a216 commit a14c730

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,69 @@
11
# AutoMapper.ExtendedConverters
2-
AutoMapper.ExtendedConverters is a small set of useful AutoMapper custom Type Converters
2+
AutoMapper.ExtendedConverters is a small set of useful AutoMapper custom Type Converters
3+
4+
```cs
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using AutoMapper;
8+
using AutoMapper.ExtendedConverters;
9+
10+
class Model
11+
{
12+
public int Id { get; set; }
13+
public string Text { get; set; }
14+
}
15+
16+
class Entity
17+
{
18+
public int Id { get; set; }
19+
public string Text { get; set; }
20+
}
21+
22+
class Program
23+
{
24+
static IMapper Mapper;
25+
26+
static void Configure()
27+
{
28+
var config = new MapperConfiguration(cfg =>
29+
{
30+
cfg.CreateMap<Model, Entity>();
31+
// create custom mapper for lists
32+
cfg.CreateMap<List<Model>, List<Entity>>()
33+
// configure 'Id' property selectors for Model and Entity
34+
.UsingListConverter(m => m.Id, e => e.Id);
35+
});
36+
37+
Mapper = config.CreateMapper();
38+
}
39+
40+
static void Main()
41+
{
42+
// values from Server
43+
var dest = new List<Entity>
44+
{
45+
new Entity { Id = 1, Text = "a" },
46+
new Entity { Id = 2, Text = "b" },
47+
};
48+
// values from Client
49+
var src = new List<Model>
50+
{
51+
new Model { Id = 1, Text = "A" },
52+
new Model { Id = 3, Text = "C" },
53+
};
54+
55+
List<Entity> res = Mapper.Map(src, dest);
56+
57+
// now res is equivalent to
58+
new List<Entity>
59+
{
60+
// Entity with Id == 1 is updated
61+
new Entity { Id = 1, Text = "A" },
62+
// Entity with Id == 2 is removed
63+
// ...
64+
// Entity with Id == 3 is added
65+
new Entity { Id = 3, Text = "C" },
66+
}
67+
}
68+
}
69+
```

0 commit comments

Comments
 (0)