├── .github
└── workflows
│ ├── push.yml
│ └── tag.yml
├── .gitignore
├── .travis.yml
├── AutoMapper.Contrib.Autofac.DependencyInjection.sln
├── AutoMapper.Contrib.Autofac.DependencyInjection.sln.DotSettings
├── Directory.Build.props
├── README.md
├── changelog.md
├── global.json
├── icon.png
├── licence.md
├── publish-packages.sh
├── samples
├── AutoMapper.Autofac.ConsoleApp
│ ├── AutoMapper.Autofac.ConsoleApp.csproj
│ └── Program.cs
├── AutoMapper.Autofac.Shared
│ ├── AutoMapper.Autofac.Shared.csproj
│ ├── Converters
│ │ └── CustomerConverter.cs
│ ├── Dtos
│ │ └── CustomerDto.cs
│ ├── Entities
│ │ └── Customer.cs
│ └── Profiles
│ │ └── CustomerProfile.cs
└── AutoMapper.Autofac.WebApi
│ ├── AutoMapper.Autofac.WebApi.csproj
│ ├── Controllers
│ └── CustomersController.cs
│ ├── Program.cs
│ ├── Properties
│ └── launchSettings.json
│ └── Startup.cs
├── src
└── AutoMapper.Contrib.Autofac.DependencyInjection
│ ├── AutoMapper.Contrib.Autofac.DependencyInjection.csproj
│ ├── AutoMapperModule.cs
│ ├── ContainerBuilderExtensions.cs
│ └── MapperConfigurationExpressionAdapter.cs
├── test.sh
└── test
├── AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly
├── AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly.csproj
├── ObjectDestination.cs
├── ObjectSource.cs
└── SecondAssemblyProfile.cs
└── AutoMapper.Contrib.Autofac.DependencyInjection.Tests
├── AutoMapper.Contrib.Autofac.DependencyInjection.Tests.csproj
├── ContainerBuilderExtensionTests.cs
├── Converter
└── NoopStringValueConverter.cs
├── Dtos
├── CustomerAttributeDto.cs
├── CustomerDto.cs
└── NameDto.cs
├── Entities
├── Customer.cs
└── Name.cs
├── Profiles
├── CustomerProfile.cs
├── Dependency.cs
├── NameProfile.cs
└── ProfileWithDependency.cs
├── Resolver
├── CustomerFullNameResolver.cs
├── CustomerTypeConverter.cs
└── IsFirstNameValidResolver.cs
└── TestConfiguration.cs
/.github/workflows/push.yml:
--------------------------------------------------------------------------------
1 | on: push
2 | name: Build Application
3 | jobs:
4 | build:
5 | runs-on: ubuntu-latest
6 |
7 | steps:
8 | - name: Checkout
9 | uses: actions/checkout@v2
10 |
11 | - name: Setup dotnet
12 | uses: actions/setup-dotnet@v3
13 | with:
14 | global-json-file: global.json
15 |
16 | - name: Build solution
17 | run: dotnet build -c Release
18 |
19 | - name: Test solution and upload code coverage
20 | run: |
21 | chmod +x test.sh
22 | ./test.sh
23 |
--------------------------------------------------------------------------------
/.github/workflows/tag.yml:
--------------------------------------------------------------------------------
1 | on:
2 | push:
3 | tags:
4 | - '*'
5 | name: Release package
6 | jobs:
7 | build:
8 | runs-on: ubuntu-latest
9 |
10 | steps:
11 | - name: Checkout
12 | uses: actions/checkout@v2
13 |
14 | - name: Setup dotnet
15 | uses: actions/setup-dotnet@v3
16 | with:
17 | global-json-file: global.json
18 |
19 | - name: Publish package
20 | run: |
21 | export TAG=${{github.ref_name}}
22 | export NUGET_API_URL=https://api.nuget.org/v3/index.json
23 | export NUGET_KEY=${{secrets.NUGET_KEY}}
24 | chmod +x ./publish-packages.sh
25 | ./publish-packages.sh
26 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | [Xx]64/
19 | [Xx]86/
20 | [Bb]uild/
21 | bld/
22 | [Bb]in/
23 | [Oo]bj/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opendb
80 | *.opensdf
81 | *.sdf
82 | *.cachefile
83 | *.VC.db
84 |
85 | # Visual Studio profiler
86 | *.psess
87 | *.vsp
88 | *.vspx
89 | *.sap
90 |
91 | # TFS 2012 Local Workspace
92 | $tf/
93 |
94 | # Guidance Automation Toolkit
95 | *.gpState
96 |
97 | # ReSharper is a .NET coding add-in
98 | _ReSharper*/
99 | *.[Rr]e[Ss]harper
100 | *.DotSettings.user
101 |
102 | # JustCode is a .NET coding add-in
103 | .JustCode
104 |
105 | # TeamCity is a build add-in
106 | _TeamCity*
107 |
108 | # DotCover is a Code Coverage Tool
109 | *.dotCover
110 |
111 | # NCrunch
112 | _NCrunch_*
113 | .*crunch*.local.xml
114 | nCrunchTemp_*
115 |
116 | # MightyMoose
117 | *.mm.*
118 | AutoTest.Net/
119 |
120 | # Web workbench (sass)
121 | .sass-cache/
122 |
123 | # Installshield output folder
124 | [Ee]xpress/
125 |
126 | # DocProject is a documentation generator add-in
127 | DocProject/buildhelp/
128 | DocProject/Help/*.HxT
129 | DocProject/Help/*.HxC
130 | DocProject/Help/*.hhc
131 | DocProject/Help/*.hhk
132 | DocProject/Help/*.hhp
133 | DocProject/Help/Html2
134 | DocProject/Help/html
135 |
136 | # Click-Once directory
137 | publish/
138 |
139 | # Publish Web Output
140 | *.[Pp]ublish.xml
141 | *.azurePubxml
142 |
143 | # TODO: Un-comment the next line if you do not want to checkin
144 | # your web deploy settings because they may include unencrypted
145 | # passwords
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # NuGet Packages
150 | *.nupkg
151 | # The packages folder can be ignored because of Package Restore
152 | **/packages/*
153 | # except build/, which is used as an MSBuild target.
154 | !**/packages/build/
155 | # Uncomment if necessary however generally it will be regenerated when needed
156 | #!**/packages/repositories.config
157 | # NuGet v3's project.json files produces more ignoreable files
158 | *.nuget.props
159 | *.nuget.targets
160 |
161 | # Microsoft Azure Build Output
162 | csx/
163 | *.build.csdef
164 |
165 | # Microsoft Azure Emulator
166 | ecf/
167 | rcf/
168 |
169 | # Windows Store app package directory
170 | AppPackages/
171 | BundleArtifacts/
172 |
173 | # Visual Studio cache files
174 | # files ending in .cache can be ignored
175 | *.[Cc]ache
176 | # but keep track of directories ending in .cache
177 | !*.[Cc]ache/
178 |
179 | # Others
180 | ClientBin/
181 | [Ss]tyle[Cc]op.*
182 | ~$*
183 | *~
184 | *.dbmdl
185 | *.dbproj.schemaview
186 | *.pfx
187 | *.publishsettings
188 | node_modules/
189 | orleans.codegen.cs
190 |
191 | # RIA/Silverlight projects
192 | Generated_Code/
193 |
194 | # Backup & report files from converting an old project file
195 | # to a newer Visual Studio version. Backup files are not needed,
196 | # because we have git ;-)
197 | _UpgradeReport_Files/
198 | Backup*/
199 | UpgradeLog*.XML
200 | UpgradeLog*.htm
201 |
202 | # SQL Server files
203 | *.mdf
204 | *.ldf
205 |
206 | # Business Intelligence projects
207 | *.rdl.data
208 | *.bim.layout
209 | *.bim_*.settings
210 |
211 | # Microsoft Fakes
212 | FakesAssemblies/
213 |
214 | # GhostDoc plugin setting file
215 | *.GhostDoc.xml
216 |
217 | # Node.js Tools for Visual Studio
218 | .ntvs_analysis.dat
219 |
220 | # Visual Studio 6 build log
221 | *.plg
222 |
223 | # Visual Studio 6 workspace options file
224 | *.opt
225 |
226 | # Visual Studio LightSwitch build output
227 | **/*.HTMLClient/GeneratedArtifacts
228 | **/*.DesktopClient/GeneratedArtifacts
229 | **/*.DesktopClient/ModelManifest.xml
230 | **/*.Server/GeneratedArtifacts
231 | **/*.Server/ModelManifest.xml
232 | _Pvt_Extensions
233 |
234 | # LightSwitch generated files
235 | GeneratedArtifacts/
236 | ModelManifest.xml
237 |
238 | # Paket dependency manager
239 | .paket/paket.exe
240 |
241 | # FAKE - F# Make
242 | .fake/
243 |
244 | # Rider files
245 | .idea/
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | dist: bionic
2 | language: csharp
3 | solution: AutoMapper.Contrib.Autofac.DependencyInjection.sln
4 | mono: none
5 | dotnet: 6.0.100
6 |
7 | env:
8 | global:
9 | - DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true
10 | - DOTNET_CLI_TELEMETRY_OPTOUT=true
11 |
12 | addons:
13 | apt:
14 | update: true
15 |
16 | stages:
17 | - build
18 | - test
19 | - publish
20 |
21 | jobs:
22 | include:
23 | - stage: build
24 | name: dotnet build
25 | script: dotnet build
26 | - stage: test
27 | name: dotnet test
28 | before_script:
29 | - sudo apt-get update && sudo apt-get install curl -y
30 | script: ./test.sh
31 | - stage: publish
32 | name: publish-packages
33 | if: tag IS present
34 | script: ./publish-packages.sh "$NUGET_SOURCE" "$NUGET_KEY" "$TRAVIS_TAG"
35 |
36 |
--------------------------------------------------------------------------------
/AutoMapper.Contrib.Autofac.DependencyInjection.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.28803.452
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C87C815F-D309-4846-933A-45221E6AC4E2}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{7E905FA2-EB17-4082-92C8-244C3933783F}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "misc", "misc", "{11A83071-C2B6-4625-AA44-60F23A20C11B}"
11 | ProjectSection(SolutionItems) = preProject
12 | .gitignore = .gitignore
13 | changelog.md = changelog.md
14 | licence.md = licence.md
15 | publish-packages.sh = publish-packages.sh
16 | test.sh = test.sh
17 | README.md = README.md
18 | global.json = global.json
19 | Directory.Build.props = Directory.Build.props
20 | icon.png = icon.png
21 | EndProjectSection
22 | EndProject
23 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoMapper.Contrib.Autofac.DependencyInjection", "src\AutoMapper.Contrib.Autofac.DependencyInjection\AutoMapper.Contrib.Autofac.DependencyInjection.csproj", "{97B8F460-4AB3-4E6D-99A0-33ADC9E5578A}"
24 | EndProject
25 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoMapper.Contrib.Autofac.DependencyInjection.Tests", "test\AutoMapper.Contrib.Autofac.DependencyInjection.Tests\AutoMapper.Contrib.Autofac.DependencyInjection.Tests.csproj", "{6E420516-09BB-4378-B65F-E02C37CD66D2}"
26 | EndProject
27 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{243D0BEA-EA8B-4D7E-B645-BE17B9763896}"
28 | EndProject
29 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper.Autofac.WebApi", "samples\AutoMapper.Autofac.WebApi\AutoMapper.Autofac.WebApi.csproj", "{67EAE13C-79FF-4E0F-8128-D69322CD2E34}"
30 | EndProject
31 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper.Autofac.ConsoleApp", "samples\AutoMapper.Autofac.ConsoleApp\AutoMapper.Autofac.ConsoleApp.csproj", "{5EADD7CB-210A-4169-A0DF-C4AE7EDBE731}"
32 | EndProject
33 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper.Autofac.Shared", "samples\AutoMapper.Autofac.Shared\AutoMapper.Autofac.Shared.csproj", "{518E0DB7-8C83-41C6-A020-5AF0DFF05F19}"
34 | EndProject
35 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{717509EE-6F82-4DCD-9110-B5EEFFDBA678}"
36 | EndProject
37 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{1393ACB8-9707-446C-B6FD-4C2513B43592}"
38 | ProjectSection(SolutionItems) = preProject
39 | .github\workflows\push.yml = .github\workflows\push.yml
40 | .github\workflows\tag.yml = .github\workflows\tag.yml
41 | EndProjectSection
42 | EndProject
43 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly", "test\AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly\AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly.csproj", "{9772890F-1F2A-4085-AF2B-384DF3ED45E1}"
44 | EndProject
45 | Global
46 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
47 | Debug|Any CPU = Debug|Any CPU
48 | Release|Any CPU = Release|Any CPU
49 | EndGlobalSection
50 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
51 | {97B8F460-4AB3-4E6D-99A0-33ADC9E5578A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
52 | {97B8F460-4AB3-4E6D-99A0-33ADC9E5578A}.Debug|Any CPU.Build.0 = Debug|Any CPU
53 | {97B8F460-4AB3-4E6D-99A0-33ADC9E5578A}.Release|Any CPU.ActiveCfg = Release|Any CPU
54 | {97B8F460-4AB3-4E6D-99A0-33ADC9E5578A}.Release|Any CPU.Build.0 = Release|Any CPU
55 | {6E420516-09BB-4378-B65F-E02C37CD66D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
56 | {6E420516-09BB-4378-B65F-E02C37CD66D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
57 | {6E420516-09BB-4378-B65F-E02C37CD66D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
58 | {6E420516-09BB-4378-B65F-E02C37CD66D2}.Release|Any CPU.Build.0 = Release|Any CPU
59 | {67EAE13C-79FF-4E0F-8128-D69322CD2E34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
60 | {67EAE13C-79FF-4E0F-8128-D69322CD2E34}.Debug|Any CPU.Build.0 = Debug|Any CPU
61 | {67EAE13C-79FF-4E0F-8128-D69322CD2E34}.Release|Any CPU.ActiveCfg = Release|Any CPU
62 | {67EAE13C-79FF-4E0F-8128-D69322CD2E34}.Release|Any CPU.Build.0 = Release|Any CPU
63 | {5EADD7CB-210A-4169-A0DF-C4AE7EDBE731}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64 | {5EADD7CB-210A-4169-A0DF-C4AE7EDBE731}.Debug|Any CPU.Build.0 = Debug|Any CPU
65 | {5EADD7CB-210A-4169-A0DF-C4AE7EDBE731}.Release|Any CPU.ActiveCfg = Release|Any CPU
66 | {5EADD7CB-210A-4169-A0DF-C4AE7EDBE731}.Release|Any CPU.Build.0 = Release|Any CPU
67 | {518E0DB7-8C83-41C6-A020-5AF0DFF05F19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
68 | {518E0DB7-8C83-41C6-A020-5AF0DFF05F19}.Debug|Any CPU.Build.0 = Debug|Any CPU
69 | {518E0DB7-8C83-41C6-A020-5AF0DFF05F19}.Release|Any CPU.ActiveCfg = Release|Any CPU
70 | {518E0DB7-8C83-41C6-A020-5AF0DFF05F19}.Release|Any CPU.Build.0 = Release|Any CPU
71 | {9772890F-1F2A-4085-AF2B-384DF3ED45E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
72 | {9772890F-1F2A-4085-AF2B-384DF3ED45E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
73 | {9772890F-1F2A-4085-AF2B-384DF3ED45E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
74 | {9772890F-1F2A-4085-AF2B-384DF3ED45E1}.Release|Any CPU.Build.0 = Release|Any CPU
75 | EndGlobalSection
76 | GlobalSection(SolutionProperties) = preSolution
77 | HideSolutionNode = FALSE
78 | EndGlobalSection
79 | GlobalSection(NestedProjects) = preSolution
80 | {97B8F460-4AB3-4E6D-99A0-33ADC9E5578A} = {C87C815F-D309-4846-933A-45221E6AC4E2}
81 | {6E420516-09BB-4378-B65F-E02C37CD66D2} = {7E905FA2-EB17-4082-92C8-244C3933783F}
82 | {67EAE13C-79FF-4E0F-8128-D69322CD2E34} = {243D0BEA-EA8B-4D7E-B645-BE17B9763896}
83 | {5EADD7CB-210A-4169-A0DF-C4AE7EDBE731} = {243D0BEA-EA8B-4D7E-B645-BE17B9763896}
84 | {518E0DB7-8C83-41C6-A020-5AF0DFF05F19} = {243D0BEA-EA8B-4D7E-B645-BE17B9763896}
85 | {1393ACB8-9707-446C-B6FD-4C2513B43592} = {717509EE-6F82-4DCD-9110-B5EEFFDBA678}
86 | {9772890F-1F2A-4085-AF2B-384DF3ED45E1} = {7E905FA2-EB17-4082-92C8-244C3933783F}
87 | EndGlobalSection
88 | GlobalSection(ExtensibilityGlobals) = postSolution
89 | SolutionGuid = {C58D8048-52B6-44D8-AF85-8759A9ED42FA}
90 | EndGlobalSection
91 | EndGlobal
92 |
--------------------------------------------------------------------------------
/AutoMapper.Contrib.Autofac.DependencyInjection.sln.DotSettings:
--------------------------------------------------------------------------------
1 |
2 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
3 | <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
4 | <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></Policy>
5 | <Policy><Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"><ElementKinds><Kind Name="FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></Policy>
6 | True
--------------------------------------------------------------------------------
/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 | latest
4 | enable
5 | enable
6 | true
7 |
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AutoMapper.Contrib.Autofac.DependencyInjection
2 |
3 | [](https://github.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection/actions/workflows/push.yml)
4 | [](https://codecov.io/gh/alsami/AutoMapper.Contrib.Autofac.DependencyInjection)
5 |
6 | [](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection)
7 | [](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection)
8 |
9 | This is a cross platform library, written in .netstandard 2.0, that serves as an extension for [autofac's containerbuilder](https://autofac.org/).
10 | It will register all necessary classes and interfaces of Jimmy Bogard's [AutoMapper](https://github.com/AutoMapper/AutoMapper) implementation to the autofac-container
11 | so you can use AutoMapper and object-mapping right ahead without worrying about setting up required infrastructure code.
12 |
13 | ## Installation
14 |
15 | This package is available via nuget. You can install it using Visual-Studio-Nuget-Browser or by using the dotnet-cli
16 |
17 | ```
18 | dotnet add package AutoMapper.Contrib.Autofac.DependencyInjection
19 | ```
20 |
21 | If you want to add a specific version of this package
22 |
23 | ```
24 | dotnet add package AutoMapper.Contrib.Autofac.DependencyInjection --version 1.0.0
25 | ```
26 |
27 | For more information please visit the official [dotnet-cli documentation](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-add-package).
28 |
29 | ## Usage sample
30 |
31 | After installing the package you define your entities and dtos and create profiles for them.
32 |
33 | ```csharp
34 | public class Customer
35 | {
36 | public Guid Id { get; }
37 | public string Name { get; }
38 |
39 | public Customer(Guid id, string name)
40 | {
41 | Id = id;
42 | Name = name;
43 | }
44 | }
45 |
46 | public class CustomerDto
47 | {
48 | public Guid Id { get; }
49 | public string Name { get; }
50 |
51 | public CustomerDto(Guid id, string name)
52 | {
53 | Id = id;
54 | Name = name;
55 | }
56 | }
57 |
58 | public class CustomerProfile : Profile
59 | {
60 | public CustomerProfile()
61 | {
62 | CreateMap()
63 | .ConstructUsing(user => new UserDto(user.Id, user.Name))
64 | .ReverseMap()
65 | .ConstructUsing(userDto => new User(userDto.Id, userDto.Name));
66 | }
67 | }
68 |
69 | public static class Program
70 | {
71 | public static void Main(string args[])
72 | {
73 | var containerBuilder = new ContainerBuilder();
74 | // here you have to pass in the assembly (or assemblies) containing AutoMapper types
75 | // stuff like profiles, resolvers and type-converters will be added by this function
76 | containerBuilder.RegisterAutoMapper(typeof(Program).Assembly);
77 |
78 | var container = containerBuilder.Build();
79 |
80 | var mapper = container.Resolve();
81 |
82 | var customer = new Customer(Guid.NewGuid(), "Google");
83 |
84 | var customerDto = mapper.Map(customer);
85 | }
86 | }
87 | ```
88 |
89 | ### Support for Property Injection
90 |
91 | You can set `propertiesAutowired` to true to enable property based injection, just modify the prior example like so:
92 |
93 | ```csharp
94 | public static class Program
95 | {
96 | public static void Main(string args[])
97 | {
98 | var containerBuilder = new ContainerBuilder();
99 |
100 | // Update this line with the setting:
101 | containerBuilder.RegisterAutoMapper(typeof(Program).Assembly, propertiesAutowired: true);
102 |
103 | var container = containerBuilder.Build();
104 |
105 | var mapper = container.Resolve();
106 |
107 | var customer = new Customer(Guid.NewGuid(), "Google");
108 |
109 | var customerDto = mapper.Map(customer);
110 | }
111 | }
112 | ```
113 |
114 | ### Validating your configuration
115 |
116 | `AutoMapper` allows the user to validate their mappings. **This should only be done within a test project, since it's time-consuming**
117 |
118 | ```csharp
119 | var containerBuilder = new ContainerBuilder();
120 | containerBuilder.RegisterAutoMapper(typeof(Program).Assembly);
121 |
122 | var container = containerBuilder.Build();
123 | var mapperConfiguration = container.Resolve();
124 |
125 | // this line will throw when mappings are not working as expected
126 | // it's wise to write a test for that, which is always executed within a CI pipeline for your project.
127 | mapperConfiguration.AssertConfigurationIsValid();
128 | ```
129 |
--------------------------------------------------------------------------------
/changelog.md:
--------------------------------------------------------------------------------
1 | # [9.0.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/9.0.0) (2025-02-20)
2 |
3 | ## Features
4 |
5 | * Update `Autofac` to `8.2.0`
6 | * Update `AutoMapper` to `14.0.0` -> this change is a small breaking change since `AutoMapper` now requires a minimum of `.NET 8.0`
7 |
8 | # [8.1.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/8.1.0) (2024-11-12)
9 |
10 | ## Features
11 |
12 | * Update `Autofac` to `8.1.1`
13 |
14 | # [8.0.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/8.0.0) (2024-02-23)
15 |
16 | ## Breaking changes
17 |
18 | * Update `AutoMapper` to `13.0.1` and target framework to `NET6.0` as required by it now
19 | * Update `Autofac` to `8.0.0`
20 |
21 | # [7.2.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/7.2.0) (2023-09-10)
22 |
23 | ## Features
24 |
25 | * Allow calling `RegisterAutoMapper` multiple times. Note that this is not compatible when using the official integration and this project together!
26 | * Upgrade `Autofac` to `7.1.0`
27 |
28 | # [7.1.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/7.1.0) (2022-11-23)
29 |
30 | ## Features
31 |
32 | * Upgrade `Autofac` to `6.6.0`
33 |
34 | # [7.0.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/7.0.0) (2022-09-30)
35 |
36 | ## Features
37 |
38 | * Upgrade `AutoMapper` to `12.0.0`. For more details, please see the release notes of [AutoMapper](https://github.com/AutoMapper/AutoMapper/releases/tag/v12.0.0).
39 |
40 | # [6.1.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/6.1.0) (2022-05-31)
41 |
42 | ## Features
43 |
44 | * Upgrade `Autofac` to `6.4.0`
45 |
46 | # [6.0.1](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/6.0.1) (2022-02-04)
47 |
48 | ## Chore
49 |
50 | * Update `AutoMapper` to `11.0.1`
51 |
52 | # [6.0.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/6.0.0) (2022-01-09)
53 |
54 | ## Breaking changes
55 |
56 | * `AutoMapper` 11.0.0 dropped support for `netstandard2.0`. No only `netstandard2.1` is supported
57 |
58 | ## Features
59 |
60 | * Add support for `AutoMapper` 11.0.0
61 |
62 | # [5.6.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/5.6.0) (2022-01-05)
63 |
64 | ## Features
65 |
66 | * Add supported for property-injection. Thanks to [matthewrhoden1](https://github.com/matthewrhoden1)!
67 |
68 | # [5.5.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/5.5.0) (2021-11-12)
69 |
70 | ## Features
71 |
72 | * Add support for `Autofac` version `6.3.0`
73 |
74 | # [5.4.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/5.4.0) (2021-04-23)
75 |
76 | ## Features
77 |
78 | * Add support for `Autofac` version `6.2.0`
79 |
80 | # [5.3.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/5.3.0) (2021-03-01)
81 |
82 | ## Features
83 |
84 | * Add support for `IValueConverter<,>`. Implements [issue #5](https://github.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection/issues/5).
85 |
86 | # [5.2.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/5.2.0) (2021-01-01)
87 |
88 | ## Features
89 |
90 | * Use different overload to register `AutoMapper` profiles so that profiles may contain dependencies. [(#4)](https://github.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection/issues/4)
91 | * Upgrade `AutoMapper` to version `10.1.1`
92 | * Update `Autofac` to version `6.1.0`
93 |
94 | # [5.1.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/5.1.0) (2020-10-15)
95 |
96 | ## Fixes
97 |
98 | * Fix obsolete-message for legacy-extensions: [7ec0b2e789ff6b7bb4ae9aa9cdba0e5e801e86a1](https://github.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection/commit/7ec0b2e789ff6b7bb4ae9aa9cdba0e5e801e86a1). Thanks to [benmccallum](https://github.com/benmccallum)!
99 |
100 | ## Features
101 |
102 | * Upgrade `AutoMapper` to version 10.1.0
103 |
104 | # [5.0.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/5.0.0) (2020-09-29)
105 |
106 | ## Breaking Changes
107 |
108 | * `Autofac` has been updated to version `6.0.0`. This release contains many new features but also breaking-changes. Check out this [blog-post](https://alistairevans.co.uk/2020/09/28/autofac-6-0-released/) for more information.
109 |
110 | ## Deprecation notice
111 |
112 | * `AddAutoMapper` has been marked as deprecated and will be removed with version `6.0.0`.
113 |
114 | ## Features
115 |
116 | * New extensions were added that are more aligned with the `Autofac` syntax for registering dependencies. Please use `RegisterAutoMapper` instead of `AddAutoMapper`
117 |
118 |
119 | # [4.0.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/4.0.0) (2020-07-15)
120 |
121 | ## Features
122 |
123 | * Update `AutoMapper` to version `10.0.0`
124 |
125 | # [3.2.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/3.2.0) (2020-06-11)
126 |
127 | ## Features
128 |
129 | * Update `Autofac` to version `5.2.0`
130 |
131 | # [3.1.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/3.1.0) (2020-02-22)
132 |
133 | ## Features
134 |
135 | * Update `Autofac` to version `5.1.1`
136 | * Expose `MapperConfiguration` as `IConfigurationProvider` as well
137 |
138 | ## Chore
139 |
140 | * Optimize registration process and make sure that `AutoMapper` assemblies are excluded when scanning for types
141 |
142 | # [3.0.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/3.0.0) (2020-01-29)
143 |
144 | ## Breaking changes
145 |
146 | * `Autofac` has been updated to `5.0.0`. The release of `Autofac` contains breaking changes, mostly making the container immutable. You can read more about the changes [here](https://www.paraesthesia.com/archive/2020/01/27/autofac-5-released/).
147 |
148 | # [2.0.1](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/2.0.1) (2019-12-26)
149 |
150 | ## Chore
151 |
152 | * Update `Autofac` to version `4.9.4`
153 |
154 | # [2.0.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/2.0.0) (2019-08-12)
155 |
156 | ## Breaking changes
157 |
158 | * `AutoMapper` has completely removed all static-apis. If you still rely on any of them, please check out this [migration-guide](https://docs.automapper.org/en/stable/9.0-Upgrade-Guide.html).
159 |
160 | ## Chore
161 |
162 | * Update `AutoMapper` to version `9.0.0`
163 |
164 | # [1.0.1](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/1.0.0) (2019-06-13)
165 |
166 | ## Chore
167 |
168 | * Update `AutoMapper` to version `8.1.1`
169 |
170 | # [1.0.0](https://www.nuget.org/packages/AutoMapper.Contrib.Autofac.DependencyInjection/1.0.0) (2019-05-16)
171 |
172 | ## Intial Release
173 |
174 | * Allow `AutoMapper` and it's components to be registered via an extension method for Autofac.
175 |
176 | ## Additional Note
177 |
178 | This package is the same as the latest version from CleanCodeLabs.AutoMapper.Extensions.Autofac.DependencyInjection. It has been moved to this repository and will be continued to be mantained here.
--------------------------------------------------------------------------------
/global.json:
--------------------------------------------------------------------------------
1 | {
2 | "sdk": {
3 | "version": "9.0.103",
4 | "rollForward": "latestFeature"
5 | }
6 | }
--------------------------------------------------------------------------------
/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection/8757d6ca0450e58242caf30c4c13fe020769cdad/icon.png
--------------------------------------------------------------------------------
/licence.md:
--------------------------------------------------------------------------------
1 | Copyright 2019©CleanCode-Labs
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 |
--------------------------------------------------------------------------------
/publish-packages.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | cd src
3 | for directory in *; do
4 | if [[ -d ${directory} ]]; then
5 | cd ${directory}
6 | dotnet pack *.csproj --include-symbols -c Release --output "."
7 | sleep 5
8 | dotnet nuget push -s ${NUGET_API_URL} -k ${NUGET_KEY} "${directory}.${TAG}.symbols.nupkg"
9 | if [[ ${?} != 0 ]]
10 | then
11 | exit -1
12 | fi
13 | cd ..
14 | fi
15 | done
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.ConsoleApp/AutoMapper.Autofac.ConsoleApp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net9.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.ConsoleApp/Program.cs:
--------------------------------------------------------------------------------
1 | using Autofac;
2 | using AutoMapper.Autofac.Shared.Dtos;
3 | using AutoMapper.Autofac.Shared.Entities;
4 | using AutoMapper.Contrib.Autofac.DependencyInjection;
5 |
6 | namespace AutoMapper.Autofac.ConsoleApp;
7 |
8 | internal class Program
9 | {
10 | private static readonly IReadOnlyCollection Customers = new List
11 | {
12 | new Customer(Guid.NewGuid(), "Google"),
13 | new Customer(Guid.NewGuid(), "Facebook")
14 | };
15 |
16 | private static void Main(string[] args)
17 | {
18 | var containerBuilder = new ContainerBuilder();
19 |
20 | containerBuilder.RegisterAutoMapper(typeof(Customer).Assembly);
21 |
22 | var container = containerBuilder.Build();
23 |
24 | var mapper = container.Resolve();
25 |
26 | var customerDtos = mapper.Map>(Customers);
27 |
28 | foreach (var customer in customerDtos)
29 | Console.WriteLine(customer);
30 | }
31 | }
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.Shared/AutoMapper.Autofac.Shared.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net9.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.Shared/Converters/CustomerConverter.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper.Autofac.Shared.Dtos;
2 | using AutoMapper.Autofac.Shared.Entities;
3 |
4 | namespace AutoMapper.Autofac.Shared.Converters;
5 |
6 | // ReSharper disable once ClassNeverInstantiated.Global
7 | public class CustomerConverter : ITypeConverter
8 | {
9 | public CustomerDto Convert(Customer source, CustomerDto destination, ResolutionContext context)
10 | {
11 | return new CustomerDto(source.Id, source.Name);
12 | }
13 | }
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.Shared/Dtos/CustomerDto.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Autofac.Shared.Dtos;
2 |
3 | public class CustomerDto
4 | {
5 | public CustomerDto(Guid id, string name)
6 | {
7 | this.Id = id;
8 | this.Name = name;
9 | }
10 |
11 | public Guid Id { get; }
12 | public string Name { get; }
13 |
14 | public override string ToString()
15 | {
16 | return $"{this.Id} - {this.Name}";
17 | }
18 | }
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.Shared/Entities/Customer.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Autofac.Shared.Entities;
2 |
3 | public class Customer
4 | {
5 | public Customer(Guid id, string name)
6 | {
7 | this.Id = id;
8 | this.Name = name;
9 | }
10 |
11 | public Guid Id { get; }
12 | public string Name { get; }
13 | }
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.Shared/Profiles/CustomerProfile.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper.Autofac.Shared.Converters;
2 | using AutoMapper.Autofac.Shared.Dtos;
3 | using AutoMapper.Autofac.Shared.Entities;
4 |
5 | namespace AutoMapper.Autofac.Shared.Profiles;
6 |
7 | public class CustomerProfile : Profile
8 | {
9 | public CustomerProfile()
10 | {
11 | this.CreateMap()
12 | .ConvertUsing();
13 | }
14 | }
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.WebApi/AutoMapper.Autofac.WebApi.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net9.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.WebApi/Controllers/CustomersController.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper.Autofac.Shared.Dtos;
2 | using AutoMapper.Autofac.Shared.Entities;
3 | using Microsoft.AspNetCore.Mvc;
4 |
5 | namespace AutoMapper.Autofac.WebApi.Controllers;
6 |
7 | [ApiController]
8 | [Route("customers")]
9 | public class CustomersController : ControllerBase
10 | {
11 | private static readonly IReadOnlyCollection Customers = new List
12 | {
13 | new Customer(Guid.NewGuid(), "Google"),
14 | new Customer(Guid.NewGuid(), "Facebook")
15 | };
16 |
17 | private readonly IMapper mapper;
18 |
19 | public CustomersController(IMapper mapper)
20 | {
21 | this.mapper = mapper;
22 | }
23 |
24 | [HttpGet]
25 | public IEnumerable Load()
26 | {
27 | return this.mapper.Map>(Customers);
28 | }
29 | }
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.WebApi/Program.cs:
--------------------------------------------------------------------------------
1 | using Autofac.Extensions.DependencyInjection;
2 |
3 | namespace AutoMapper.Autofac.WebApi;
4 |
5 | // ReSharper disable once ClassNeverInstantiated.Global
6 | public class Program
7 | {
8 | public static async Task Main(string[] args)
9 | {
10 | using var host = CreateHost(args);
11 |
12 | await host.RunAsync();
13 | }
14 |
15 | private static IHost CreateHost(string[] args)
16 | {
17 | return Host.CreateDefaultBuilder(args)
18 | .UseServiceProviderFactory(new AutofacServiceProviderFactory())
19 | .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup())
20 | .Build();
21 | }
22 | }
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.WebApi/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json.schemastore.org/launchsettings.json",
3 | "profiles": {
4 | "AutoMapper.Autofac.WebApi": {
5 | "commandName": "Project",
6 | "launchBrowser": true,
7 | "launchUrl": "customers",
8 | "applicationUrl": "http://localhost:5000",
9 | "environmentVariables": {
10 | "ASPNETCORE_ENVIRONMENT": "Development"
11 | }
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/samples/AutoMapper.Autofac.WebApi/Startup.cs:
--------------------------------------------------------------------------------
1 | using Autofac;
2 | using AutoMapper.Autofac.Shared.Entities;
3 | using AutoMapper.Contrib.Autofac.DependencyInjection;
4 |
5 | namespace AutoMapper.Autofac.WebApi;
6 |
7 | public class Startup
8 | {
9 | public void ConfigureServices(IServiceCollection services)
10 | {
11 | services.AddControllers()
12 | .AddNewtonsoftJson();
13 | }
14 |
15 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
16 | {
17 | if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
18 |
19 | app.UseHttpsRedirection();
20 |
21 | app.UseRouting();
22 |
23 | app.UseEndpoints(endpoints => endpoints.MapControllers());
24 | }
25 |
26 | public void ConfigureContainer(ContainerBuilder builder)
27 | {
28 | builder.RegisterAutoMapper(typeof(Customer).Assembly);
29 | }
30 | }
--------------------------------------------------------------------------------
/src/AutoMapper.Contrib.Autofac.DependencyInjection/AutoMapper.Contrib.Autofac.DependencyInjection.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net8.0
4 | 9.0.0
5 | 9.0.0.0
6 | 2025©Sami Al Khatib
7 | Sami Al Khatib
8 | AutoMapper-Autofac-Extension
9 | An extension for Autofac-IoC to use AutoMapper with ease.
10 | MIT
11 | https://github.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection.git
12 | https://github.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection/blob/main/changelog.md
13 | AutoMapper, Object-To-Object-Mapping, Mapper, Autofac, IoC, Dependency-Injection
14 | icon.png
15 | https://s3.amazonaws.com/automapper/icon.png
16 | git
17 | git://github.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection.git
18 | true
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | all
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/AutoMapper.Contrib.Autofac.DependencyInjection/AutoMapperModule.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using Autofac;
3 | using Module = Autofac.Module;
4 |
5 | namespace AutoMapper.Contrib.Autofac.DependencyInjection;
6 |
7 | internal class AutoMapperModule : Module
8 | {
9 | private readonly Assembly[] assembliesToScan;
10 | private readonly Action mappingConfigurationAction;
11 | private readonly bool propertiesAutowired;
12 |
13 | private static readonly MapperConfigurationExpression MapperConfigurationExpression = new();
14 |
15 | public AutoMapperModule(
16 | Assembly[] assembliesToScan,
17 | Action mappingConfigurationAction,
18 | bool propertiesAutowired)
19 | {
20 | this.assembliesToScan = assembliesToScan ?? throw new ArgumentNullException(nameof(assembliesToScan));
21 | this.mappingConfigurationAction = mappingConfigurationAction ??
22 | throw new ArgumentNullException(nameof(mappingConfigurationAction));
23 | this.propertiesAutowired = propertiesAutowired;
24 | }
25 |
26 | protected override void Load(ContainerBuilder builder)
27 | {
28 | var distinctAssemblies = this.assembliesToScan
29 | .Where(a => !a.IsDynamic && a.GetName().Name != nameof(AutoMapper))
30 | .Distinct()
31 | .ToArray();
32 |
33 | var profiles = builder
34 | .RegisterAssemblyTypes(distinctAssemblies)
35 | .AssignableTo(typeof(Profile))
36 | .As()
37 | .SingleInstance();
38 |
39 | if (propertiesAutowired)
40 | {
41 | profiles.PropertiesAutowired();
42 | }
43 |
44 | builder
45 | .RegisterType()
46 | .AsSelf()
47 | .IfNotRegistered(typeof(MapperConfigurationExpression))
48 | .SingleInstance();
49 |
50 | builder
51 | .Register(componentContext => new MapperConfiguration(componentContext.Resolve()))
52 | .AsSelf()
53 | .As()
54 | .IfNotRegistered(typeof(MapperConfigurationExpression))
55 | .SingleInstance();
56 |
57 | builder
58 | .Register(componentContext =>
59 | {
60 | var expression = componentContext.Resolve();
61 | this.ConfigurationAction(expression, componentContext);
62 | return new MapperConfigurationExpressionAdapter(expression);
63 | })
64 | .AsSelf()
65 | .InstancePerDependency();
66 |
67 |
68 | builder
69 | .Register(componentContext =>
70 | {
71 | var adapter = componentContext.Resolve();
72 |
73 | return new MapperConfiguration(adapter.MapperConfigurationExpression);
74 | })
75 | .As()
76 | .AsSelf()
77 | .IfNotRegistered(typeof(IConfigurationProvider))
78 | .SingleInstance();
79 |
80 | var openTypes = new[]
81 | {
82 | typeof(IValueResolver<,,>),
83 | typeof(IValueConverter<,>),
84 | typeof(IMemberValueResolver<,,,>),
85 | typeof(ITypeConverter<,>),
86 | typeof(IMappingAction<,>)
87 | };
88 |
89 | foreach (var openType in openTypes)
90 | {
91 | var openTypeBuilder = builder.RegisterAssemblyTypes(distinctAssemblies)
92 | .AsClosedTypesOf(openType)
93 | .AsImplementedInterfaces()
94 | .InstancePerDependency();
95 |
96 | if (propertiesAutowired)
97 | {
98 | openTypeBuilder.PropertiesAutowired();
99 | }
100 | }
101 |
102 | builder
103 | .Register(componentContext => componentContext
104 | .Resolve()
105 | .CreateMapper(componentContext.Resolve().Resolve))
106 | .As()
107 | .IfNotRegistered(typeof(IMapper))
108 | .InstancePerLifetimeScope();
109 | }
110 |
111 | private void ConfigurationAction(IMapperConfigurationExpression cfg, IComponentContext componentContext)
112 | {
113 | this.mappingConfigurationAction.Invoke(cfg);
114 |
115 | var profiles = componentContext.Resolve>();
116 |
117 | foreach (var profile in profiles)
118 | {
119 | cfg.AddProfile(profile);
120 | }
121 | }
122 | }
--------------------------------------------------------------------------------
/src/AutoMapper.Contrib.Autofac.DependencyInjection/ContainerBuilderExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using Autofac;
3 |
4 | namespace AutoMapper.Contrib.Autofac.DependencyInjection;
5 |
6 | public static class ContainerBuilderExtensions
7 | {
8 | private static readonly Action FallBackExpression =
9 | _ => { };
10 |
11 | // ReSharper disable once UnusedMember.Global
12 | public static ContainerBuilder RegisterAutoMapper(
13 | this ContainerBuilder builder,
14 | bool propertiesAutowired = false,
15 | params Assembly[] assemblies)
16 | {
17 | return RegisterAutoMapperInternal(builder, assemblies, propertiesAutowired: propertiesAutowired);
18 | }
19 |
20 | public static ContainerBuilder RegisterAutoMapper(
21 | this ContainerBuilder builder,
22 | Assembly assembly,
23 | bool propertiesAutowired = false)
24 | {
25 | return RegisterAutoMapperInternal(builder, new[] {assembly}, propertiesAutowired: propertiesAutowired);
26 | }
27 |
28 | // ReSharper disable once UnusedMember.Global
29 | public static ContainerBuilder RegisterAutoMapper(
30 | this ContainerBuilder builder,
31 | Action configExpression,
32 | bool propertiesAutowired = false,
33 | params Assembly[] assemblies)
34 | {
35 | return RegisterAutoMapperInternal(builder, assemblies, configExpression, propertiesAutowired);
36 | }
37 |
38 | // ReSharper disable once UnusedMember.Global
39 | public static ContainerBuilder RegisterAutoMapper(
40 | this ContainerBuilder builder, Action configExpression,
41 | Assembly assembly,
42 | bool propertiesAutowired = false)
43 | {
44 | return RegisterAutoMapperInternal(builder, new[] {assembly}, configExpression, propertiesAutowired);
45 | }
46 |
47 | private static ContainerBuilder RegisterAutoMapperInternal(ContainerBuilder builder,
48 | IEnumerable assemblies, Action? configExpression = null, bool propertiesAutowired = false)
49 | {
50 | var usedAssemblies = assemblies as Assembly[] ?? assemblies.ToArray();
51 |
52 | var usedConfigExpression = configExpression ?? FallBackExpression;
53 |
54 | builder.RegisterModule(new AutoMapperModule(usedAssemblies, usedConfigExpression, propertiesAutowired));
55 |
56 | return builder;
57 | }
58 | }
--------------------------------------------------------------------------------
/src/AutoMapper.Contrib.Autofac.DependencyInjection/MapperConfigurationExpressionAdapter.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection;
2 |
3 | internal class MapperConfigurationExpressionAdapter
4 | {
5 | public MapperConfigurationExpression MapperConfigurationExpression { get; }
6 |
7 | public MapperConfigurationExpressionAdapter(MapperConfigurationExpression mapperConfigurationExpression)
8 | {
9 | this.MapperConfigurationExpression = mapperConfigurationExpression;
10 | }
11 | }
--------------------------------------------------------------------------------
/test.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | function run_tests() {
3 | dotnet test -c Release -p:CoverletOutput="../../coverage/" -p:MergeWith="../../coverage/coverage.json" -p:CollectCoverage=true -p:CoverletOutputFormat=\"json,lcov,opencover\" -p:ThresholdType=line -p:ThresholdStat=total -p:ExcludeFromCoverage="test/IdentitiyServer4.Api" | tee testoutput.txt
4 | return ${?}
5 | }
6 |
7 | function publish_coverage() {
8 | curl https://codecov.io/bash -o codecov.sh
9 | chmod +x codecov.sh
10 | ./codecov.sh
11 | return ${?}
12 | }
13 |
14 | run_tests
15 | publish_coverage
16 | exit ${?}
17 |
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly/AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net9.0
5 | enable
6 | enable
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly/ObjectDestination.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly;
2 |
3 | public class ObjectDestination
4 | {
5 | public string? Id { get; set; }
6 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly/ObjectSource.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly;
2 |
3 | public class ObjectSource
4 | {
5 | public string? Id { get; set; }
6 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly/SecondAssemblyProfile.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly;
2 |
3 | public class SecondAssemblyProfile : Profile
4 | {
5 | public SecondAssemblyProfile()
6 | {
7 | this.CreateMap().ReverseMap();
8 | }
9 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/AutoMapper.Contrib.Autofac.DependencyInjection.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net9.0
4 | false
5 | true
6 |
7 |
8 |
9 |
10 |
11 | all
12 | runtime; build; native; contentfiles; analyzers; buildtransitive
13 |
14 |
15 | runtime; build; native; contentfiles; analyzers; buildtransitive
16 | all
17 |
18 |
19 | all
20 | runtime; build; native; contentfiles; analyzers; buildtransitive
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/ContainerBuilderExtensionTests.cs:
--------------------------------------------------------------------------------
1 | using Autofac;
2 | using AutoMapper.Contrib.Autofac.DependencyInjection.SecondAssembly;
3 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Dtos;
4 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Entities;
5 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Profiles;
6 | using Xunit;
7 |
8 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests;
9 |
10 | public class ContainerBuilderExtensionTests
11 | {
12 | [Fact]
13 | public void
14 | ContainerBuilderExtensions_AddAutoMapper_Convert_Customer_To_CustomerAttributeDto_Expect_Values_To_Be_Equal()
15 | {
16 | var customer = new Customer(Guid.NewGuid(), "google", "google1");
17 |
18 | var builder = new ContainerBuilder()
19 | .RegisterAutoMapper(typeof(Customer).Assembly);
20 |
21 | builder.RegisterType()
22 | .AsSelf();
23 |
24 | var container = builder.Build();
25 |
26 | var mapper = container.Resolve();
27 |
28 | var customerDto = mapper.Map(customer);
29 | var reverseMappedCustomer = mapper.Map(customerDto);
30 |
31 | Assert.Equal(customer.Id, reverseMappedCustomer.Id);
32 | Assert.Equal(customer.FirstName, reverseMappedCustomer.FirstName);
33 | Assert.Equal(customer.Name, reverseMappedCustomer.Name);
34 |
35 | Assert.Equal(customer.Id, customerDto.Id);
36 | Assert.Equal(customer.FirstName, customerDto.FirstName);
37 | Assert.Equal(customer.Name, customerDto.Name);
38 | Assert.Equal($"{customer.FirstName} {customer.Name}", customerDto.FullName);
39 | }
40 |
41 | [Fact]
42 | public void ContainerBuilderExtensions_AddAutoMapperAssembliesOnly_ExpectTypesToBeRegistered()
43 | {
44 | var builder = new ContainerBuilder()
45 | .RegisterAutoMapper(typeof(Customer).Assembly);
46 |
47 | builder.RegisterType()
48 | .AsSelf();
49 |
50 | var container = builder.Build();
51 |
52 | Assert.True(container.IsRegistered>());
53 | Assert.True(container.IsRegistered());
54 | Assert.True(container.IsRegistered());
55 | Assert.True(container.IsRegistered());
56 | Assert.True(container.IsRegistered>());
57 | Assert.True(container.IsRegistered>());
58 | Assert.True(container.IsRegistered>());
59 |
60 | var profiles = container.Resolve>();
61 | var resolver = container.Resolve>();
62 | var converter = container.Resolve>();
63 | var mapper = container.Resolve();
64 |
65 | Assert.Equal(3, profiles.Count());
66 | Assert.NotNull(resolver);
67 | Assert.NotNull(converter);
68 | Assert.NotNull(mapper);
69 | }
70 |
71 | [Fact]
72 | public void ContainerBuilderExtension_PropertiesAutoWiredNotSet_ExpectExceptions()
73 | {
74 | // Arrange...
75 | var name = new Name() { FirstName = "Maximilian", LastName = "Smith" };
76 |
77 | var builder = new ContainerBuilder()
78 | .RegisterAutoMapper(typeof(Name).Assembly);
79 |
80 | builder.RegisterType()
81 | .AsSelf();
82 |
83 | // Simulate loading from appsettings.json:
84 | builder.RegisterInstance(new TestConfiguration() { FirstNameCharacterLimit = 12 }).AsSelf();
85 |
86 | var container = builder.Build();
87 |
88 | var mapper = container.Resolve();
89 |
90 | // Act...
91 | var ex = Record.Exception(() => mapper.Map(name));
92 |
93 | // Assert...
94 | Assert.NotNull(ex);
95 | Assert.Equal(typeof(AutoMapperMappingException), ex.GetType());
96 | }
97 |
98 | [Fact]
99 | public void ContainerBuilderExtension_PropertiesAutoWiredSet_NoExceptionsExpected()
100 | {
101 | // Arrange...
102 | var name = new Name() { FirstName = "Maximilian", LastName = "Smith" };
103 |
104 | var builder = new ContainerBuilder()
105 | .RegisterAutoMapper(typeof(Name).Assembly, propertiesAutowired: true);
106 |
107 | builder.RegisterType()
108 | .AsSelf();
109 |
110 | // Simulate loading from appsettings.json:
111 | builder.RegisterInstance(new TestConfiguration() { FirstNameCharacterLimit = 12 }).AsSelf();
112 |
113 | var container = builder.Build();
114 |
115 | var mapper = container.Resolve();
116 |
117 | // Act...
118 | var exception = Record.Exception(() => mapper.Map(name));
119 |
120 | // Assert...
121 | Assert.Null(exception);
122 | }
123 |
124 | [Fact]
125 | public void ContainerBuilderExtension_PropertiesAutowired_CustomValueResolverWithPropertyDependencyIsResolved()
126 | {
127 | // Arrange...
128 | var name = new Name() { FirstName = "Maximilian", LastName = "Smith" };
129 |
130 | var builder = new ContainerBuilder()
131 | .RegisterAutoMapper(typeof(Name).Assembly, propertiesAutowired: true);
132 |
133 | builder.RegisterType()
134 | .AsSelf();
135 |
136 | // Simulate loading from appsettings.json:
137 | var configuration = new TestConfiguration() { FirstNameCharacterLimit = 12 };
138 | builder.RegisterInstance(configuration).AsSelf();
139 |
140 | var container = builder.Build();
141 |
142 | var mapper = container.Resolve();
143 |
144 | // Act...
145 | var result = mapper.Map(name);
146 |
147 | // Assert...
148 | Assert.True(result.IsValidFirstName);
149 | Assert.True(result.FirstName.Length < configuration.FirstNameCharacterLimit);
150 | }
151 |
152 | [Fact]
153 | public void ContainerBuilderExtensions_CallRegisterAutoMapperTwice_OnlyNewStuffAddedOnSecondCall()
154 | {
155 | var builder = new ContainerBuilder()
156 | .RegisterAutoMapper(typeof(Customer).Assembly)
157 | .RegisterAutoMapper(typeof(SecondAssemblyProfile).Assembly);
158 |
159 | builder.RegisterType()
160 | .AsSelf();
161 |
162 | var container = builder.Build();
163 |
164 | Assert.True(container.IsRegistered>());
165 | Assert.True(container.IsRegistered());
166 | Assert.True(container.IsRegistered());
167 | Assert.True(container.IsRegistered());
168 | Assert.True(container.IsRegistered>());
169 | Assert.True(container.IsRegistered>());
170 | Assert.True(container.IsRegistered>());
171 |
172 | var profiles = container.Resolve>();
173 | var resolver = container.Resolve>();
174 | var converter = container.Resolve>();
175 | var mappers = container.Resolve>().ToArray();
176 | var configurationProvider = container.Resolve>().ToArray();
177 | var mapperConfigurationExpressions = container.Resolve>().ToArray();
178 |
179 | Assert.Single(mappers);
180 | Assert.Single(configurationProvider);
181 | Assert.Single(mapperConfigurationExpressions);
182 | Assert.Equal(4, profiles.Count());
183 | Assert.NotNull(resolver);
184 | Assert.NotNull(converter);
185 |
186 | var mapper = mappers.Single();
187 | var src = new ObjectSource { Id = "1" };
188 | var dest = mapper.Map(src);
189 | Assert.Equal(src.Id, dest.Id);
190 |
191 | var customer = new Customer(Guid.NewGuid(), "google", "google1");
192 | var customerDto = mapper.Map(customer);
193 | var reverseMappedCustomer = mapper.Map(customerDto);
194 | Assert.Equal(customer.Id, reverseMappedCustomer.Id);
195 | Assert.Equal(customer.FirstName, reverseMappedCustomer.FirstName);
196 | Assert.Equal(customer.Name, reverseMappedCustomer.Name);
197 |
198 | Assert.Equal(customer.Id, customerDto.Id);
199 | Assert.Equal(customer.FirstName, customerDto.FirstName);
200 | Assert.Equal(customer.Name, customerDto.Name);
201 | Assert.Equal($"{customer.FirstName} {customer.Name}", customerDto.FullName);
202 | }
203 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Converter/NoopStringValueConverter.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Converter;
2 |
3 | public class NoopStringValueConverter : IValueConverter
4 | {
5 | public string Convert(string sourceMember, ResolutionContext context) => sourceMember;
6 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Dtos/CustomerAttributeDto.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper.Configuration.Annotations;
2 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Entities;
3 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Resolver;
4 |
5 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Dtos;
6 |
7 | [AutoMap(typeof(Customer))]
8 | public class CustomerAttributeDto
9 | {
10 | public Guid Id { get; set; }
11 |
12 | public string Name { get; set; } = null!;
13 |
14 | public string FirstName { get; set; } = null!;
15 |
16 | [ValueResolver(typeof(CustomerFullNameResolver))]
17 | public string FullName { get; set; } = null!;
18 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Dtos/CustomerDto.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Dtos;
2 |
3 | public class CustomerDto
4 | {
5 | public Guid Id { get; set; }
6 |
7 | public string Name { get; set; } = null!;
8 |
9 | public string FirstName { get; set; } = null!;
10 |
11 | public string FullName { get; set; } = null!;
12 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Dtos/NameDto.cs:
--------------------------------------------------------------------------------
1 |
2 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Dtos;
3 |
4 | public class NameDto
5 | {
6 | public string FirstName { get; set; } = null!;
7 |
8 | public string LastName { get; set; } = null!;
9 |
10 | public bool IsValidFirstName { get; set; }
11 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Entities/Customer.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Entities;
2 |
3 | public class Customer
4 | {
5 | public Customer(Guid id, string name, string firstName)
6 | {
7 | this.Id = id;
8 | this.Name = name;
9 | this.FirstName = firstName;
10 | }
11 |
12 | public Guid Id { get; }
13 |
14 | public string Name { get; }
15 |
16 | public string FirstName { get; }
17 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Entities/Name.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Entities;
2 |
3 | public class Name
4 | {
5 | public string? FirstName { get; set; }
6 |
7 | public string LastName { get; set; } = null!;
8 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Profiles/CustomerProfile.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Converter;
2 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Dtos;
3 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Entities;
4 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Resolver;
5 |
6 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Profiles;
7 |
8 | public class CustomerProfile : Profile
9 | {
10 | public CustomerProfile()
11 | {
12 | this.CreateMap()
13 | .ForMember(destination => destination.FullName,
14 | options => options.MapFrom())
15 | .ForMember(destination => destination.FullName, options => options.ConvertUsing(source => $"{source.FirstName} {source.Name}"))
16 | .ReverseMap()
17 | .ConvertUsing();
18 | }
19 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Profiles/Dependency.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Profiles;
2 |
3 | public class Dependency
4 | {
5 |
6 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Profiles/NameProfile.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Dtos;
2 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Entities;
3 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Resolver;
4 |
5 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Profiles;
6 |
7 | public class NameProfile : Profile
8 | {
9 | public NameProfile()
10 | {
11 | CreateMap()
12 | .ForMember(destination => destination.IsValidFirstName, options => options.MapFrom());
13 | }
14 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Profiles/ProfileWithDependency.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Profiles;
2 |
3 | // ReSharper disable once UnusedType.Global
4 | public class ProfileWithDependency : Profile
5 | {
6 | // ReSharper disable once NotAccessedField.Local
7 | private readonly Dependency dependency;
8 |
9 | public ProfileWithDependency(Dependency dependency)
10 | {
11 | this.dependency = dependency;
12 | }
13 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Resolver/CustomerFullNameResolver.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Dtos;
2 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Entities;
3 |
4 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Resolver;
5 |
6 | public class CustomerFullNameResolver : IValueResolver
7 | {
8 | public string Resolve(Customer source, CustomerDto destination, string destMember, ResolutionContext context)
9 | {
10 | return $"{source.FirstName} {source.Name}";
11 | }
12 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Resolver/CustomerTypeConverter.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Dtos;
2 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Entities;
3 |
4 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Resolver;
5 |
6 | // ReSharper disable once ClassNeverInstantiated.Global
7 | public class CustomerTypeConverter : ITypeConverter
8 | {
9 | public Customer Convert(CustomerDto source, Customer destination, ResolutionContext context)
10 | {
11 | return new Customer(source.Id, source.Name, source.FirstName);
12 | }
13 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/Resolver/IsFirstNameValidResolver.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Dtos;
2 | using AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Entities;
3 |
4 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests.Resolver;
5 |
6 | public class IsFirstNameValidResolver : IValueResolver
7 | {
8 | // ReSharper disable once MemberCanBePrivate.Global
9 | public TestConfiguration Configuration { get; set; } = null!;
10 |
11 | public bool Resolve(Name source, NameDto destination, bool destMember, ResolutionContext context)
12 | {
13 | return source.FirstName != null && source.FirstName.Length <= Configuration.FirstNameCharacterLimit;
14 | }
15 | }
--------------------------------------------------------------------------------
/test/AutoMapper.Contrib.Autofac.DependencyInjection.Tests/TestConfiguration.cs:
--------------------------------------------------------------------------------
1 | namespace AutoMapper.Contrib.Autofac.DependencyInjection.Tests;
2 |
3 | public class TestConfiguration
4 | {
5 | public int FirstNameCharacterLimit { get; set; }
6 | }
--------------------------------------------------------------------------------