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

fix FindRegistration for collection case #336

Merged
merged 2 commits into from Jan 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion VContainer/Assets/VContainer/Runtime/Container.cs
Expand Up @@ -157,9 +157,16 @@ Registration FindRegistration(Type type)
{
case CollectionInstanceProvider localCollection:
if (entirelyCollection == null)
entirelyCollection = registration;
{
var typeParameters = RuntimeTypeCache.GenericTypeParametersOf(type);
var collection = new CollectionInstanceProvider(typeParameters[0]);
collection.Merge(localCollection);
entirelyCollection = new Registration(registration.ImplementationType, registration.Lifetime, registration.InterfaceTypes, collection);;
}
else
{
((CollectionInstanceProvider)entirelyCollection.Provider).Merge(localCollection);
}
break;
default:
return registration;
Expand Down
8 changes: 8 additions & 0 deletions VContainer/Assets/VContainer/Tests/Fixtures.cs
Expand Up @@ -183,6 +183,14 @@ class MultipleInterfaceServiceA : I1, I2, I3
class MultipleInterfaceServiceB : I1, I2, I3
{
}

class MultipleInterfaceServiceC : I1, I2, I3
{
}

class MultipleInterfaceServiceD : I1, I2, I3
{
}

class HasDefaultValue
{
Expand Down
19 changes: 19 additions & 0 deletions VContainer/Assets/VContainer/Tests/ScopedContainerTest.cs
Expand Up @@ -211,6 +211,25 @@ public void ResolveCollectionFromParent()
Assert.That(scopedService.enumerable.Count(), Is.EqualTo(2));
}

[Test]
public void ResolveCollectionFromParentByContinuous()
{
var builder = new ContainerBuilder();
builder.Register<I1, MultipleInterfaceServiceA>(Lifetime.Singleton);
builder.Register<I1, MultipleInterfaceServiceB>(Lifetime.Singleton);
var parentContainer = builder.Build();

var childContainer = parentContainer.CreateScope(childBuilder =>
{
childBuilder.Register<I1, MultipleInterfaceServiceC>(Lifetime.Singleton);
childBuilder.Register<I1, MultipleInterfaceServiceD>(Lifetime.Singleton);
});

var scopedService = childContainer.Resolve<IReadOnlyList<I1>>();
var moreScopedService = childContainer.Resolve<IReadOnlyList<I1>>();
Assert.That(moreScopedService.Count(), Is.EqualTo(4));
}

[Test]
public void ResolveCollectionByLazyInstance()
{
Expand Down