Skip to content

Commit

Permalink
added example of console app integrating DryIoc.MS.DI with HostApplic…
Browse files Browse the repository at this point in the history
…ationBuilder + updated MinWeb to .NET8, see #664
  • Loading branch information
dadhi committed Nov 6, 2024
1 parent e1dab33 commit 0e50cd4
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 25 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "MediatR comptime",
"type": "coreclr",
Expand Down
16 changes: 16 additions & 0 deletions samples/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="8.0.0-preview-03" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>

</Project>
73 changes: 73 additions & 0 deletions samples/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Sample from the official https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using DryIoc;
using DryIoc.Microsoft.DependencyInjection;
using static System.Console;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddTransient<TransientDisposable>();
builder.Services.AddScoped<ScopedDisposable>();
builder.Services.AddSingleton<SingletonDisposable>();

// Integrate DryIoc
builder.ConfigureContainer(new DryIocServiceProviderFactory(),
// optional configuration action
serviceProvider =>
{
// Factory returns the DryIocServiceProvider, but you may access the DryIoc container via the Container field.
// Note: avoid storing the container instance in your app, because it depends on the current scope you're in.
// Instead, if required, resolve it from the services as shown below.
var c = serviceProvider.Container;
c.Register<Bazz>();
});


using IHost host = builder.Build();

// Resolve the actual DryIoc.IContainer from the services.
var container = host.Services.GetRequiredService<IContainer>();
var serviceProvider = container.Resolve<IServiceProvider>();

Console.WriteLine($"The actual container is {container.GetType().FullName}, and the service provider is {serviceProvider.GetType().FullName}");

ExemplifyDisposableScoping(host.Services, "Scope 1");
Console.WriteLine();

ExemplifyDisposableScoping(host.Services, "Scope 2");
Console.WriteLine();

await host.RunAsync();

static void ExemplifyDisposableScoping(IServiceProvider services, string scope)
{
Console.WriteLine($"{scope}...");

using IServiceScope serviceScope = services.CreateScope();
Console.WriteLine($"The actual serviceScope implementation is {serviceScope.GetType().Name}");

IServiceProvider provider = serviceScope.ServiceProvider;

_ = provider.GetRequiredService<TransientDisposable>();
_ = provider.GetRequiredService<ScopedDisposable>();
_ = provider.GetRequiredService<SingletonDisposable>();
_ = provider.GetRequiredService<Bazz>();
}

public sealed class TransientDisposable : IDisposable
{
public void Dispose() => Console.WriteLine($"{nameof(TransientDisposable)}.Dispose()");
}

public sealed class ScopedDisposable : IDisposable
{
public void Dispose() => Console.WriteLine($"{nameof(ScopedDisposable)}.Dispose()");
}

public sealed class SingletonDisposable : IDisposable
{
public void Dispose() => Console.WriteLine($"{nameof(SingletonDisposable)}.Dispose()");
}

public record Bazz(TransientDisposable TransientDep, ScopedDisposable ScopedDep, SingletonDisposable SingletonDep);
11 changes: 0 additions & 11 deletions samples/ConsoleNet7/ConsoleNet7.csproj

This file was deleted.

1 change: 0 additions & 1 deletion samples/ConsoleNet7/Program.cs

This file was deleted.

9 changes: 4 additions & 5 deletions samples/MinimalWeb/MinimalWeb.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>

<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DryIoc.Microsoft.DependencyInjection\DryIoc.Microsoft.DependencyInjection.csproj" />
<!-- <PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="6.0.0" /> -->
<!-- <ProjectReference Include="..\..\src\DryIoc.Microsoft.DependencyInjection\DryIoc.Microsoft.DependencyInjection.csproj" /> -->
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="8.0.0-preview-03" />
</ItemGroup>

</Project>
16 changes: 8 additions & 8 deletions samples/MinimalWeb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@

var app = builder.Build();

app.MapGet("/", (Foo foo) => $"Hello world with `{foo}`");
app.MapGet("/", (Foo foo) => $"Hello world with `{foo}`, try /bar to get bar.");
app.MapGet("/bar", (Foo foo, Bar bar) => $"Hello world with `{foo}` and `{bar}`");

app.Run();

public class Foo
{
public Foo(Bar bar = null) {}
public Foo(Bar bar = null) { }
}

public class Bar {}
public class Bar { }

public class ScopedAutomaticallyResolved
public class ScopedAutomaticallyResolved
{
public readonly SingletonAutomaticallyResolved Singleton;
public ScopedAutomaticallyResolved(SingletonAutomaticallyResolved singleton)
Expand All @@ -41,7 +41,7 @@ public ScopedAutomaticallyResolved(SingletonAutomaticallyResolved singleton)
}
}

public class SingletonAutomaticallyResolved
public class SingletonAutomaticallyResolved
{
public SingletonAutomaticallyResolved()
{
Expand All @@ -51,9 +51,9 @@ public SingletonAutomaticallyResolved()

public sealed class MyContainer : Container
{
public MyContainer(Rules rules) : base(rules) {}
public MyContainer(Rules rules) : base(rules) { }

public override IContainer WithNewOpenScope()
public override IContainer WithNewOpenScope()
{
var scope = base.WithNewOpenScope();
scope.Resolve<ScopedAutomaticallyResolved>();
Expand All @@ -63,7 +63,7 @@ public override IContainer WithNewOpenScope()

public class MyDryIocServiceProviderFactory : DryIocServiceProviderFactory
{
public MyDryIocServiceProviderFactory(IContainer container) : base(container) {}
public MyDryIocServiceProviderFactory(IContainer container) : base(container) { }

public override IServiceProvider CreateServiceProvider(DryIocServiceProvider provider)
{
Expand Down

0 comments on commit 0e50cd4

Please sign in to comment.