-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from AutoMapper/development
Development
- Loading branch information
Showing
30 changed files
with
417 additions
and
329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,88 @@ | ||
<img src="https://s3.amazonaws.com/automapper/logo.png" alt="AutoMapper"> | ||
|
||
AutoMapper.Collection | ||
================================ | ||
# AutoMapper.Collection | ||
Adds ability to map collections to existing collections without re-creating the collection object. | ||
|
||
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. | ||
|
||
How to add to AutoMapper? | ||
-------------------------------- | ||
## How to add to AutoMapper? | ||
Call AddCollectionMappers when configuring | ||
|
||
Mapper.Initialize(cfg => | ||
{ | ||
cfg.AddCollectionMappers(); | ||
// Configuration code | ||
}); | ||
``` | ||
Mapper.Initialize(cfg => | ||
{ | ||
cfg.AddCollectionMappers(); | ||
// Configuration code | ||
}); | ||
``` | ||
Will add new IObjectMapper objects into the master mapping list. | ||
|
||
Adding equivalency between two classes | ||
-------------------------------- | ||
## Adding equivalency between two classes | ||
Adding equivalence to objects is done with EqualityComparison extended from the IMappingExpression class. | ||
|
||
cfg.CreateMap<OrderItemDTO, OrderItem>().EqualityComparison((odto, o) => odto.ID == o.ID); | ||
``` | ||
cfg.CreateMap<OrderItemDTO, OrderItem>().EqualityComparison((odto, o) => odto.ID == o.ID); | ||
``` | ||
Mapping OrderDTO back to Order will compare Order items list based on if their ID's match | ||
|
||
Mapper.Map<List<OrderDTO>,List<Order>>(orderDtos, orders); | ||
``` | ||
Mapper.Map<List<OrderDTO>,List<Order>>(orderDtos, orders); | ||
``` | ||
If ID's match will map OrderDTO to Order | ||
|
||
If OrderDTO exists and Order doesn't add to collection | ||
|
||
If Order exists and OrderDTO doesn't remove from collection | ||
|
||
Why update collection? Just recreate it | ||
------------------------------- | ||
## Why update collection? Just recreate it | ||
ORMs don't like setting the collection, so you need to add and remove from preexisting one. | ||
|
||
This automates the process by just specifying what is equal to each other. | ||
|
||
Can it just figure out the ID equivalency for me in EF? | ||
------------------------------- | ||
Automapper.Collection.EntityFramework can do that for you. | ||
|
||
Mapper.Initialize(cfg => | ||
{ | ||
cfg.AddCollectionMappers(); | ||
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DB>>(); | ||
// Configuration code | ||
}); | ||
## Can it just figure out the ID equivalency for me in Entity Framework? | ||
`Automapper.Collection.EntityFramework` or `Automapper.Collection.EntityFrameworkCore` can do that for you. | ||
|
||
``` | ||
Mapper.Initialize(cfg => | ||
{ | ||
cfg.AddCollectionMappers(); | ||
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DB>>(); | ||
// Configuration code | ||
}); | ||
``` | ||
User defined equality expressions will overwrite primary key expressions. | ||
|
||
What about comparing to a single existing Entity for updating? | ||
-------------------------------- | ||
## What about comparing to a single existing Entity for updating? | ||
Automapper.Collection.EntityFramework does that as well through extension method from of DbSet<TEntity>. | ||
|
||
Translate equality between dto and EF object to an expression of just the EF using the dto's values as constants. | ||
|
||
dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(newOrderDto); | ||
dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(existingOrderDto); | ||
dbContext.Orders.Persist().Remove<OrderDTO>(deletedOrderDto); | ||
dbContext.SubmitChanges(); | ||
``` | ||
dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(newOrderDto); | ||
dbContext.Orders.Persist().InsertOrUpdate<OrderDTO>(existingOrderDto); | ||
dbContext.Orders.Persist().Remove<OrderDTO>(deletedOrderDto); | ||
dbContext.SubmitChanges(); | ||
``` | ||
**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. | ||
|
||
Persist doesn't call submit changes automatically | ||
|
||
How to get it | ||
-------------------------------- | ||
On Nuget | ||
## Where can I get it? | ||
|
||
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: | ||
``` | ||
PM> Install-Package AutoMapper.Collection | ||
``` | ||
|
||
### Additional packages | ||
|
||
#### AutoMapper Collection for Entity Framework | ||
``` | ||
PM> Install-Package AutoMapper.Collection.EntityFramework | ||
``` | ||
|
||
PM> Install-Package AutoMapper.Collection | ||
PM> Install-Package AutoMapper.Collection.EntityFramework | ||
Also have AutoMapper.LinqToSQL | ||
#### AutoMapper Collection for Entity Framework Core | ||
``` | ||
PM> Install-Package AutoMapper.Collection.EntityFrameworkCore | ||
``` | ||
|
||
PM> Install-Package AutoMapper.Collection.LinqToSQL | ||
#### AutoMapper Collection for LinqToSQL | ||
``` | ||
PM> Install-Package AutoMapper.Collection.LinqToSQL | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
src/AutoMapper.Collection.EntityFramework.Tests/App.config
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
src/AutoMapper.Collection.EntityFramework.Tests/AssemblyInfo.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.