Skip to content

Danand/SimpleContainer

Repository files navigation

SimpleContainer

MIT license

The point is the usage of DI-container in a proper way.

How to install

From remote repository

Add in Packages/manifest.json to dependencies:

"com.danand.simplecontainer": "https://github.com/Danand/SimpleContainer.git?path=SimpleContainer.Unity/Assets#0.7.1-beta.4",

From local path

From local repository
"com.danand.simplecontainer": "file:///D/repos/SimpleContainer/.git?path=SimpleContainer.Unity/Assets#0.7.1-beta.4",
From local working copy
"com.danand.simplecontainer": "file:D:/repos/SimpleContainer/SimpleContainer.Unity/Assets",
What is the difference?

Local repository is resolved just like normal Git repository with optionally specified revision.
Local working copy is being copied "as is" into dependent project, without running any Git process.

How to use

  1. Add UnityProjectRoot component to any object in hierarchy.
  2. Reference SimpleContainer.Core and SimpleContainer.Unity assemblies to your installer assembly via Assembly Definitions).
  3. Implement your own MonoInstaller and add it as component too (example project below).
  4. Reference MonoInstaller to UnityProjectRoot via inspector.
  5. Run.

Best practices

  1. Do not use DontDestroyOnLoad() to pass container through different scenes. Use LoadSceneMode.Single for "immortal" objects (e.g. managers, services, etc.), and LoadSceneMode.Additive for presentation layer (e.g. gameplay, UI, etc.). Quite good alternative is to use separate UnityProjectRoot per each loaded scene.
  2. Prefer constructor injection over any other. Yeah, that means less usage of MonoBehaviour dependants.
  3. Do not put dependencies on container. It will be perfect, if you did not have any using SimpleContainer; in your needy classes.
  4. Use custom Inject attribute (example below).
  5. You may move your installers to a separate assembly (via .asmdef) for stronger container usage protection.
  6. If you really want to inject into MonoBehaviour without explicit registration – use MonoRegistrator.

Example

See example project included into this repository. Also you may open it with Unity and run.

How to contribute

See contribution note.

Roadmap

  • Clean up repository
  • Set up CI
  • Optimize resolving
  • Refactor container interface