Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Commit 191acb9

Browse files
authored
Merge pull request #38 from AutoMapper/ExplicitConfig
Exposes config option to not register statically
2 parents b52a831 + 714492a commit 191acb9

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

src/AutoMapper.Extensions.Microsoft.DependencyInjection/AutoMapper.Extensions.Microsoft.DependencyInjection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Description>AutoMapper extensions for ASP.NET Core</Description>
55
<Copyright>Copyright Jimmy Bogard</Copyright>
6-
<VersionPrefix>3.1.0</VersionPrefix>
6+
<VersionPrefix>3.2.0</VersionPrefix>
77
<Authors>Jimmy Bogard</Authors>
88
<TargetFrameworks>netstandard2.0</TargetFrameworks>
99
<AssemblyName>AutoMapper.Extensions.Microsoft.DependencyInjection</AssemblyName>

src/AutoMapper.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414
/// - Registers <see cref="Mapper.Configuration"/> as <see cref="ServiceLifetime.Singleton"/>
1515
/// - Registers <see cref="IMapper"/> as <see cref="ServiceLifetime.Scoped"/> with a service factory of the scoped <see cref="IServiceProvider"/>
1616
/// After calling AddAutoMapper you will have the static <see cref="Mapper"/> configuration initialized and you can use Mapper.Map and ProjectTo in your application code.
17+
/// To use instance-based registration instead of the static <see cref="Mapper"/> class, set the <see cref="UseStaticRegistration"/> to false.
1718
/// </summary>
1819
public static class ServiceCollectionExtensions
1920
{
21+
/// <summary>
22+
/// Use the static registration method of Mapper.Initialize. Defaults to true.
23+
/// When false, an instance of a MapperConfiguration object is registered instead.
24+
/// </summary>
25+
public static bool UseStaticRegistration { get; set; } = true;
26+
2027
public static IServiceCollection AddAutoMapper(this IServiceCollection services)
2128
{
2229
return services.AddAutoMapper(null, AppDomain.CurrentDomain.GetAssemblies());
@@ -73,17 +80,30 @@ private static IServiceCollection AddAutoMapperClasses(IServiceCollection servic
7380
.ToArray();
7481

7582
var profiles = allTypes
76-
.Where(t => typeof(Profile).GetTypeInfo().IsAssignableFrom(t) && !t.IsAbstract);
83+
.Where(t => typeof(Profile).GetTypeInfo().IsAssignableFrom(t) && !t.IsAbstract)
84+
.ToArray();
85+
7786

78-
Mapper.Initialize(cfg =>
87+
void ConfigAction(IMapperConfigurationExpression cfg)
7988
{
8089
additionalInitAction(cfg);
8190

8291
foreach (var profile in profiles.Select(t => t.AsType()))
8392
{
8493
cfg.AddProfile(profile);
8594
}
86-
});
95+
}
96+
97+
IConfigurationProvider config;
98+
if (UseStaticRegistration)
99+
{
100+
Mapper.Initialize(ConfigAction);
101+
config = Mapper.Configuration;
102+
}
103+
else
104+
{
105+
config = new MapperConfiguration(ConfigAction);
106+
}
87107

88108
var openTypes = new[]
89109
{
@@ -100,7 +120,7 @@ private static IServiceCollection AddAutoMapperClasses(IServiceCollection servic
100120
services.AddTransient(type.AsType());
101121
}
102122

103-
services.AddSingleton(Mapper.Configuration);
123+
services.AddSingleton(config);
104124
return services.AddScoped<IMapper>(sp => new Mapper(sp.GetRequiredService<IConfigurationProvider>(), sp.GetService));
105125
}
106126

test/AutoMapper.Extensions.Microsoft.DependencyInjection.Tests/AppDomainResolutionTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class AppDomainResolutionTests
1414

1515
public AppDomainResolutionTests()
1616
{
17+
ServiceCollectionExtensions.UseStaticRegistration = true;
1718
IServiceCollection services = new ServiceCollection();
1819
services.AddAutoMapper();
1920
_provider = services.BuildServiceProvider();

test/AutoMapper.Extensions.Microsoft.DependencyInjection.Tests/AssemblyResolutionTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class AssemblyResolutionTests
1414

1515
public AssemblyResolutionTests()
1616
{
17+
ServiceCollectionExtensions.UseStaticRegistration = true;
18+
1719
IServiceCollection services = new ServiceCollection();
1820
services.AddAutoMapper(typeof(Source).GetTypeInfo().Assembly);
1921
_provider = services.BuildServiceProvider();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Shouldly;
4+
using Xunit;
5+
6+
namespace AutoMapper.Extensions.Microsoft.DependencyInjection.Tests
7+
{
8+
[Collection(nameof(RegistrationTests))]
9+
public class RegistrationTests
10+
{
11+
[Fact]
12+
public void Should_not_register_static_instance_when_configured()
13+
{
14+
ServiceCollectionExtensions.UseStaticRegistration = false;
15+
16+
IServiceCollection services = new ServiceCollection();
17+
services.AddAutoMapper();
18+
19+
var serviceProvider = services.BuildServiceProvider();
20+
21+
var config = serviceProvider.GetService<IConfigurationProvider>();
22+
config.ShouldNotBeNull();
23+
24+
try
25+
{
26+
config.ShouldNotBeSameAs(Mapper.Configuration);
27+
}
28+
catch (InvalidOperationException)
29+
{
30+
// Success if the mapper has not been initialized anyway
31+
}
32+
}
33+
}
34+
}

test/AutoMapper.Extensions.Microsoft.DependencyInjection.Tests/TypeResolutionTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class TypeResolutionTests_ForStaticConfig
6666

6767
public TypeResolutionTests_ForStaticConfig()
6868
{
69+
ServiceCollectionExtensions.UseStaticRegistration = true;
6970
IServiceCollection services = new ServiceCollection();
7071
services.AddAutoMapper(typeof(Source));
7172
_provider = services.BuildServiceProvider();

0 commit comments

Comments
 (0)