Skip to content

How can I inject dependencies into nested objects? #201

Answered by JesseTG
JesseTG asked this question in Q&A
Discussion options

You must be logged in to vote

I have decided to bite the bullet and just use a big-ass constructor. My PhysicsSystem as defined in the original post is unchanged.

My GameplayFeature now looks like this:

using VContainer;

public sealed class GameplayFeature : Feature
{
    public GameplayFeature(
        PhysicsSystem physics,
        AiFeature ai
    )
    {
        Add(physics);
        Add(ai);
    }

    public static void Register(IContainerBuilder builder)
    {
        builder.Register<GameplayFeature>(Lifetime.Singleton);
        builder.Register<PhysicsSystem>(Lifetime.Singleton);
        AiFeature.Register(builder); // Implementation is similar to this one, but with AiFeature's systems and sub-features
    }
}

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by JesseTG
Comment options

You must be logged in to vote
3 replies
@JesseTG
Comment options

@hadashiA
Comment options

@SewerynVixa
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants