Skip to content

Commit

Permalink
Allow multiple Register<T> with Resolve<IEnumerable<T>>
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Jun 29, 2020
1 parent 94481af commit 117fd60
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public class GenericService : IGenericService
public string Name { get; set; }
}

public class AltGenericService : IGenericService
{
public string Name { get; set; }
}

public interface IGenericService
{
string Name { get; }
Expand Down
29 changes: 29 additions & 0 deletions tests/Prism.DryIoc.Extensions.Tests/ContainerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Prism.Container.Extensions.Shared.Mocks;
using Prism.Container.Extensions.Shared.Tests;
using Prism.Container.Extensions.Tests.Mocks;
Expand Down Expand Up @@ -414,6 +415,34 @@ public void ResolveNamedInstance()
Assert.Same(genB, c.Resolve<IGenericService>("genB"));
}

[Fact]
public void ResolveTakesLastIn()
{
var c = CreateContainer();
c.Register<IGenericService, GenericService>();
c.Register<IGenericService, AltGenericService>();

Assert.IsType<AltGenericService>(c.Resolve<IGenericService>());
}

[Fact]
public void ResolveEnumerableResolvesAll()
{
var c = CreateContainer();
c.Register<IGenericService, GenericService>();
c.Register<IGenericService, AltGenericService>();

IEnumerable<IGenericService> all = null;
var ex = Record.Exception(() => all = c.Resolve<IEnumerable<IGenericService>>());

Assert.Null(ex);
Assert.NotNull(all);
Assert.NotEmpty(all);
Assert.Equal(2, all.Count());
Assert.Contains(all, x => x is GenericService);
Assert.Contains(all, x => x is AltGenericService);
}

public static IFoo FooFactory() => new Foo { Message = "expected" };

public static IBar BarFactoryWithIContainerProvider(IContainerProvider containerProvider) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Prism.Container.Extensions.Shared.Mocks;
using Prism.Container.Extensions.Shared.Tests;
Expand Down Expand Up @@ -516,6 +517,34 @@ public void ResolveNamedInstance()
}
}

[Fact]
public void ResolveTakesLastIn()
{
var c = CreateContainer();
c.Register<IGenericService, GenericService>();
c.Register<IGenericService, AltGenericService>();

Assert.IsType<AltGenericService>(c.Resolve<IGenericService>());
}

[Fact]
public void ResolveEnumerableResolvesAll()
{
var c = CreateContainer();
c.Register<IGenericService, GenericService>();
c.Register<IGenericService, AltGenericService>();

IEnumerable<IGenericService> all = null;
var ex = Record.Exception(() => all = c.Resolve<IEnumerable<IGenericService>>());

Assert.Null(ex);
Assert.NotNull(all);
Assert.NotEmpty(all);
Assert.Equal(2, all.Count());
Assert.Contains(all, x => x is GenericService);
Assert.Contains(all, x => x is AltGenericService);
}

public static IFoo FooFactory() => new Foo { Message = "expected" };

public static IBar BarFactoryWithIContainerProvider(IContainerProvider containerProvider) =>
Expand Down
35 changes: 32 additions & 3 deletions tests/Prism.Unity.Extensions.Tests/ContainerTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Unity;
using System.Linq;
using Prism.Container.Extensions.Shared.Mocks;
using Prism.Container.Extensions.Shared.Tests;
using Prism.Container.Extensions.Tests.Mocks;
using Prism.Ioc;
using Unity;
using Xunit;
using Xunit.Abstractions;
using Prism.Container.Extensions.Shared.Mocks;
using Prism.Container.Extensions.Shared.Tests;

namespace Prism.Unity.Extensions.Tests
{
Expand Down Expand Up @@ -501,6 +502,34 @@ public void ResolveNamedInstance()
}
}

[Fact]
public void ResolveTakesLastIn()
{
var c = CreateContainer();
c.Register<IGenericService, GenericService>();
c.Register<IGenericService, AltGenericService>();

Assert.IsType<AltGenericService>(c.Resolve<IGenericService>());
}

[Fact(Skip = "Not Supported on Unity")]
public void ResolveEnumerableResolvesAll()
{
var c = CreateContainer();
c.Register<IGenericService, GenericService>();
c.Register<IGenericService, AltGenericService>();

IEnumerable<IGenericService> all = null;
var ex = Record.Exception(() => all = c.Resolve<IEnumerable<IGenericService>>());

Assert.Null(ex);
Assert.NotNull(all);
Assert.NotEmpty(all);
Assert.Equal(2, all.Count());
Assert.Contains(all, x => x is GenericService);
Assert.Contains(all, x => x is AltGenericService);
}

public static IFoo FooFactory() => new Foo { Message = "expected" };

public static IBar BarFactoryWithIContainerProvider(IContainerProvider containerProvider) =>
Expand Down

0 comments on commit 117fd60

Please sign in to comment.