Skip to content

Commit

Permalink
Merge pull request #2836 from PrismLibrary/transient-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel authored Mar 5, 2023
2 parents 403b313 + d3ee5bf commit 8fcc856
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public IContainerRegistry Register(Type from, Type to, string name)
/// <returns>The <see cref="IContainerRegistry" /> instance</returns>
public IContainerRegistry Register(Type type, Func<object> factoryMethod)
{
Instance.RegisterDelegate(type, r => factoryMethod());
Instance.RegisterDelegate(type, r => factoryMethod(), Reuse.Transient);
return this;
}

Expand All @@ -247,7 +247,7 @@ public IContainerRegistry Register(Type type, Func<object> factoryMethod)
/// <returns>The <see cref="IContainerRegistry" /> instance</returns>
public IContainerRegistry Register(Type type, Func<IContainerProvider, object> factoryMethod)
{
Instance.RegisterDelegate(type, factoryMethod);
Instance.RegisterDelegate(type, factoryMethod, Reuse.Transient);
return this;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Containers/Prism.Container.Shared/Tests/ContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,24 @@ public void LocatesImplementationType()
Assert.Equal(typeof(ServiceA), type);
}

[Fact]
public void RegisterTransientDelegateReturnsNewInstanceEachTime()
{
var container = Setup.CreateContainer();
Setup.Registry.Register<ServiceA>(() => new ServiceA());

Assert.NotSame(container.Resolve<ServiceA>(), container.Resolve<ServiceA>());
}

[Fact]
public void RegisterTransientDelegateWithContainerReturnsNewInstanceEachTime()
{
var container = Setup.CreateContainer();
Setup.Registry.Register<ServiceA>(x => new ServiceA());

Assert.NotSame(container.Resolve<ServiceA>(), container.Resolve<ServiceA>());
}

private static IServiceA CreateService() =>
new ServiceA { SomeProperty = "Created through a factory" };

Expand Down

0 comments on commit 8fcc856

Please sign in to comment.