Skip to content

Commit 246e7e7

Browse files
committed
[#495] [add] impl
1 parent 64ffdbd commit 246e7e7

File tree

9 files changed

+97
-107
lines changed

9 files changed

+97
-107
lines changed
Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
1-
using Microsoft.AspNetCore;
2-
using Microsoft.AspNetCore.Hosting;
1+
using DryIoc;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Simplify.DI;
7+
using Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection;
8+
using Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection.Tester.Setup;
9+
using Simplify.DI.Provider.DryIoc;
310

4-
namespace Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection.Tester;
11+
var builder = WebApplication.CreateBuilder(args);
512

6-
public class Program
13+
// DryIoc specific workaround
14+
DIContainer.Current = new DryIocDIProvider
715
{
8-
public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();
16+
Container = new Container()
17+
.With(rules =>
18+
rules.With(FactoryMethod.ConstructorWithResolvableArguments)
19+
.WithoutThrowOnRegisteringDisposableTransient())
20+
};
921

10-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
11-
WebHost.CreateDefaultBuilder(args)
12-
.UseStartup<Startup>();
13-
}
22+
// Registrations using Microsoft.Extensions.DependencyInjection
23+
builder.Services.RegisterAll();
24+
25+
// Registrations using Simplify.DI
26+
DIContainer.Current.RegisterAll();
27+
28+
// Unresolved types fix
29+
DIContainer.Current.Register<IHttpContextAccessor, HttpContextAccessor>(LifetimeType.Singleton);
30+
31+
builder.Host.UseServiceProviderFactory(new DIServiceProviderFactory(DIContainer.Current));
32+
33+
var app = builder.Build();
34+
35+
if (app.Environment.IsDevelopment())
36+
app.UseDeveloperExceptionPage();
37+
38+
app.Run(x => x.Response.WriteAsync("Hello World!"));
39+
40+
await app.RunAsync();

src/Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection.Tester/Startup.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [1.5] - 2026-03-07
4+
5+
### Dependencies
6+
7+
- Microsoft.Extensions.DependencyInjection bump to 10.0.3
8+
39
## [1.4] - 2025-10-10
410

511
### Removed

src/Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection/DIRegistratorExtensions.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,13 @@ public static void RegisterFromServiceCollection(this IDIRegistrator registrator
2323
RegisterServiceDescriptor(registrator, item, GetLifetime(item.Lifetime));
2424
}
2525

26-
private static LifetimeType GetLifetime(ServiceLifetime lifetime)
26+
private static LifetimeType GetLifetime(ServiceLifetime lifetime) => lifetime switch
2727
{
28-
switch (lifetime)
29-
{
30-
case ServiceLifetime.Scoped:
31-
return LifetimeType.PerLifetimeScope;
32-
33-
case ServiceLifetime.Singleton:
34-
return LifetimeType.Singleton;
35-
36-
case ServiceLifetime.Transient:
37-
return LifetimeType.Transient;
38-
39-
default:
40-
throw new ArgumentOutOfRangeException();
41-
}
42-
}
28+
ServiceLifetime.Scoped => LifetimeType.PerLifetimeScope,
29+
ServiceLifetime.Singleton => LifetimeType.Singleton,
30+
ServiceLifetime.Transient => LifetimeType.Transient,
31+
_ => throw new ArgumentOutOfRangeException(),
32+
};
4333

4434
private static void RegisterServiceDescriptor(IDIRegistrator registrator, ServiceDescriptor item, LifetimeType lifetime)
4535
{

src/Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection/DIServiceProvider.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@ namespace Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection;
66
/// Simplify.DI based service provider for Microsoft.Extensions.DependencyInjection
77
/// </summary>
88
/// <seealso cref="IServiceProvider" />
9-
public class DIServiceProvider : IServiceProvider
9+
/// <remarks>
10+
/// Initializes a new instance of the <see cref="DIServiceProvider"/> class.
11+
/// </remarks>
12+
/// <param name="resolver">The registrator.</param>
13+
public class DIServiceProvider(IDIResolver resolver) : IServiceProvider
1014
{
11-
private readonly IDIResolver _resolver;
12-
13-
/// <summary>
14-
/// Initializes a new instance of the <see cref="DIServiceProvider"/> class.
15-
/// </summary>
16-
/// <param name="resolver">The registrator.</param>
17-
public DIServiceProvider(IDIResolver resolver) => _resolver = resolver;
18-
1915
/// <summary>
2016
/// Gets the service object of the specified type.
2117
/// </summary>
2218
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
2319
/// <returns>
2420
/// A service object of type <paramref name="serviceType">serviceType</paramref>. -or- null if there is no service object of type <paramref name="serviceType">serviceType</paramref>.
2521
/// </returns>
26-
public object GetService(Type serviceType) => _resolver.Resolve(serviceType);
22+
public object GetService(Type serviceType) => resolver.Resolve(serviceType);
2723
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection;
5+
6+
/// <summary>
7+
/// Provides IServiceProviderFactory implementation for Simplify.DI integration with Microsoft.Extensions.DependencyInjection hosting model
8+
/// </summary>
9+
/// <remarks>
10+
/// Initializes a new instance of <see cref="DIServiceProviderFactory"/>.
11+
/// </remarks>
12+
/// <param name="provider">The DI container provider.</param>
13+
public class DIServiceProviderFactory(IDIContainerProvider provider) : IServiceProviderFactory<IDIContainerProvider>
14+
{
15+
private readonly IDIContainerProvider _provider = provider ?? throw new ArgumentNullException(nameof(provider));
16+
17+
/// <inheritdoc/>
18+
public IDIContainerProvider CreateBuilder(IServiceCollection services)
19+
{
20+
_provider.RegisterFromServiceCollection(services);
21+
22+
return _provider;
23+
}
24+
25+
/// <inheritdoc/>
26+
public IServiceProvider CreateServiceProvider(IDIContainerProvider containerBuilder)
27+
{
28+
var serviceProvider = containerBuilder.CreateServiceProvider();
29+
30+
containerBuilder.Verify();
31+
32+
return serviceProvider;
33+
}
34+
}

src/Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection/DIServiceScope.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,5 @@ public DIServiceScope(IDIContextHandler contextHandler)
2929
/// <summary>
3030
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
3131
/// </summary>
32-
public void Dispose()
33-
{
34-
_scope.Dispose();
35-
}
32+
public void Dispose() => _scope.Dispose();
3633
}

src/Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection/DIServiceScopeFactory.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@ namespace Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection;
66
/// Simplify.DI based service scope factory for Microsoft.Extensions.DependencyInjection
77
/// </summary>
88
/// <seealso cref="IServiceScopeFactory" />
9-
public class DIServiceScopeFactory : IServiceScopeFactory
9+
/// <remarks>
10+
/// Initializes a new instance of the <see cref="DIServiceScopeFactory"/> class.
11+
/// </remarks>
12+
/// <param name="contextHandler">The context handler.</param>
13+
public class DIServiceScopeFactory(IDIContextHandler contextHandler) : IServiceScopeFactory
1014
{
11-
private readonly IDIContextHandler _contextHandler;
12-
13-
/// <summary>
14-
/// Initializes a new instance of the <see cref="DIServiceScopeFactory"/> class.
15-
/// </summary>
16-
/// <param name="contextHandler">The context handler.</param>
17-
public DIServiceScopeFactory(IDIContextHandler contextHandler)
18-
{
19-
_contextHandler = contextHandler;
20-
}
21-
2215
/// <summary>
2316
/// Create an <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceScope" /> which
2417
/// contains an <see cref="T:System.IServiceProvider" /> used to resolve dependencies from a
@@ -30,8 +23,5 @@ public DIServiceScopeFactory(IDIContextHandler contextHandler)
3023
/// from the <see cref="P:Microsoft.Extensions.DependencyInjection.IServiceScope.ServiceProvider" />
3124
/// will also be disposed.
3225
/// </returns>
33-
public IServiceScope CreateScope()
34-
{
35-
return new DIServiceScope(_contextHandler);
36-
}
26+
public IServiceScope CreateScope() => new DIServiceScope(contextHandler);
3727
}

src/Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection/Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99

10-
<Version>1.4</Version>
10+
<Version>1.5</Version>
1111

1212
<Description>Provides ability to use Simplify.DI as IOC container instead of Microsoft.Extensions.DependencyInjection</Description>
1313
<Product>Simplify</Product>
@@ -22,7 +22,7 @@
2222
<PackageReleaseNotes>See https://github.com/SimplifyNet/Simplify/tree/master/src/Simplify.DI.Integration.Microsoft.Extensions.DependencyInjection/CHANGELOG.md for details</PackageReleaseNotes>
2323
</PropertyGroup>
2424
<ItemGroup>
25-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.9" />
25+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.3" />
2626
<PackageReference Include="Simplify.DI" Version="4.2.11" />
2727
</ItemGroup>
2828
<ItemGroup>

0 commit comments

Comments
 (0)