automapperdev - AutoMapper.Collection-Signed 3.1.0-ci-00039

Collection Add/Remove/Update support for AutoMapper. AutoMapper.Collection adds EqualityComparison Expressions for TypeMaps to determine if Source and Destination type are equivalent to each other when mapping collections.

PM> Install-Package AutoMapper.Collection-Signed -Version 3.1.0-ci-00039 -Source https://www.myget.org/F/automapperdev/api/v3/index.json

Copy to clipboard

> nuget.exe install AutoMapper.Collection-Signed -Version 3.1.0-ci-00039 -Source https://www.myget.org/F/automapperdev/api/v3/index.json

Copy to clipboard

> dotnet add package AutoMapper.Collection-Signed --version 3.1.0-ci-00039 --source https://www.myget.org/F/automapperdev/api/v3/index.json

Copy to clipboard
<PackageReference Include="AutoMapper.Collection-Signed" Version="3.1.0-ci-00039" />
Copy to clipboard
source https://www.myget.org/F/automapperdev/api/v3/index.json

nuget AutoMapper.Collection-Signed  ~> 3.1.0-ci-00039
Copy to clipboard

> choco install AutoMapper.Collection-Signed --version 3.1.0-ci-00039 --source https://www.myget.org/F/automapperdev/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "automapperdev" -SourceLocation "https://www.myget.org/F/automapperdev/api/v2"
Install-Module -Name "AutoMapper.Collection-Signed" -RequiredVersion "3.1.0-ci-00039" -Repository "automapperdev" -AllowPreRelease
Copy to clipboard

AutoMapper

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.

NuGet

How to add to AutoMapper?

Call AddCollectionMappers when configuring

Mapper.Initialize(cfg =>
{
    cfg.AddCollectionMappers();
    // Configuration code
});

Will add new IObjectMapper objects into the master mapping list.

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);

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);

If ID's match, then AutoMapper will map OrderDTO to Order

If OrderDTO exists and Order doesn't, then AutoMapper will add a new Order mapped from OrderDTO to the collection

If Order exists and OrderDTO doesn't, then AutoMapper will remove Order from collection

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 Entity Framework?

Automapper.Collection.EntityFramework or Automapper.Collection.EntityFrameworkCore can do that for you.

Mapper.Initialize(cfg =>
{
    cfg.AddCollectionMappers();
// entity framework
    cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DB>>();
// entity framework core
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkCorePrimaryKeyPropertyMaps<DB>>();
    // Configuration code
});

User defined equality expressions will overwrite primary key expressions.

What about comparing to a single existing Entity for updating?

Automapper.Collection.EntityFramework does that as well through extension method from of DbSet

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();

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

Where can I get it?

First, install NuGet. Then, install 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

AutoMapper Collection for Entity Framework Core

PM> Install-Package AutoMapper.Collection.EntityFrameworkCore

AutoMapper Collection for LinqToSQL

PM> Install-Package AutoMapper.Collection.LinqToSQL
  • .NETFramework 4.5
    • AutoMapper (>= 6.0.0)
  • .NETStandard 1.3
    • AutoMapper (>= 6.0.0)
    • NETStandard.Library (>= 1.6.1)
    • System.Threading (>= 4.0.11)
  • .NETStandard 1.1
    • AutoMapper (>= 6.0.0)
    • NETStandard.Library (>= 1.6.1)
    • System.Threading (>= 4.0.11)
  • .NETFramework 4.5: 4.5.0.0
  • .NETStandard 1.1: 1.1.0.0
  • .NETStandard 1.3: 1.3.0.0

Owners

Jimmy Bogard

Authors

Tyler Carlson

Project URL

https://github.com/AutoMapper/Automapper.Collection

License

Unknown

Info

8 total downloads
8 downloads for version 3.1.0-ci-00039
Download (52.14 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
3.1.0-ci-00039 52.14 KB Fri, 30 Jun 2017 05:26:37 GMT 8