Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for keyed services #211

Merged
merged 5 commits into from
Nov 7, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Install .Net Core
uses: actions/[email protected]
with:
dotnet-version: 7.0.100
dotnet-version: 8.0.x
- name: Install dotnet-script
run: dotnet tool install dotnet-script --tool-path dotnet-script-tool

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"coverage-gutters.lcovname": "../build/Artifacts/TestCoverage/coverage.info"
"coverage-gutters.lcovname": "../build/Artifacts/TestCoverage/coverage.info",
"cSpell.words": [
"Xunit"
]
}
2 changes: 1 addition & 1 deletion build/build.csx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#load "nuget:Dotnet.Build, 0.16.1"
#load "nuget:Dotnet.Build, 0.23.0"
#load "nuget:dotnet-steps, 0.0.2"

[StepDescription("Runs the tests with test coverage")]
Expand Down
2 changes: 1 addition & 1 deletion omnisharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
},
"script": {
"enableScriptNuGetReferences": true,
"defaultTargetFramework": "net7.0"
"defaultTargetFramework": "net8.0"
}
}
1 change: 1 addition & 0 deletions packlocal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotnet pack "src/LightInject.Microsoft.DependencyInjection/LightInject.Microsoft.DependencyInjection.csproj" --configuration Release --output "build/Artifacts/NuGet" /property:Version=$1
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LightInject.Microsoft.DependencyInjection\LightInject.Microsoft.DependencyInjection.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ public async Task ShouldDisposeAsyncDisposableFromRootScope()
Assert.Contains(asyncDisposable, disposedObjects);
Assert.Single(disposedObjects);
}

[Fact]
public async Task ShouldHandleDisposeAsyncOnServiceProvider()
{

}
}

public class AsyncDisposable : IAsyncDisposable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Specification;
using Xunit;
using Xunit.Sdk;

namespace LightInject.Microsoft.DependencyInjection.Tests;

public class LightInjectKeyedSpecificationTests : KeyedDependencyInjectionSpecificationTests
{
protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection)
{
return serviceCollection.CreateLightInjectServiceProvider(new ContainerOptions() { EnableCurrentScope = false });
}

[Fact]
public void ShouldHandleKeyedServiceWithEnumKey()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService, KeyedService>(Key.A);
var provider = CreateServiceProvider(serviceCollection);
provider.GetKeyedService<IKeyedService>(Key.A);
}

[Fact]
public void ShouldHandleKeyedServiceWithStringServiceKey()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService, KeyedServiceWithStringServiceKey>("A");
var provider = CreateServiceProvider(serviceCollection);
var instance = provider.GetKeyedService<IKeyedService>("A");
Assert.Equal("A", ((KeyedServiceWithStringServiceKey)instance).ServiceKey);
}

[Fact]
public void ShouldHandleRequiredKeyedServiceWithStringServiceKeyUsingFactory()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService>(Key.A, (sp, key) => new KeyedServiceWithStringServiceKey((string)key));
var provider = CreateServiceProvider(serviceCollection);
var instance = provider.GetRequiredKeyedService<IKeyedService>("A");
Assert.Equal("A", ((KeyedServiceWithStringServiceKey)instance).ServiceKey);
}

[Fact]
public void ShouldHandleKeyedServiceWithEnumServiceKey()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService, KeyedServiceWithEnumServiceKey>(Key.A);
var provider = CreateServiceProvider(serviceCollection);
var instance = provider.GetKeyedService<IKeyedService>(Key.A);
Assert.Equal(Key.A, ((KeyedServiceWithEnumServiceKey)instance).ServiceKey);
}

[Fact]
public void ShouldHandleKeyedServiceWithIntServiceKey()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService, KeyServiceWithIntKey>(1);
var provider = CreateServiceProvider(serviceCollection);
var instance = provider.GetKeyedService<IKeyedService>(1);
Assert.Equal(1, ((KeyServiceWithIntKey)instance).ServiceKey);
}

[Fact]
public void ShouldHandlePassingServiceKeyAsInteger()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService>(1, (sp, key) => new KeyServiceWithIntKey((int)key));
var provider = CreateServiceProvider(serviceCollection);
var instance = provider.GetKeyedService<IKeyedService>(1);
Assert.Equal(1, ((KeyServiceWithIntKey)instance).ServiceKey);
}

[Fact]
public void ShouldHandlePassingServiceKeyAsEnum()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService>(Key.A, (sp, key) => new KeyedServiceWithEnumServiceKey((Key)key));
var provider = CreateServiceProvider(serviceCollection);
var instance = provider.GetKeyedService<IKeyedService>(Key.A);
Assert.Equal(Key.A, ((KeyedServiceWithEnumServiceKey)instance).ServiceKey);
}

[Fact]
public void ShouldThrowExceptionWhenUsingInvalidKeyType()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService>(new StringBuilder(), (sp, key) => new KeyedServiceWithEnumServiceKey((Key)key));
var provider = CreateServiceProvider(serviceCollection);
Assert.Throws<InvalidOperationException>(() => provider.GetKeyedService<IKeyedService>(new StringBuilder()));
}

[Fact]
public void ShouldHandleConvertibleType()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService>(1L, (sp, key) => new KeyedServiceWithLongServiceKey((long)key));
var provider = CreateServiceProvider(serviceCollection);
var instance = provider.GetKeyedService<IKeyedService>(1L);
Assert.Equal(1L, ((KeyedServiceWithLongServiceKey)instance).ServiceKey);
}

[Fact]
public void ShouldHandlePassingNullAsServiceKeyForRequiredService()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IKeyedService, KeyedService>(Key.A);
var provider = CreateServiceProvider(serviceCollection);
Assert.Throws<InvalidOperationException>(() => provider.GetRequiredKeyedService<IKeyedService>(null));
}
}

public interface IKeyedService
{
}

public class KeyedService : IKeyedService
{

}

public class KeyedServiceWithStringServiceKey([ServiceKey] string serviceKey) : IKeyedService
{
public string ServiceKey { get; } = serviceKey;
}

public class KeyedServiceWithEnumServiceKey([ServiceKey] Key serviceKey) : IKeyedService
{
public Key ServiceKey { get; } = serviceKey;
}


public class KeyServiceWithIntKey([ServiceKey] int serviceKey) : IKeyedService
{
public int ServiceKey { get; } = serviceKey;
}


public class KeyedServiceWithLongServiceKey([ServiceKey] long serviceKey) : IKeyedService
{
public long ServiceKey { get; } = serviceKey;
}

public enum Key
{
A,
B
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,6 @@ public void ShouldDisposeRootScopeWhenProviderRequestedFromContainerIsDisposed()
Assert.True(foo.IsDisposed);
}


[Fact]
public void ShouldPickServiceWithoutServiceNameAsDefaultIfRegistered()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<string>(p => "42");
var factory = new LightInjectServiceProviderFactory(options => options.DefaultServiceSelector = serviceNames => serviceNames.SingleOrDefault(string.IsNullOrWhiteSpace) ?? serviceNames.Last());
var container = factory.CreateBuilder(serviceCollection);

container.Register<string>(f => "84");
var provider = container.CreateServiceProvider(serviceCollection);
var value = provider.GetService<string>();

Assert.Equal("84", value);
}

[Fact]
public void ShouldCallConfigureAction()
{
Expand Down
Loading
Loading