Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transient factory #2836

Merged
merged 2 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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