Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
],
"service": "dev",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"postCreateCommand": "/bin/sh ./.devcontainer/postCreateCommand.sh",
"remoteUser": "vscode",
"customizations": {
"vscode": {
Expand Down
5 changes: 5 additions & 0 deletions .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
echo "USER:" `whoami`

# add xunit3 template
dotnet new install xunit.v3.templates
1 change: 0 additions & 1 deletion examples-dotnet.slnx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Solution>
<Folder Name="/src/">
<Project Path="src/Examples.Benchmark/Examples.Benchmark.csproj" />
<Project Path="src/Examples.DependencyInjection.Autofac.Tests/Examples.DependencyInjection.Autofac.Tests.csproj" />
<Project Path="src/Examples.DependencyInjection.Extensions.Tests/Examples.DependencyInjection.Extensions.Tests.csproj" />
<Project Path="src/Examples.DependencyInjection.Mef.Tests/Examples.DependencyInjection.Mef.Tests.csproj" />
<Project Path="src/Examples.DependencyInjection.Tests/Examples.DependencyInjection.Tests.csproj" />
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Autofac;
using Examples.DependencyInjection.Autofac.Tests.Fixtures.Greeting;

namespace Examples.DependencyInjection.Autofac.Tests.Articles.RegisteredOnSameInterface;

public class RegisteredOnSameInterfaceTests
{
[Fact]
public void When_RegisteredMultipleServicesWithSameInterface_Then_CanInjectedIntoEnumerable()
{
var printer = new MockMessagePrinter();

var builder = new ContainerBuilder();

builder.RegisterInstance(printer)
.As<IMessagePrinter>()
.ExternallyOwned();
builder.RegisterTypes(
typeof(MyMessageGenerator1),
typeof(MyMessageGenerator2),
typeof(MyMessageGenerator3))
.As<IMessageGenerator>()
.SingleInstance();
builder
.RegisterType<GreetingService>()
.As<IGreetingService>()
.SingleInstance();

using var container = builder.Build();

var instance = container.Resolve<IGreetingService>();
instance?.Greet();

var messages = printer.Messages.ToArray();
Assert.Equal(3, messages.Length);
Assert.Contains("Hello Autofac world 1st.", messages[0]);
Assert.Contains("Hello Autofac world 2nd.", messages[1]);
Assert.Contains("Hello Autofac world 3rd.", messages[2]);
}

[Fact]
public void When_UsingGetServices_Then_ReturnsAllServices()
{
var builder = new ContainerBuilder();
builder.RegisterTypes(
typeof(MyMessageGenerator1),
typeof(MyMessageGenerator2),
typeof(MyMessageGenerator3))
.As<IMessageGenerator>()
.SingleInstance();

using var container = builder.Build();

var actual = container.Resolve<IEnumerable<IMessageGenerator>>();
Assert.Equal(3, actual.Count());
}

[Fact]
public void When_UsingGetService_Then_ReturnsLastRegisteredService()
{
var builder = new ContainerBuilder();
builder.RegisterTypes(
typeof(MyMessageGenerator1),
typeof(MyMessageGenerator2),
typeof(MyMessageGenerator3))
.As<IMessageGenerator>()
.SingleInstance();

using var container = builder.Build();

var actual = container.Resolve<IMessageGenerator>();

Assert.Equal("Hello Autofac world 3rd.", actual?.Generate());
}
}

file class MyMessageGenerator1 : IMessageGenerator
{
public string Generate() => "Hello Autofac world 1st.";
}

file class MyMessageGenerator2 : IMessageGenerator
{
public string Generate() => "Hello Autofac world 2nd.";
}

file class MyMessageGenerator3 : IMessageGenerator
{
public string Generate() => "Hello Autofac world 3rd.";
}
Loading