|
1 | 1 | <img src="https://s3.amazonaws.com/automapper/logo.png" alt="AutoMapper"> |
2 | 2 |
|
3 | | -AutoMapper.Collection |
4 | | -================================ |
| 3 | +# AutoMapper.Collection |
5 | 4 | Adds ability to map collections to existing collections without re-creating the collection object. |
6 | 5 |
|
7 | 6 | Will Add/Update/Delete items from a preexisting collection object based on user defined equivalency between the collection's generic item type from the source collection and the destination collection. |
8 | 7 |
|
9 | | -How to add to AutoMapper? |
10 | | --------------------------------- |
| 8 | +## How to add to AutoMapper? |
11 | 9 | Call AddCollectionMappers when configuring |
12 | | - |
13 | | - Mapper.Initialize(cfg => |
14 | | - { |
15 | | - cfg.AddCollectionMappers(); |
16 | | - // Configuration code |
17 | | - }); |
| 10 | +``` |
| 11 | +Mapper.Initialize(cfg => |
| 12 | +{ |
| 13 | + cfg.AddCollectionMappers(); |
| 14 | + // Configuration code |
| 15 | +}); |
| 16 | +``` |
18 | 17 | Will add new IObjectMapper objects into the master mapping list. |
19 | 18 |
|
20 | | -Adding equivalency between two classes |
21 | | --------------------------------- |
| 19 | +## Adding equivalency between two classes |
22 | 20 | Adding equivalence to objects is done with EqualityComparison extended from the IMappingExpression class. |
23 | | - |
24 | | - cfg.CreateMap<OrderItemDTO, OrderItem>().EqualityComparison((odto, o) => odto.ID == o.ID); |
| 21 | +``` |
| 22 | +cfg.CreateMap<OrderItemDTO, OrderItem>().EqualityComparison((odto, o) => odto.ID == o.ID); |
| 23 | +``` |
25 | 24 | Mapping OrderDTO back to Order will compare Order items list based on if their ID's match |
26 | | - |
27 | | - Mapper.Map<List<OrderDTO>,List<Order>>(orderDtos, orders); |
| 25 | +``` |
| 26 | +Mapper.Map<List<OrderDTO>,List<Order>>(orderDtos, orders); |
| 27 | +``` |
28 | 28 | If ID's match will map OrderDTO to Order |
29 | 29 |
|
30 | 30 | If OrderDTO exists and Order doesn't add to collection |
31 | 31 |
|
32 | 32 | If Order exists and OrderDTO doesn't remove from collection |
33 | 33 |
|
34 | | -Why update collection? Just recreate it |
35 | | -------------------------------- |
| 34 | +## Why update collection? Just recreate it |
36 | 35 | ORMs don't like setting the collection, so you need to add and remove from preexisting one. |
37 | 36 |
|
38 | 37 | This automates the process by just specifying what is equal to each other. |
39 | 38 |
|
40 | | -Can it just figure out the ID equivalency for me in EF? |
41 | | -------------------------------- |
42 | | -Automapper.Collection.EntityFramework can do that for you. |
43 | | - |
44 | | - Mapper.Initialize(cfg => |
45 | | - { |
46 | | - cfg.AddCollectionMappers(); |
47 | | - cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DB>>(); |
48 | | - // Configuration code |
49 | | - }); |
| 39 | +## Can it just figure out the ID equivalency for me in Entity Framework? |
| 40 | +`Automapper.Collection.EntityFramework` or `Automapper.Collection.EntityFrameworkCore` can do that for you. |
| 41 | + |
| 42 | +``` |
| 43 | +Mapper.Initialize(cfg => |
| 44 | +{ |
| 45 | + cfg.AddCollectionMappers(); |
| 46 | + cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DB>>(); |
| 47 | + // Configuration code |
| 48 | +}); |
| 49 | +``` |
50 | 50 | User defined equality expressions will overwrite primary key expressions. |
51 | 51 |
|
52 | | -What about comparing to a single existing Entity for updating? |
53 | | --------------------------------- |
| 52 | +## What about comparing to a single existing Entity for updating? |
54 | 53 | Automapper.Collection.EntityFramework does that as well through extension method from of DbSet<TEntity>. |
55 | 54 |
|
56 | 55 | Translate equality between dto and EF object to an expression of just the EF using the dto's values as constants. |
57 | | - |
58 | | - dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(newOrderDto); |
59 | | - dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(existingOrderDto); |
60 | | - dbContext.Orders.Persist().Remove<OrderDTO>(deletedOrderDto); |
61 | | - dbContext.SubmitChanges(); |
| 56 | +``` |
| 57 | +dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(newOrderDto); |
| 58 | +dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(existingOrderDto); |
| 59 | +dbContext.Orders.Persist().Remove<OrderDTO>(deletedOrderDto); |
| 60 | +dbContext.SubmitChanges(); |
| 61 | +``` |
62 | 62 | **Note:** This is done by converting the OrderDTO to Expression<Func<Order,bool>> and using that to find matching type in the database. You can also map objects to expressions as well. |
63 | 63 |
|
64 | 64 | Persist doesn't call submit changes automatically |
65 | 65 |
|
66 | | -How to get it |
67 | | --------------------------------- |
68 | | -On Nuget |
| 66 | +## Where can I get it? |
| 67 | + |
| 68 | +First, [install NuGet](http://docs.nuget.org/docs/start-here/installing-nuget). Then, install [AutoMapper.Collection](https://www.nuget.org/packages/AutoMapper.Collection/) from the package manager console: |
| 69 | +``` |
| 70 | +PM> Install-Package AutoMapper.Collection |
| 71 | +``` |
| 72 | + |
| 73 | +### Additional packages |
| 74 | + |
| 75 | +#### AutoMapper Collection for Entity Framework |
| 76 | +``` |
| 77 | +PM> Install-Package AutoMapper.Collection.EntityFramework |
| 78 | +``` |
69 | 79 |
|
70 | | - PM> Install-Package AutoMapper.Collection |
71 | | - PM> Install-Package AutoMapper.Collection.EntityFramework |
72 | | -Also have AutoMapper.LinqToSQL |
| 80 | +#### AutoMapper Collection for Entity Framework Core |
| 81 | +``` |
| 82 | +PM> Install-Package AutoMapper.Collection.EntityFrameworkCore |
| 83 | +``` |
73 | 84 |
|
74 | | - PM> Install-Package AutoMapper.Collection.LinqToSQL |
| 85 | +#### AutoMapper Collection for LinqToSQL |
| 86 | +``` |
| 87 | +PM> Install-Package AutoMapper.Collection.LinqToSQL |
| 88 | +``` |
0 commit comments