Skip to content

Commit

Permalink
fix for DryIoc - Change Append to Replace for keyed (contract) regist…
Browse files Browse the repository at this point in the history
…rations (#753)
  • Loading branch information
ChrisPulman authored Aug 18, 2021
1 parent 932d901 commit 1ae8a34
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/Splat.DryIoc.Tests/DependencyResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,21 @@ public void DryIocDependencyResolver_PreInit_Should_ReturnRegisteredLogger()
var d = Splat.Locator.Current.GetService<ILogManager>();
Assert.IsType<FuncLogManager>(d);
}

/// <summary>
/// DryIoc dependency resolver should resolve after duplicate keyed registratoion.
/// </summary>
[Fact]
public void DryIocDependencyResolver_Should_Resolve_AfterDuplicateKeyedRegistratoion()
{
var container = new Container();
container.UseDryIocDependencyResolver();
Locator.CurrentMutable.Register(() => new ViewModelOne(), typeof(ViewModelOne), "ViewModelOne");
Locator.CurrentMutable.Register(() => new ViewModelOne(), typeof(ViewModelOne), "ViewModelOne");

var vmOne = Locator.Current.GetService<ViewModelOne>("ViewModelOne");

vmOne.Should().NotBeNull();
}
}
}
9 changes: 8 additions & 1 deletion src/Splat.DryIoc/DryIocDependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using DryIoc;

Expand Down Expand Up @@ -111,10 +112,16 @@ public virtual void Register(Func<object?> factory, Type? serviceType, string? c

var key = (serviceType, contract);

if (HasRegistration(serviceType, contract))
{
Trace.WriteLine($"Warning: Service {serviceType} already exists with key {contract}, the registration will be replaced.");
}

// Keyed instances can only have a single instance so keep latest
_container.UseInstance(
serviceType,
isNull ? new NullServiceType(factory) : factory(),
IfAlreadyRegistered.AppendNewImplementation,
IfAlreadyRegistered.Replace,
serviceKey: key);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Splat.sln
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Splat.Prism.Tests", "Splat.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Splat.Drawing.Tests", "Splat.Drawing.Tests\Splat.Drawing.Tests.csproj", "{7E350DE5-B8C4-4EF1-9211-7B35643B1971}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.DI.Tests", "ReactiveUI.DI.Tests\ReactiveUI.DI.Tests.csproj", "{FEA4E637-0409-40BC-8518-860509FEEA64}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReactiveUI.DI.Tests", "ReactiveUI.DI.Tests\ReactiveUI.DI.Tests.csproj", "{FEA4E637-0409-40BC-8518-860509FEEA64}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

0 comments on commit 1ae8a34

Please sign in to comment.