Skip to content

Commit

Permalink
Ensure transient reuse irrespective of default reuse setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoasai committed Oct 6, 2020
1 parent 66e5b93 commit cd8bc0a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DryIocContainerExtension : IContainerExtension<IContainer>, IContainerInfo
/// <summary>
/// Gets the Default DryIoc Container Rules used by Prism
/// </summary>
public static Rules DefaultRules => Rules.Default.WithConcreteTypeDynamicRegistrations()
public static Rules DefaultRules => Rules.Default.WithConcreteTypeDynamicRegistrations(reuse:Reuse.Transient)
.With(Made.Of(FactoryMethod.ConstructorWithResolvableArguments))
.WithFuncAndLazyWithoutRegistration()
.WithTrackingDisposableTransients()
Expand Down Expand Up @@ -204,7 +204,7 @@ public IContainerRegistry RegisterScoped(Type type, Func<IContainerProvider, obj
/// <returns>The <see cref="IContainerRegistry" /> instance</returns>
public IContainerRegistry Register(Type from, Type to)
{
Instance.Register(from, to);
Instance.Register(from, to, Reuse.Transient);
return this;
}

Expand All @@ -217,7 +217,7 @@ public IContainerRegistry Register(Type from, Type to)
/// <returns>The <see cref="IContainerRegistry" /> instance</returns>
public IContainerRegistry Register(Type from, Type to, string name)
{
Instance.Register(from, to, ifAlreadyRegistered: IfAlreadyRegistered.Replace, serviceKey: name);
Instance.Register(from, to, Reuse.Transient, ifAlreadyRegistered: IfAlreadyRegistered.Replace, serviceKey: name);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Prism.Ioc.Tests
{
partial class ContainerSetup
{
IContainerExtension CreateContainerInternal() => new DryIocContainerExtension();
protected virtual IContainerExtension CreateContainerInternal() => new DryIocContainerExtension();

public Type NativeContainerType => typeof(IContainer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using DryIoc;
using Prism.DryIoc;
using Prism.Ioc.Tests;

namespace Prism.Ioc.DryIoc.Tests
{
public class ContainerSetupWithDefaultSingleton : ContainerSetup
{
public static Rules RulesWithDefaultSingleton => DryIocContainerExtension.DefaultRules.WithDefaultReuse(Reuse.Singleton);

protected override IContainerExtension CreateContainerInternal() => new DryIocContainerExtension(new Container(RulesWithDefaultSingleton));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Prism.Ioc.Mocks.Services;
using Prism.Ioc.Tests;
using System;
using Xunit;

namespace Prism.Ioc.DryIoc.Tests
{
public class ContainerTestsWithDefaultSingleton : IClassFixture<ContainerSetupWithDefaultSingleton>, IDisposable
{
private bool disposedValue;

protected ContainerSetup Setup { get; }

public ContainerTestsWithDefaultSingleton(ContainerSetupWithDefaultSingleton setup)
{
Setup = setup;
}

[Fact]
public void RegisterServiceMappingCreatesTransient()
{
var container = Setup.CreateContainer();
Setup.Registry.Register<IServiceA, ServiceA>();
var resolved1 = container.Resolve<IServiceA>();
var resolved2 = container.Resolve<IServiceA>();
Assert.NotNull(resolved1);
Assert.NotNull(resolved2);
Assert.IsType<ServiceA>(resolved1);
Assert.IsType<ServiceA>(resolved2);
Assert.NotSame(resolved1, resolved2);
}

[Fact]
public void RegisterNamedServiceMappingCreatesTransient()
{
var container = Setup.CreateContainer();
Setup.Registry.Register<IServiceA, ServiceA>("Test");
var resolved1 = container.Resolve<IServiceA>("Test");
var resolved2 = container.Resolve<IServiceA>("Test");
var ex = Record.Exception(() => container.Resolve<IServiceA>());
Assert.NotNull(ex);
Assert.NotNull(resolved1);
Assert.NotNull(resolved2);
Assert.IsType<ServiceA>(resolved1);
Assert.IsType<ServiceA>(resolved2);
Assert.NotSame(resolved1, resolved2);
}

[Fact]
public void AutomaticTransientResolutionOfConcreteType()
{
var container = Setup.CreateContainer();
var resolved1 = container.Resolve<ServiceA>();
var resolved2 = container.Resolve<ServiceA>();
Assert.NotNull(resolved1);
Assert.NotSame(resolved1, resolved2);
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Setup.Dispose();
}

disposedValue = true;
}
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Prism.Ioc.Tests
{
public sealed partial class ContainerSetup : IDisposable
public partial class ContainerSetup : IDisposable
{
public ContainerSetup()
{
Expand Down

0 comments on commit cd8bc0a

Please sign in to comment.