8000 Intended behaviour of RegisterComponentInHierarchy() · Issue #727 · hadashiA/VContainer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Intended behaviour of RegisterComponentInHierarchy() #727
Open
@JanikHelbig-NB

Description

@JanikHelbig-NB

Hi,
recently I was debugging an issue in our project related to multiple repeated injections into a MonoBehaviour, let's call it InstancingSystem. This system is registered in the SceneLifetimeScope using RegisterComponentInHierarchy<InstancingSystem>(). Agents can use this system to get instances of pooled objects, for example projectiles. More complex agents, such as enemies, have their own nested EnemyLifetimeScope.

The problem is the following: InstancingSystem is injected with a IObjectResolver, initially the SceneLifetimeScope's. Now, when an enemy is spawned, the IObjectResolver is re-injected, this time with the EnemyLifetimeScope's. When the enemy is destroyed, the IObjectResolver is destroyed with it, resulting in NullReferenceExceptions when InstancingSystem is used again.

The root of the issue is, at least in my opinion, the fact that RegisterComponentInHierarchy() uses Lifetime.Scoped. This does not make sense to me, because in my understanding Scoped is supposed to ensure that a new instance is created for each scope. This is obviously not the case for Components and a automatic re-injection should also always be avoided anyway IMO.

In general I can't really think of a use case where the behavior of Lifetime.Scoped is preferable to Lifetime.Singleton when using RegisterComponentInHierarchy().

I'm also not the only one who has run into issues with this behavior, see #709 and #704 for examples.
Yes, I can just use RegisterComponent(FindFirstObjectOfType<InstancingSystem>()) to work around this, but then what is the point of RegisterComponentInHierarchy()?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0