Skip to content

Commit 117fd60

Browse files
committed
Allow multiple Register<T> with Resolve<IEnumerable<T>>
1 parent 94481af commit 117fd60

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

tests/Prism.Container.Extensions.Shared.Tests/Mocks/GenericService.cs

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public class GenericService : IGenericService
99
public string Name { get; set; }
1010
}
1111

12+
public class AltGenericService : IGenericService
13+
{
14+
public string Name { get; set; }
15+
}
16+
1217
public interface IGenericService
1318
{
1419
string Name { get; }

tests/Prism.DryIoc.Extensions.Tests/ContainerTests.cs

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using Prism.Container.Extensions.Shared.Mocks;
56
using Prism.Container.Extensions.Shared.Tests;
67
using Prism.Container.Extensions.Tests.Mocks;
@@ -414,6 +415,34 @@ public void ResolveNamedInstance()
414415
Assert.Same(genB, c.Resolve<IGenericService>("genB"));
415416
}
416417

418+
[Fact]
419+
public void ResolveTakesLastIn()
420+
{
421+
var c = CreateContainer();
422+
c.Register<IGenericService, GenericService>();
423+
c.Register<IGenericService, AltGenericService>();
424+
425+
Assert.IsType<AltGenericService>(c.Resolve<IGenericService>());
426+
}
427+
428+
[Fact]
429+
public void ResolveEnumerableResolvesAll()
430+
{
431+
var c = CreateContainer();
432+
c.Register<IGenericService, GenericService>();
433+
c.Register<IGenericService, AltGenericService>();
434+
435+
IEnumerable<IGenericService> all = null;
436+
var ex = Record.Exception(() => all = c.Resolve<IEnumerable<IGenericService>>());
437+
438+
Assert.Null(ex);
439+
Assert.NotNull(all);
440+
Assert.NotEmpty(all);
441+
Assert.Equal(2, all.Count());
442+
Assert.Contains(all, x => x is GenericService);
443+
Assert.Contains(all, x => x is AltGenericService);
444+
}
445+
417446
public static IFoo FooFactory() => new Foo { Message = "expected" };
418447

419448
public static IBar BarFactoryWithIContainerProvider(IContainerProvider containerProvider) =>

tests/Prism.Microsoft.DependencyInjection.Extensions.Tests/ContainerTests.cs

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using Microsoft.Extensions.DependencyInjection;
56
using Prism.Container.Extensions.Shared.Mocks;
67
using Prism.Container.Extensions.Shared.Tests;
@@ -516,6 +517,34 @@ public void ResolveNamedInstance()
516517
}
517518
}
518519

520+
[Fact]
521+
public void ResolveTakesLastIn()
522+
{
523+
var c = CreateContainer();
524+
c.Register<IGenericService, GenericService>();
525+
c.Register<IGenericService, AltGenericService>();
526+
527+
Assert.IsType<AltGenericService>(c.Resolve<IGenericService>());
528+
}
529+
530+
[Fact]
531+
public void ResolveEnumerableResolvesAll()
532+
{
533+
var c = CreateContainer();
534+
c.Register<IGenericService, GenericService>();
535+
c.Register<IGenericService, AltGenericService>();
536+
537+
IEnumerable<IGenericService> all = null;
538+
var ex = Record.Exception(() => all = c.Resolve<IEnumerable<IGenericService>>());
539+
540+
Assert.Null(ex);
541+
Assert.NotNull(all);
542+
Assert.NotEmpty(all);
543+
Assert.Equal(2, all.Count());
544+
Assert.Contains(all, x => x is GenericService);
545+
Assert.Contains(all, x => x is AltGenericService);
546+
}
547+
519548
public static IFoo FooFactory() => new Foo { Message = "expected" };
520549

521550
public static IBar BarFactoryWithIContainerProvider(IContainerProvider containerProvider) =>

tests/Prism.Unity.Extensions.Tests/ContainerTests.cs

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4-
using Unity;
4+
using System.Linq;
5+
using Prism.Container.Extensions.Shared.Mocks;
6+
using Prism.Container.Extensions.Shared.Tests;
57
using Prism.Container.Extensions.Tests.Mocks;
68
using Prism.Ioc;
9+
using Unity;
710
using Xunit;
811
using Xunit.Abstractions;
9-
using Prism.Container.Extensions.Shared.Mocks;
10-
using Prism.Container.Extensions.Shared.Tests;
1112

1213
namespace Prism.Unity.Extensions.Tests
1314
{
@@ -501,6 +502,34 @@ public void ResolveNamedInstance()
501502
}
502503
}
503504

505+
[Fact]
506+
public void ResolveTakesLastIn()
507+
{
508+
var c = CreateContainer();
509+
c.Register<IGenericService, GenericService>();
510+
c.Register<IGenericService, AltGenericService>();
511+
512+
Assert.IsType<AltGenericService>(c.Resolve<IGenericService>());
513+
}
514+
515+
[Fact(Skip = "Not Supported on Unity")]
516+
public void ResolveEnumerableResolvesAll()
517+
{
518+
var c = CreateContainer();
519+
c.Register<IGenericService, GenericService>();
520+
c.Register<IGenericService, AltGenericService>();
521+
522+
IEnumerable<IGenericService> all = null;
523+
var ex = Record.Exception(() => all = c.Resolve<IEnumerable<IGenericService>>());
524+
525+
Assert.Null(ex);
526+
Assert.NotNull(all);
527+
Assert.NotEmpty(all);
528+
Assert.Equal(2, all.Count());
529+
Assert.Contains(all, x => x is GenericService);
530+
Assert.Contains(all, x => x is AltGenericService);
531+
}
532+
504533
public static IFoo FooFactory() => new Foo { Message = "expected" };
505534

506535
public static IBar BarFactoryWithIContainerProvider(IContainerProvider containerProvider) =>

0 commit comments

Comments
 (0)