VContainer icon indicating copy to clipboard operation
VContainer copied to clipboard

Simple, but confusing logic of LifetimeScope.Enqueue

Open longbombus opened this issue 4 years ago • 4 comments

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.

longbombus avatar Oct 18 '21 06:10 longbombus

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?

hadashiA avatar Nov 28 '21 05:11 hadashiA

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.

warmerko avatar Apr 06 '22 03:04 warmerko

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

Yggdrasills avatar Apr 06 '22 06:04 Yggdrasills

Also faced this issue in my project

Akilaydin avatar Aug 01 '23 16:08 Akilaydin