-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneServiceImplementatinoForVaiousTypes.cs
More file actions
55 lines (43 loc) · 1.7 KB
/
Copy pathOneServiceImplementatinoForVaiousTypes.cs
File metadata and controls
55 lines (43 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Autofac;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace ExtensionsDIvsAutofac
{
public interface IService { }
public class Service : IService { }
public class OneServiceImplementatinoForVaiousTypes
{
[Fact]
public void Autofac_ReturnsSameInstance()
{
var contaienrBuilder = new ContainerBuilder();
contaienrBuilder.RegisterType<Service>().AsSelf().AsImplementedInterfaces().SingleInstance();
var container = contaienrBuilder.Build();
var byInterface = container.Resolve<IService>();
var byExactType = container.Resolve<Service>();
Assert.Same(byInterface, byExactType);
}
[Fact]
public void DI_ReturnsDifferentInstances()
{
var serviceProvider = new ServiceCollection()
.AddSingleton<Service>()
.AddSingleton<IService, Service>()
.BuildServiceProvider();
var byInterface = serviceProvider.GetRequiredService<IService>();
var byExactType = serviceProvider.GetRequiredService<Service>();
Assert.NotSame(byInterface, byExactType);
}
[Fact]
public void DI_ReturnsSameInstace_LikeAutofac()
{
var serviceProvider = new ServiceCollection()
.AddSingleton<Service>()
.AddSingleton<IService>(sp => sp.GetRequiredService<Service>())
.BuildServiceProvider();
var byInterface = serviceProvider.GetRequiredService<IService>();
var byExactType = serviceProvider.GetRequiredService<Service>();
Assert.Same(byInterface, byExactType);
}
}
}