Skip to content

Commit e0a5077

Browse files
Merge pull request #124 from Tasteful/feature_AM810
Update to AM 8.1.0
2 parents 5dd4326 + 95a8969 commit e0a5077

17 files changed

+327
-221
lines changed

README.md

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,88 @@
11
<img src="https://s3.amazonaws.com/automapper/logo.png" alt="AutoMapper">
22

3-
AutoMapper.Collection
4-
================================
3+
# AutoMapper.Collection
54
Adds ability to map collections to existing collections without re-creating the collection object.
65

76
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.
87

9-
How to add to AutoMapper?
10-
--------------------------------
8+
## How to add to AutoMapper?
119
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+
```
1817
Will add new IObjectMapper objects into the master mapping list.
1918

20-
Adding equivalency between two classes
21-
--------------------------------
19+
## Adding equivalency between two classes
2220
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+
```
2524
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+
```
2828
If ID's match will map OrderDTO to Order
2929

3030
If OrderDTO exists and Order doesn't add to collection
3131

3232
If Order exists and OrderDTO doesn't remove from collection
3333

34-
Why update collection? Just recreate it
35-
-------------------------------
34+
## Why update collection? Just recreate it
3635
ORMs don't like setting the collection, so you need to add and remove from preexisting one.
3736

3837
This automates the process by just specifying what is equal to each other.
3938

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+
```
5050
User defined equality expressions will overwrite primary key expressions.
5151

52-
What about comparing to a single existing Entity for updating?
53-
--------------------------------
52+
## What about comparing to a single existing Entity for updating?
5453
Automapper.Collection.EntityFramework does that as well through extension method from of DbSet<TEntity>.
5554

5655
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+
```
6262
**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.
6363

6464
Persist doesn't call submit changes automatically
6565

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+
```
6979

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+
```
7384

74-
PM> Install-Package AutoMapper.Collection.LinqToSQL
85+
#### AutoMapper Collection for LinqToSQL
86+
```
87+
PM> Install-Package AutoMapper.Collection.LinqToSQL
88+
```

src/AutoMapper.Collection.EntityFramework.Tests/AssemblyInfo.cs

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/AutoMapper.Collection.EntityFramework.Tests/EntityFramworkTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@
1010

1111
namespace AutoMapper.Collection.EntityFramework.Tests
1212
{
13-
public class EntityFramworkTests
13+
public class EntityFramworkTests : MappingTestBase
1414
{
15-
public EntityFramworkTests()
15+
private void ConfigureMapper(IMapperConfigurationExpression cfg)
1616
{
17-
Mapper.Reset();
18-
Mapper.Initialize(x =>
19-
{
20-
x.AddCollectionMappers();
21-
x.CreateMap<ThingDto, Thing>().ReverseMap();
22-
x.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DB>>();
23-
});
17+
cfg.AddCollectionMappers();
18+
cfg.CreateMap<ThingDto, Thing>().ReverseMap();
19+
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DB>>();
2420
}
2521

2622
[Fact]
2723
public void Should_Persist_To_Update()
2824
{
25+
var mapper = CreateMapper(ConfigureMapper);
26+
2927
var db = new DB();
3028
db.Things.Add(new Thing { Title = "Test2" });
3129
db.Things.Add(new Thing { Title = "Test3" });
@@ -36,7 +34,7 @@ public void Should_Persist_To_Update()
3634

3735
var item = db.Things.First();
3836

39-
db.Things.Persist().InsertOrUpdate(new ThingDto { ID = item.ID, Title = "Test" });
37+
db.Things.Persist(mapper).InsertOrUpdate(new ThingDto { ID = item.ID, Title = "Test" });
4038
Assert.Equal(1, db.ChangeTracker.Entries<Thing>().Count(x => x.State == EntityState.Modified));
4139

4240
Assert.Equal(3, db.Things.Count());
@@ -47,6 +45,8 @@ public void Should_Persist_To_Update()
4745
[Fact]
4846
public void Should_Persist_To_Insert()
4947
{
48+
var mapper = CreateMapper(ConfigureMapper);
49+
5050
var db = new DB();
5151
db.Things.Add(new Thing { Title = "Test2" });
5252
db.Things.Add(new Thing { Title = "Test3" });
@@ -55,7 +55,7 @@ public void Should_Persist_To_Insert()
5555

5656
Assert.Equal(3, db.Things.Count());
5757

58-
db.Things.Persist().InsertOrUpdate(new ThingDto { Title = "Test" });
58+
db.Things.Persist(mapper).InsertOrUpdate(new ThingDto { Title = "Test" });
5959
Assert.Equal(3, db.Things.Count());
6060
Assert.Equal(1, db.ChangeTracker.Entries<Thing>().Count(x => x.State == EntityState.Added));
6161

@@ -69,7 +69,7 @@ public void Should_Persist_To_Insert()
6969
public class DB : DbContext
7070
{
7171
public DB()
72-
: base(new SqlCeConnection("Data Source=MyDatabase.sdf;Persist Security Info=False;"), contextOwnsConnection: true)
72+
: base(new SqlCeConnection($"Data Source={Guid.NewGuid()}.sdf;Persist Security Info=False;"), contextOwnsConnection: true)
7373
{
7474
Things.RemoveRange(Things);
7575
SaveChanges();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace AutoMapper.Collection
4+
{
5+
public abstract class MappingTestBase
6+
{
7+
protected IMapper CreateMapper(Action<IMapperConfigurationExpression> cfg)
8+
{
9+
var map = new MapperConfiguration(cfg);
10+
map.CompileMappings();
11+
12+
var mapper = map.CreateMapper();
13+
mapper.ConfigurationProvider.AssertConfigurationIsValid();
14+
return mapper;
15+
}
16+
}
17+
}

src/AutoMapper.Collection.Tests/AssemblyInfo.cs

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/AutoMapper.Collection.Tests/InheritanceWithCollectionTests.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
using AutoMapper.EquivalencyExpression;
2-
using System;
32
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
74
using FluentAssertions;
85
using Xunit;
96

107
namespace AutoMapper.Collection
118
{
12-
public abstract class InheritanceWithCollectionTests
9+
public abstract class InheritanceWithCollectionTests : MappingTestBase
1310
{
14-
protected abstract void Create(IMapperConfigurationExpression cfg);
15-
16-
private IMapper CreateMapper()
17-
{
18-
var map = new MapperConfiguration(Create);
19-
map.AssertConfigurationIsValid();
20-
map.CompileMappings();
21-
22-
return map.CreateMapper();
23-
}
11+
protected abstract void ConfigureMapper(IMapperConfigurationExpression cfg);
2412

2513
[Fact]
2614
public void TypeMap_Should_include_base_types()
2715
{
28-
var mapper = CreateMapper();
16+
var mapper = CreateMapper(ConfigureMapper);
2917
var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(typeof(MailOrderDomain), typeof(OrderEf));
3018

3119
var typePairs = new[]{
@@ -37,7 +25,7 @@ public void TypeMap_Should_include_base_types()
3725
[Fact]
3826
public void TypeMap_Should_include_derivied_types()
3927
{
40-
var mapper = CreateMapper();
28+
var mapper = CreateMapper(ConfigureMapper);
4129
var typeMap = mapper.ConfigurationProvider.ResolveTypeMap(typeof(OrderDomain), typeof(OrderEf));
4230

4331
var typePairs = new[]{
@@ -50,7 +38,7 @@ public void TypeMap_Should_include_derivied_types()
5038
[Fact]
5139
public void Map_Should_ReturnOnlineOrderEf_When_ListIsOfTypeOrderEf()
5240
{
53-
var mapper = CreateMapper();
41+
var mapper = CreateMapper(ConfigureMapper);
5442

5543
//arrange
5644
var orderDomain = new OnlineOrderDomain { Id = "Id", Key = "Key" };
@@ -85,7 +73,7 @@ public void Map_Should_ReturnOnlineOrderEf_When_ListIsOfTypeOrderEf()
8573
[Fact]
8674
public void Map_FromEfToDomain_And_AddAnOnlineOrderInTheDomainObject_And_ThenMapBackToEf_Should_UseTheSameReferenceInTheEfCollection()
8775
{
88-
var mapper = CreateMapper();
76+
var mapper = CreateMapper(ConfigureMapper);
8977

9078
//arrange
9179
var onlineOrderEf = new OnlineOrderEf { Id = "Id", Key = "Key" };
@@ -193,7 +181,7 @@ public List<OrderEf> Resolve(RootDomain source, RootEf destination, List<OrderEf
193181

194182
public class Include : InheritanceWithCollectionTests
195183
{
196-
protected override void Create(IMapperConfigurationExpression cfg)
184+
protected override void ConfigureMapper(IMapperConfigurationExpression cfg)
197185
{
198186
cfg.ShouldMapProperty = propertyInfo => propertyInfo.GetMethod.IsPublic || propertyInfo.GetMethod.IsAssembly || propertyInfo.GetMethod.IsFamily || propertyInfo.GetMethod.IsPrivate;
199187
cfg.AddCollectionMappers();
@@ -238,7 +226,7 @@ protected override void Create(IMapperConfigurationExpression cfg)
238226

239227
public class IncludeBase : InheritanceWithCollectionTests
240228
{
241-
protected override void Create(IMapperConfigurationExpression cfg)
229+
protected override void ConfigureMapper(IMapperConfigurationExpression cfg)
242230
{
243231
cfg.ShouldMapProperty = propertyInfo => propertyInfo.GetMethod.IsPublic || propertyInfo.GetMethod.IsAssembly || propertyInfo.GetMethod.IsFamily || propertyInfo.GetMethod.IsPrivate;
244232
cfg.AddCollectionMappers();

0 commit comments

Comments
 (0)