Last active
July 19, 2020 12:29
-
-
Save dadhi/b63196646c3788dc7f96b32be454ff51 to your computer and use it in GitHub Desktop.
DryIoc issue #293: ServiceKey and ServiceProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Execute me as: | |
``` | |
dotnet tool install dotnet-script | |
dotnet sctipt https://tinyurl.com/yylj3vcp | |
``` | |
```cs */ | |
#r "nuget: Microsoft.Extensions.DependencyInjection, 2.2.0" | |
#r "nuget: Microsoft.Extensions.Options, 1.0.0" | |
#r "nuget: DryIoc.Microsoft.DependencyInjection, 4.0.0" | |
#r "nuget: NUnit, 3.12.0" | |
using DryIoc; | |
using DryIoc.Microsoft.DependencyInjection; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Options; | |
using NUnit.Framework; | |
ResolveViaOptions(); | |
WriteLine($"{nameof(ResolveViaOptions)} passed"); | |
ResolveDirect(); | |
WriteLine($"{nameof(ResolveDirect)} passed"); | |
public void ResolveViaOptions() | |
{ | |
var container = new Container(); | |
var services = new ServiceCollection(); | |
services.AddOptions(); | |
services.Configure<MsDiServiceThatNeedsOptionsOption>(c => c.Info = "Bob Builder"); | |
// 1st FIX | |
// services.AddSingleton<IMsDiServiceThatNeedsOptions, MsDiServiceThatNeedsOptions>(); | |
container.Register<IMsDiServiceThatNeedsOptions, MsDiServiceThatNeedsOptions>( | |
Reuse.Singleton, | |
made: Parameters.Of.Type<IOptions<MsDiServiceThatNeedsOptionsOption>>(serviceKey: "special")); | |
container.Register<IDryIocServiceThatNeedsOptions, DryIocServiceThatNeedsOptions>(Reuse.Transient); | |
// comment to avoid the "special" registation that just hides the default implementation | |
container.Register<IOptions<MsDiServiceThatNeedsOptionsOption>>( | |
serviceKey: "special", | |
made: Made.Of(() => Options.Create(Arg.Index<MsDiServiceThatNeedsOptionsOption>(0)), | |
r => new MsDiServiceThatNeedsOptionsOption { Info = "Alice Anger" }), | |
reuse: Reuse.Singleton); | |
var serviceContainer = container.WithDependencyInjectionAdapter(services); | |
var resolvedMsDiService = serviceContainer.GetRequiredService<IMsDiServiceThatNeedsOptions>(); | |
// PROBLEM is it should be "Bob Builder" | |
Assert.NotNull(resolvedMsDiService); | |
Assert.AreEqual("Alice Anger", resolvedMsDiService.GiveInfo(), "MsDI"); | |
var resolvedDryIocService = serviceContainer.GetRequiredService<IDryIocServiceThatNeedsOptions>(); | |
Assert.NotNull(resolvedDryIocService); | |
Assert.AreEqual("Alice Anger", resolvedDryIocService.GiveInfo(), "DiDI"); | |
} | |
public void ResolveDirect() | |
{ | |
var services = new ServiceCollection(); | |
services.AddSingleton<MsDiServiceThatNeedsOptionsOption>(new MsDiServiceThatNeedsOptionsOption { Info = "A" }); | |
var container = new Container(); | |
container.Register<MsDiServiceThatNeedsOptionsOption>( | |
serviceKey: "special", | |
made: Made.Of(() => new MsDiServiceThatNeedsOptionsOption { Info = "B" }), | |
reuse: Reuse.Singleton); | |
var serviceContainer = container.WithDependencyInjectionAdapter(services); | |
var resolvedMsDiService = serviceContainer.GetRequiredService<MsDiServiceThatNeedsOptionsOption>(); | |
Assert.NotNull(resolvedMsDiService); | |
Assert.AreEqual("A", resolvedMsDiService.Info); | |
var serviceFromDryIoc = serviceContainer.Resolve<MsDiServiceThatNeedsOptionsOption>(serviceKey: "special"); | |
Assert.NotNull(serviceFromDryIoc); | |
Assert.AreEqual("B", serviceFromDryIoc.Info); | |
var serviceFromMsDi = serviceContainer.Resolve<MsDiServiceThatNeedsOptionsOption>(serviceKey: null); | |
Assert.NotNull(serviceFromMsDi); | |
Assert.AreEqual("A", serviceFromMsDi.Info); | |
} | |
public class DryIocServiceThatNeedsOptions : IDryIocServiceThatNeedsOptions | |
{ | |
private readonly IMsDiServiceThatNeedsOptions msService; | |
public DryIocServiceThatNeedsOptions(IMsDiServiceThatNeedsOptions msService) | |
{ | |
this.msService = msService; | |
} | |
public string GiveInfo() => msService.GiveInfo(); | |
} | |
public interface IDryIocServiceThatNeedsOptions | |
{ | |
string GiveInfo(); | |
} | |
public class MsDiServiceThatNeedsOptions : IMsDiServiceThatNeedsOptions | |
{ | |
private readonly string info; | |
public MsDiServiceThatNeedsOptions(IOptions<MsDiServiceThatNeedsOptionsOption> options) | |
{ | |
info = options.Value.Info; | |
} | |
public string GiveInfo() => info; | |
} | |
public interface IMsDiServiceThatNeedsOptions | |
{ | |
string GiveInfo(); | |
} | |
public class MsDiServiceThatNeedsOptionsOption | |
{ | |
public string Info { get; set; } | |
} | |
/* | |
```cs */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment