Skip to content

Commit

Permalink
Merge pull request #137 from AutoMapper/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
TylerCarlson1 authored Dec 25, 2019
2 parents 582dd7f + 20f5cfa commit 40dddc6
Show file tree
Hide file tree
Showing 30 changed files with 417 additions and 329 deletions.
9 changes: 5 additions & 4 deletions AutoMapper.Collection.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
# Visual Studio Version 16
VisualStudioVersion = 16.0.29411.108
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{19AAEE83-5EEC-4EAA-9CF7-16F8ED58B50E}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -10,6 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{19AAEE
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{578F2483-CF08-409D-A316-31BCB7C5D9D0}"
ProjectSection(SolutionItems) = preProject
appveyor.yml = appveyor.yml
Directory.Build.props = Directory.Build.props
global.json = global.json
README.md = README.md
Expand All @@ -22,9 +23,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoMapper.Collection.Entit
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoMapper.Collection.LinqToSQL", "src\AutoMapper.Collection.LinqToSQL\AutoMapper.Collection.LinqToSQL.csproj", "{A0A023B6-D02A-4CD3-9B3D-3B3022DB001A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper.Collection.Tests", "src\AutoMapper.Collection.Tests\AutoMapper.Collection.Tests.csproj", "{2D3D34AD-6A0A-4382-9A2F-894F52D184A7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoMapper.Collection.Tests", "src\AutoMapper.Collection.Tests\AutoMapper.Collection.Tests.csproj", "{2D3D34AD-6A0A-4382-9A2F-894F52D184A7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper.Collection.EntityFramework.Tests", "src\AutoMapper.Collection.EntityFramework.Tests\AutoMapper.Collection.EntityFramework.Tests.csproj", "{BDE127AB-AC3F-44DF-BC33-210DAFD12E15}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoMapper.Collection.EntityFramework.Tests", "src\AutoMapper.Collection.EntityFramework.Tests\AutoMapper.Collection.EntityFramework.Tests.csproj", "{BDE127AB-AC3F-44DF-BC33-210DAFD12E15}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
14 changes: 11 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
<Import Project="version.props" />

<PropertyGroup>
<EFVersion>6.1.3</EFVersion>
<FluentAssertions>4.15.0</FluentAssertions>
<TestSDKVersion>15.5.0</TestSDKVersion>
<xUnitVersion>2.3.1</xUnitVersion>
<SqlServerCompactVersion>4.0.8876.1</SqlServerCompactVersion>
<xUnitVersion>2.4.1</xUnitVersion>
<EffortVersion>2.2.1</EffortVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net461' ">
<EFVersion>6.1.3</EFVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'netcoreapp3.0' ">
<EFVersion>6.3.0</EFVersion>
</PropertyGroup>

</Project>
98 changes: 56 additions & 42 deletions README.md
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
```
5 changes: 4 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ branches:
only:
- master
- development
image: Visual Studio 2017
image: Visual Studio 2019
nuget:
disable_publish_on_pr: true
environment:
DOTNET_CLI_VERSION: 3.0.100
DOTNET_CLI_TELEMETRY_OPTOUT: true
build_script:
- cmd: .\build.cmd
test: off
Expand Down
43 changes: 22 additions & 21 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ task compile -depends clean {
$buildParam = @{ $true = ""; $false = "--version-suffix=$buildSuffix"}[$tag -ne $NULL -and $revision -ne "local"]
$packageParam = @{ $true = ""; $false = "--version-suffix=$suffix"}[$tag -ne $NULL -and $revision -ne "local"]

echo "build: Tag is $tag"
echo "build: Package version suffix is $suffix"
echo "build: Build version suffix is $buildSuffix"
Write-Output "build: Tag is $tag"
Write-Output "build: Package version suffix is $suffix"
Write-Output "build: Build version suffix is $buildSuffix"

# restore all project references (creating project.assets.json for each project)
exec { dotnet restore $base_dir\AutoMapper.Collection.sln /nologo }

exec { dotnet build $base_dir\AutoMapper.Collection.sln -c $config $buildParam /nologo --no-restore }
exec { dotnet build $base_dir\AutoMapper.Collection.sln -c $config $buildParam --no-restore /nologo }

exec { dotnet pack $base_dir\AutoMapper.Collection.sln -c $config --include-symbols --no-build --no-restore --output $artifacts_dir $packageParam /nologo}
exec { dotnet pack $base_dir\AutoMapper.Collection.sln -c $config --include-symbols --no-build --no-restore --output $artifacts_dir $packageParam /nologo }
}

task test {
Expand All @@ -60,29 +60,30 @@ task test {

function Install-Dotnet
{
$dotnetcli = where-is('dotnet')
$dotnetCli = (where-is "dotnet" | Select-Object -First 1)
$install = ($null -eq $dotnetCli -or ($null -ne $env:DOTNET_CLI_VERSION -and $null -eq (&"$dotnetCli" --info | Where-Object { $_ -like " $env:DOTNET_CLI_VERSION*" })))

if($dotnetcli -eq $null)
{
if ($install)
{
$dotnetPath = "$pwd\.dotnet"
$dotnetCliVersion = if ($env:DOTNET_CLI_VERSION -eq $null) { 'Latest' } else { $env:DOTNET_CLI_VERSION }
$dotnetInstallScriptUrl = 'https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain/install.ps1'
$dotnetInstallScriptPath = '.\scripts\obtain\install.ps1'
$dotnetCliVersion = if ($null -eq $env:DOTNET_CLI_VERSION) { 'Latest' } else { $env:DOTNET_CLI_VERSION }
$dotnetInstallScriptUrl = 'https://raw.githubusercontent.com/dotnet/cli/v2.1.4/scripts/obtain/dotnet-install.ps1'
$dotnetInstallScriptPath = '.\scripts\obtain\dotnet-install.ps1'

md -Force ".\scripts\obtain\" | Out-Null
curl $dotnetInstallScriptUrl -OutFile $dotnetInstallScriptPath
& .\scripts\obtain\install.ps1 -Channel "preview" -version $dotnetCliVersion -InstallDir $dotnetPath -NoPath
mkdir -Force ".\scripts\obtain\" | Out-Null
Invoke-WebRequest $dotnetInstallScriptUrl -OutFile $dotnetInstallScriptPath
& .\scripts\obtain\dotnet-install.ps1 -Channel "preview" -version $dotnetCliVersion -InstallDir $dotnetPath -NoPath
$env:Path = "$dotnetPath;$env:Path"
}
}

function where-is($command) {
(ls env:\path).Value.split(';') | `
where { $_ } | `
%{ [System.Environment]::ExpandEnvironmentVariables($_) } | `
where { test-path $_ } |`
%{ ls "$_\*" -include *.bat,*.exe,*cmd } | `
%{ $file = $_.Name; `
(Get-ChildItem env:\path).Value.split(';') | `
Where-Object { $_ } | `
ForEach-Object{ [System.Environment]::ExpandEnvironmentVariables($_) } | `
Where-Object { test-path $_ } |`
ForEach-Object{ Get-ChildItem "$_\*" -include *.bat,*.exe,*cmd } | `
ForEach-Object{ $file = $_.Name; `
if($file -and ($file -eq $command -or `
$file -eq ($command + '.exe') -or `
$file -eq ($command + '.bat') -or `
Expand All @@ -91,5 +92,5 @@ function where-is($command) {
$_.FullName `
} `
} | `
select -unique
Select-Object -unique
}
24 changes: 0 additions & 24 deletions src/AutoMapper.Collection.EntityFramework.Tests/App.config

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net461</TargetFrameworks>
<TargetFrameworks>net461;netcoreapp3.0</TargetFrameworks>
<AssemblyName>AutoMapper.Collection.EntityFramework.Tests</AssemblyName>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand All @@ -10,18 +10,12 @@
<ProjectReference Include="..\AutoMapper.Collection.EntityFramework\AutoMapper.Collection.EntityFramework.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="$(FluentAssertions)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSDKVersion)" />
<PackageReference Include="xunit" Version="$(xUnitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(xUnitVersion)" />
<PackageReference Include="EntityFramework.SqlServerCompact" Version="$(EfVersion)" />
<PackageReference Include="Microsoft.SqlServer.Compact" Version="$(SqlServerCompactVersion)" />
<PackageReference Include="Effort.EF6" Version="$(EffortVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 40dddc6

Please sign in to comment.