LoginSignup
1
0

More than 1 year has passed since last update.

VContainerの第一歩

Last updated at Posted at 2022-07-26

サンプルコード

SampleLifetimeScope.cs
using VContainer;
using VContainer.Unity;
using UnityEngine;

public class SampleLifetimeScope : LifetimeScope
{
    [SerializeField] ViewS view;
    protected override void Configure(IContainerBuilder builder)
    {
        builder.RegisterComponent<IView>(view);
        if (Const.isdebug)
        {
            builder.Register<StubModelS>(Lifetime.Singleton).As<IModel>();
        }
        else
        {
            builder.Register<ModelS>(Lifetime.Singleton).As<IModel>();
        }
        builder.RegisterEntryPoint<PresenterS>(Lifetime.Singleton);
    }
}

今まではPresenterがエントリーポイントの役割をしていたが、その役割をこのSampleLifetimeScope.csに持たせる。
ここでインスタンスを呼び出したり、引数を持たせてエントリーポイントとする。
ファイル名にLifetimeScopeと入れることで専用の雛形を作ってくれる。

builder.Register<ModelS>(Lifetime.Singleton)

builder.Register<ModelS>(Lifetime.Singleton).As<IModel>();

Monobehaviorを継承していないクラスを登録する際に使用する。
<>内には型を指定する。As<>にはInterfaceを指定することでその型として認識させることができる。

builder.RegisterComponent<IView>(view)

builder.RegisterComponent<IView>(view);

Monobehaviorを継承しているクラスを登録する際に使用する。

builder.RegisterEntryPoint<PresenterS>(Lifetime.Singleton)

builder.RegisterEntryPoint<PresenterS>(Lifetime.Singleton);

エントリーポイントを登録する際に使用する。
実行時にはその他のResisterで登録されているものを勝手に認識してエントリーポイントとして登録したものに代入していってくれるため便利

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0