Skip to content

Commit

Permalink
Merge pull request #500 from seesharper/flow-scope
Browse files Browse the repository at this point in the history
Flow the scope when possible
  • Loading branch information
seesharper authored Aug 28, 2019
2 parents 695fde7 + 1801487 commit cabdef2
Show file tree
Hide file tree
Showing 39 changed files with 1,268 additions and 4,052 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local.properties
.settings/
.loadpath
Artifacts
BenchmarkDotNet.Artifacts
CoverageResult
*.exe
.DS_Store
Expand Down
31 changes: 15 additions & 16 deletions build/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Script Debug",
"type": "coreclr",
"request": "launch",
"program": "dotnet",
"args": [
"exec",
"/Users/bernhardrichter/.dotnet/tools/.store/dotnet-script/0.28.0/dotnet-script/0.28.0/tools/netcoreapp2.1/any/dotnet-script.dll"
"${file}"
],
"cwd": "${workspaceRoot}",
"stopAtEntry": true
}
]
"version": "0.2.0",
"configurations": [
{
"name": ".NET Script Debug",
"type": "coreclr",
"request": "launch",
"program": "${env:HOME}/.dotnet/tools/dotnet-script",
"args": ["${file}"],
"windows": {
"program": "${env:USERPROFILE}/.dotnet/tools/dotnet-script.exe",
},
"cwd": "${workspaceFolder}",
"stopAtEntry": true,
}
]
}
2 changes: 1 addition & 1 deletion build/BuildContext.csx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#load "nuget:Dotnet.Build, 0.5.0"
#load "nuget:Dotnet.Build, 0.7.1"
using static FileUtils;
using System.Xml.Linq;

Expand Down
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.5.0"
#load "nuget:Dotnet.Build, 0.7.1"
#load "nuget:github-changelog, 0.1.5"
#load "nuget:dotnet-steps, 0.0.1"
#load "BuildContext.csx"
Expand Down
7 changes: 7 additions & 0 deletions src/.vscode/csharp.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Inheritdoc": {
"prefix": "idoc",
"body": ["/// <inheritdoc/>"],
"description": "Inheritdoc"
}
}
21 changes: 16 additions & 5 deletions src/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{
"coverage-gutters.lcovname": "./CoverageResult/coverage.info",
"coverage-gutters.xmlname": "./CoverageResult/coverage.opencover.xml",
"dotnet-test-explorer.testArguments": "-c release -f netcoreapp2.0",
"dotnet-test-explorer.showCodeLens": true
}
"coverage-gutters.lcovname": "./CoverageResult/coverage.info",
"coverage-gutters.xmlname": "./CoverageResult/coverage.opencover.xml",
"omnisharp.path": "latest",
"cSpell.words": [
"Callvirt",
"Conv",
"Ldarg",
"Ldelem",
"Ldlen",
"Ldloc",
"Ldstr",
"MSIL",
"Stloc",
"Xunit"
]
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions src/LightInject.Benchmarks/LightInject.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
<PackageReference Include="DryIoc" Version="4.0.5" />
<PackageReference Include="Grace" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="SimpleInjector" Version="4.6.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LightInject\LightInject.csproj" />
Expand Down
66 changes: 2 additions & 64 deletions src/LightInject.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,13 @@
using BenchmarkDotNet.Running;
using Microsoft.Extensions.DependencyInjection;

namespace LightInject.Microsoft.DependencyInjection.Benchmarks
namespace LightInject.Benchmarks
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<PerformanceTests>();
}
static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}

public class PerformanceTests
{
private IServiceProvider lightInjectServiceProvider;

private IServiceProvider microsoftServiceProvider;

private IServiceContainer serviceContainer;

private IServiceFactory scope;



[GlobalSetup]
public void Setup()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<Foo>();

serviceContainer = new ServiceContainer();
scope = serviceContainer.BeginScope();
serviceContainer.Register<Foo>();
//lightInjectServiceProvider = serviceContainer.CreateServiceProvider(serviceCollection);

microsoftServiceProvider = serviceCollection.BuildServiceProvider();
}

[Benchmark]
public void UsingLightInject()
{
//var instance = lightInjectServiceProvider.GetService<Foo>();

var instance = scope.GetInstance<Foo>();

//var factory = (IServiceScopeFactory)lightInjectServiceProvider.GetService(typeof(IServiceScopeFactory));

// using (var scope = factory.CreateScope())
// {
// //var controller = scope.ServiceProvider.GetService(typeof(Foo));
// }
}

[Benchmark]
public void UsingMicrosoft()
{
var instance = microsoftServiceProvider.GetService<Foo>();

//var factory = (IServiceScopeFactory)microsoftServiceProvider.GetService(typeof(IServiceScopeFactory));

// using (var scope = factory.CreateScope())
// {
// //var controller = scope.ServiceProvider.GetService(typeof(Foo));
// }
}
}


public class Foo
{

}

}
71 changes: 71 additions & 0 deletions src/LightInject.Benchmarks/ScopeBencharks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.DependencyInjection;
using SimpleInjector;
using SimpleInjector.Lifestyles;

namespace LightInject.BenchMarks
{
[MemoryDiagnoser]
public class ScopeBenchmarks
{
private IServiceScope microsoftScope;

private IServiceContainer serviceContainer;

private IServiceFactory lightInjectScope;

private SimpleInjector.Scope simpleInjectorScope;


[GlobalSetup]
public void Setup()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<Foo>();

serviceContainer = new ServiceContainer();
serviceContainer.Register<Foo>();

lightInjectScope = serviceContainer.BeginScope();

var microsoftServiceProvider = serviceCollection.BuildServiceProvider();
microsoftScope = microsoftServiceProvider.CreateScope();


var simpleInjectorContainer = new Container();
simpleInjectorContainer.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();


simpleInjectorContainer.Register<Foo>(Lifestyle.Scoped);
simpleInjectorScope = AsyncScopedLifestyle.BeginScope(simpleInjectorContainer);


}

[Benchmark]
public void UsingLightInject()
{
var instance = lightInjectScope.GetInstance<Foo>();
}

[Benchmark]
public void UsingSimpleInjector()
{
var instance = simpleInjectorScope.GetInstance<Foo>();
}

[Benchmark]
public void UsingMicrosoft()
{
#pragma warning disable IDE0059
var instance = microsoftScope.ServiceProvider.GetService<Foo>();
}
}


public class Foo
{

}
}
11 changes: 7 additions & 4 deletions src/LightInject.Tests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace LightInject.Tests
using Xunit;
using System.Threading.Tasks;

#if NET452 || NET46
#if NET452 || NET46 || NETCOREAPP2_0

public class AsyncTests : TestBase
{
[Fact]
[Fact]
public void GetInstance_Continuation_ThrowException()
{
var container = new ServiceContainer();
Expand All @@ -24,7 +24,10 @@ public void GetInstance_Continuation_ThrowException()
using (container.BeginScope())
{
var instance = container.GetInstance<IAsyncFoo>();
Assert.Throws<AggregateException>(() => instance.GetBar().Wait());
// This no longer throws since we injected lazy is closed around the scope.
// Assert.Throws<AggregateException>(() => instance.GetBar().Wait());
var bar = instance.GetBar().Result;
Assert.NotNull(bar);
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/LightInject.Tests/ContainerMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace LightInject.Tests
using LightMock;

internal class ContainerMock : MockContext<IServiceContainer>, IServiceContainer
{
{

public IEnumerable<ServiceRegistration> AvailableServices { get; private set; }

Expand Down Expand Up @@ -179,7 +179,7 @@ public IServiceRegistry Register(ServiceRegistration serviceRegistration)

public IServiceRegistry RegisterAssembly(Assembly assembly)
{
return ((IInvocationContext<IServiceContainer>)this).Invoke(c => c.RegisterAssembly(assembly));
return ((IInvocationContext<IServiceContainer>)this).Invoke(c => c.RegisterAssembly(assembly));
}

public IServiceRegistry RegisterAssembly(Assembly assembly, Func<Type, Type, bool> shouldRegister)
Expand Down Expand Up @@ -424,5 +424,15 @@ public void Compile<TService>(string serviceType = null)
{
throw new NotImplementedException();
}

public object GetInstance(Type serviceType, Scope scope)
{
throw new NotImplementedException();
}

public object GetInstance(Type serviceType, Scope scope, string serviceName)
{
throw new NotImplementedException();
}
}
}
Loading

0 comments on commit cabdef2

Please sign in to comment.