Simple, but confusing logic of LifetimeScope.Enqueue
As I understand IContainerBuilder passed to LifetimeScope.Enqueue just adds registrations to each LifetimeScope in scene. So
using (Enqueue(
builder =>
{
builder.Register<UniqueContext>(Lifetime.Singleton); // this is not Singleton for scene or prefab which context would to register
builder.RegisterBuildCallback(c => Debug.Log("Extra scope container is ready")); // It executes every time for each scope in scene or prefab
}
))
works right if loaded scene or created prefab contain only one scope.
Is it possible to make alternate Enqueue method which creates super-scope with passed builder? Also it would be convenient to have ability to alternate IObjectResolver and LifetimeScope in hierarchy.
Is it possible to make alternate Enqueue method which creates super-scope with passed builder?
How about this ?
// You can find scope from loaded scene
// var parentLifetimeScope = LifetimeScope.Find<ParentLifetimeScope>();
parentLifetimeScope.CreateChild(childLifetimeScopePrefab);
it would be convenient to have ability to alternate IObjectResolver and LifetimeScope in hierarchy.
Sorry, I don't think I understand your request. Could you provide an example of the code you want to write?
There is a scenario where we want to register services in scope 1. Scope 1 also creates child scopes, for example scope 2 which also registers services, instead of just using services from parent scope 1.
We use such a scenario to load a scene and provide it with the necessary services to work. But the scene itself can create a LifetimeScope in which the registration code is called too, which seems not obvious. For example, when calling RegisterComponent, a component will be created for each child LifetimeScope inside our scene.
If you look through VContainerDiagnostic, you can see that on the second stage a new dependency is created on ExampleRegistration. unitypackage
You can also change
b.Register<ExampleRegistration>(Lifetime.Singleton).AsSelf();
to
b.RegisterEntryPoint<ExampleRegistration>();
and get the same result with debug logs from ExampleRegistration
Also faced this issue in my project