From a69b3965cdbae591f3a2174a37cced8168d68572 Mon Sep 17 00:00:00 2001 From: dadhi Date: Sun, 28 May 2023 19:02:05 +0200 Subject: [PATCH] verify multi-kley behavior for #574 --- ...hild_container_with_default_service_key.cs | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/test/DryIoc.IssuesTests/GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key.cs b/test/DryIoc.IssuesTests/GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key.cs index 7eeca7b5..38bcfdd5 100644 --- a/test/DryIoc.IssuesTests/GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key.cs +++ b/test/DryIoc.IssuesTests/GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key.cs @@ -9,17 +9,18 @@ namespace DryIoc.IssuesTests { - // [TestFixture] + [TestFixture] public class GHIssue574_Cannot_register_multiple_impls_in_child_container_with_default_service_key : ITest { public int Run() { // ResolveEnumerableFromChild(); - ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys(); - return 2; + // ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys(); + ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys_2(); + return 1; } - [Ignore("fixme")] + //[Ignore("fixme")] public void ResolveEnumerableFromChild() { var services = new ServiceCollection(); @@ -79,18 +80,48 @@ public void ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServic Is.EqualTo(4)); } + [Test] + public void ResolveEnumerableFromChild_MefAttributedModel_SupportsMultipleServiceKeys_2() + { + var container = new Container(Rules.MicrosoftDependencyInjectionRules) + .WithMef(); // <-- this is the key, LOL ;-) + + // here use RegisterExport instead of the RegisterDescriptor + container.RegisterExports( + typeof(Printer), + typeof(PrinterA), + typeof(PrinterB), + typeof(NeighborPrinter) + ); + + // all printers with and without the name, this is the default behavior of the Collection wrapper + var ps = container.Resolve(); + CollectionAssert.AreEquivalent( + new[] { typeof(Printer), typeof(PrinterA), typeof(PrinterB), + typeof(NeighborPrinter) }, + ps.Select(p => p.GetType())); + + // only printers with the specific StampName + var psStamped = container.Resolve(serviceKey: StampName); + CollectionAssert.AreEquivalent( + new[] { typeof(Printer), typeof(PrinterA), typeof(PrinterB) }, + psStamped.Select(p => p.GetType())); + } + + private const string StampName = "child-stamp"; + private interface IPrinter { } - [Export("child-stamp", typeof(IPrinter))] + [Export(StampName, typeof(IPrinter))] private class Printer : IPrinter { } - [Export("child-stamp", typeof(IPrinter))] + [Export(StampName, typeof(IPrinter))] private class PrinterA : IPrinter { } - [Export("child-stamp", typeof(IPrinter))] + [Export(StampName, typeof(IPrinter))] private class PrinterB : IPrinter { } - [Export("child-stamp", typeof(IPrinter))] + [Export(typeof(IPrinter))] // No name private class NeighborPrinter : IPrinter { } } }