Skip to content

Commit

Permalink
Named
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym committed Dec 30, 2023
1 parent 694aa23 commit d4deff3
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Jab.FunctionalTests.Common/ContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,35 @@ public void SupportsImplicitFunc()

Assert.NotSame(transientService1, transientService2);
}

[Fact]
public void SupportsImplicitNamedFunc()
{
SupportsImplicitFuncFactoryContainer c = new();
var transientFunc = c.GetService<Func<IService>>("named");
var transientFunc2 = c.GetService<Func<IService>>("named");
var transientService1 = transientFunc();
var transientService2 = transientFunc();

var singletonFunc = c.GetService<Func<IService2>>("named");
var singletonFunc2 = c.GetService<Func<IService2>>("named");

var singletonService1 = singletonFunc();
var singletonService2 = singletonFunc2();

Assert.Equal(2, c.TransientNamedCount);
Assert.Equal(1, c.SingletonNamedCount);

Assert.Same(singletonFunc, singletonFunc2);
Assert.Same(transientFunc, transientFunc2);

Assert.Same(singletonService1, singletonService2);
Assert.NotSame(transientService1, transientService2);
}

[ServiceProvider(RootServices = new [] { typeof(Func<IService1>) })]
[Transient(typeof(IService), Factory=nameof(TransientNamedFactory), Name = "named")]
[Singleton(typeof(IService2), Factory=nameof(SingletonNamedFactory), Name = "named")]
[Transient(typeof(IService), Factory=nameof(TransientFactory))]
[Scoped(typeof(IService1), Factory=nameof(ScopedFactory))]
[Singleton(typeof(IService2), Factory=nameof(SingletonFactory))]
Expand All @@ -974,6 +1001,9 @@ internal partial class SupportsImplicitFuncFactoryContainer
internal int TransientCount = 0;
internal int ScopedCount = 0;
internal int SingletonCount = 0;

internal int TransientNamedCount = 0;
internal int SingletonNamedCount = 0;

internal ServiceImplementation TransientFactory()
{
Expand All @@ -990,6 +1020,17 @@ internal ServiceImplementation SingletonFactory()
SingletonCount++;
return new();
}

internal ServiceImplementation TransientNamedFactory()
{
TransientNamedCount++;
return new();
}
internal ServiceImplementation SingletonNamedFactory()
{
SingletonNamedCount++;
return new();
}
}

#region Non-generic member factory with parameters
Expand Down

0 comments on commit d4deff3

Please sign in to comment.