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

Commit 631dbd4

Browse files
committed
Tests showing how bad singleton is
1 parent 20877eb commit 631dbd4

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ public interface ISomeService
7272
int Modify(int value);
7373
}
7474

75+
public class MutableService : ISomeService
76+
{
77+
public int Value { get; set; }
78+
79+
public int Modify(int value) => value + Value;
80+
}
81+
7582
public class FooService : ISomeService
7683
{
7784
private readonly int _value;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace AutoMapper.Extensions.Microsoft.DependencyInjection.Tests
6+
{
7+
public class ScopeTests
8+
{
9+
[Fact]
10+
public void Can_depend_on_scoped_services_as_transient_default()
11+
{
12+
var services = new ServiceCollection();
13+
services.AddAutoMapper(new [] { typeof(Source).Assembly });
14+
services.AddScoped<ISomeService, MutableService>();
15+
16+
var provider = services.BuildServiceProvider();
17+
18+
using (var scope = provider.CreateScope())
19+
{
20+
var mutableService = (MutableService)scope.ServiceProvider.GetService<ISomeService>();
21+
mutableService.Value = 10;
22+
23+
var mapper = scope.ServiceProvider.GetService<IMapper>();
24+
25+
var dest = mapper.Map<Dest2>(new Source2 {ConvertedValue = 5});
26+
27+
dest.ConvertedValue.ShouldBe(15);
28+
}
29+
}
30+
31+
[Fact]
32+
public void Can_depend_on_scoped_services_as_scoped()
33+
{
34+
var services = new ServiceCollection();
35+
services.AddAutoMapper(new [] { typeof(Source).Assembly }, ServiceLifetime.Scoped);
36+
services.AddScoped<ISomeService, MutableService>();
37+
38+
var provider = services.BuildServiceProvider();
39+
40+
using (var scope = provider.CreateScope())
41+
{
42+
var mutableService = (MutableService)scope.ServiceProvider.GetService<ISomeService>();
43+
mutableService.Value = 10;
44+
45+
var mapper = scope.ServiceProvider.GetService<IMapper>();
46+
47+
var dest = mapper.Map<Dest2>(new Source2 {ConvertedValue = 5});
48+
49+
dest.ConvertedValue.ShouldBe(15);
50+
}
51+
}
52+
53+
[Fact]
54+
public void Cannot_correctly_resolve_scoped_services_as_singleton()
55+
{
56+
var services = new ServiceCollection();
57+
services.AddAutoMapper(new [] { typeof(Source).Assembly }, ServiceLifetime.Singleton);
58+
services.AddScoped<ISomeService, MutableService>();
59+
60+
var provider = services.BuildServiceProvider();
61+
62+
using (var scope = provider.CreateScope())
63+
{
64+
var mutableService = (MutableService)scope.ServiceProvider.GetService<ISomeService>();
65+
mutableService.Value = 10;
66+
67+
var mapper = scope.ServiceProvider.GetService<IMapper>();
68+
69+
var dest = mapper.Map<Dest2>(new Source2 {ConvertedValue = 5});
70+
71+
dest.ConvertedValue.ShouldBe(5);
72+
}
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)