Skip to content

IoC(DI)

chaolun edited this page Oct 22, 2019 · 3 revisions

Dependency Injection

  • Before start don't forgot using UniEasy.DI

  • About inject you can use like

    • Constructor Injection

      public class Foo
      {
          IBar bar;
      
          public Foo(IBar bar)
          {
              this.bar = bar;
          }
      }
      
    • Field Injection

      public class Foo
      {
          [Inject]
          IBar bar;
      }
      
    • Property Injection

      public class Foo
      {
          [Inject]
          public IBar Bar {
              get;
              private set;
          }
      }
      
    • Method Injection

      public class Foo
      {
          IBar bar;
      
          [Inject]
          public Init(IBar bar)
          {
              this.bar = bar;
          }
      }
      
  • About binding you can use like

    Container.Bind<Foo>().AsSingle();
    
    Container.Bind<IBar>().To<Bar>().AsSingle();
    
    Container.Bind<IBar>().To<Bar>().FromInstance(new Bar()).AsSingle();
    
    Container.Bind<IBar>().WithId("id").To<Bar>().FromInstance(new Bar ()).AsSingle ();
    
    Container.Bind<Foo>().AsSingle().WhenInjectedInto<Bar>(); (It mean delay binding wait until have Bar type Inject)
    
    Container.Bind<Foo>().AsSingle().NonLazy(); (Normally, The ResultType will instantiate only when the binding variable first used. But, if used NonLazy, The ResultType will be created immediately when application starts)
    
  • Want Inject work

    After var bar = new Bar() don't forgot use DiContainer.Inject (bar);

    If Bar is sub class of MonoBehaviour, It is better add DiContainer.Inject (this) to void Awake ()

Multiple Constructors

In general, a class will be created by binding. eg: Container.Bind<IBar>().To<Bar>().FromInstance(new Bar()).AsSingle();

But if your Bar class have multiple constructors, you need to specify a constructor through the InjectAttribute to make the function of automatic instance creation available.

Or just keep one public constructor.

Installer

  • Installer

  • MonoInstaller

  • ScriptableObjectInstaller

First Inherit one of the three classes and override InstallBindings() function
Then binding and inject objects in InstallBindings() function
Finally use UniEasy.DI.Context class install you created Installer

Context

  • Context

  • ProjectContext

    ProjectContext need to create a prefab and put in Resources/ProjectContext.prefab

    Drag any MonoInstallers that you have added to your Scene Hierarchy to ProjectContext's Installers ReorderableList

    All MonoInstallers will be called InstallBindings() function when before scenes loaded

  • SceneContext

    SceneContext need to added to scene root gameObject

    Drag any MonoInstallers that you have added to your Scene Hierarchy to SceneContext's Installers ReorderableList

    All MonoInstallers will be called InstallBindings() function when SceneContext's gameObject Awake()

    Important you need to set SceneContext Script Execution Order high than Default Time

Clone this wiki locally