Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Zenject Example SubContainer
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
いも
September 23, 2019
Programming
14k
1
Share
Zenject Example SubContainer
Unite Tokyo 2019 Eve2 LT Fes の資料です
いも
September 23, 2019
More Decks by いも
See All by いも
UnityプログラミングバイブルR6号宣伝&Unity Logging小話
adarapata
0
630
Unityテスト活動のふりかえり
adarapata
1
650
Gather.townはいいぞ その後
adarapata
1
1.7k
Unityでの開発事例
adarapata
3
23k
どこのご家庭にもあるシーンマネージャーの話
adarapata
2
8.7k
Gather.townはいいぞ
adarapata
2
2.4k
宴はいいぞ
adarapata
0
2.1k
わかった気になるモブプログラミング
adarapata
1
170
モブワークっぽいのをやっている話/Trying mobwork
adarapata
2
1.3k
Other Decks in Programming
See All in Programming
ECR拡張スキャンでSBOMを収集して サプライチェーン攻撃の影響調査を 爆速で終わらせてみた
akihisaikeda
2
210
Migrations : C'est une question d'hygiène !
vinceamstoutz
0
2.5k
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
190
Transactional Change Stream Processing With Debezium and Apache Flink
gunnarmorling
1
140
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
110
新規プロダクトを高速で生み出すハーネスエンジニアリング
seanchas116
17
7.6k
Zod v4 Codec でスキーマに型変換を埋め込む REST API 設計 #TSKaigi2026
ryutaro_yako
0
180
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
11k
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
4
980
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
11
3.1k
プラグインで拡張される Context をtype-safe にする難しさと設計判断
kazupon
2
480
New "Type" system on PicoRuby
pocke
1
390
Featured
See All Featured
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
370
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
YesSQL, Process and Tooling at Scale
rocio
174
15k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
23k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
210
Writing Fast Ruby
sferik
630
63k
Marketing to machines
jonoalderson
1
5.3k
BBQ
matthewcrist
89
10k
Music & Morning Musume
bryan
47
7.2k
The untapped power of vector embeddings
frankvandijk
2
1.7k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
540
Transcript
Zenject Example ~Container の分割編~ @ いも 2019/09/23 Unite Tokyo 2019
Eve2 LT Fes
いも twitter: @adarapata ゲーム作ったりしてます 2019/09/23 Unite Tokyo 2019 Eve2 LT
Fes
宣伝 「Zenject チョットワカルBook 」を書きました ↓Booth 2019/09/23 Unite Tokyo 2019 Eve2
LT Fes
今日話すこと Installer 肥大化問題 Zenject のSubContainer について Zenject チョット触り始めてる人向け 2019/09/23 Unite
Tokyo 2019 Eve2 LT Fes
Installer 肥大化問題 public class SomeInstaller : MonoInstaller { public override
void InstallBindings() { // Player 関連のBind Container.Bind<Player>().AsCached(); Container.Bind<PlayerDependentA>().AsCached(); // Player しか依存してない Container.Bind<PlayerDependentB>().AsCached(); // Player しか依存してない ~~~ Container.Bind<PlayerDependentN>().AsCached(); // Player しか依存してない // Enemy 関連のBind Container.Bind<Enemy>().AsCached(); Container.Bind<EnemyDependentA>().AsCached(); // Enemy しか依存してない } } 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
Installer 肥大化問題 1 シーンに必要なものがそれなりに大きくなると起きる問題 あらゆる依存関係が1 シーンに刻まれる Scene のInsaller が細かい依存関係を知ることになる 不要な衝突も発生する可能性が出てくる
2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
不要な衝突が発生する public class SomeInstaller : MonoInstaller { public override void
InstallBindings() { // Player 関連のBind Container.Bind<Player>().AsCached(); Container.Bind<PlayerDependentA>().AsCached(); // Player しか依存してない Container.Bind<Life>().AsCached(); // Player のLife を表現したい // Enemy 関連のBind Container.Bind<Enemy>().AsCached(); Container.Bind<EnemyDependentA>().AsCached(); // Enemy しか依存してない Container.Bind<Life>().AsCached(); // Enemy のLife を表現したい } } 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
不要な衝突が発生する 回避できないことはないがちょっと厳しい Container を分けてスマートにしたい 2019/09/23 Unite Tokyo 2019 Eve2 LT
Fes
やること SubContainer の利用 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
Container の階層関係 Container は階層関係を持つことができる まず自身のContainer を探して、解決できないなら親のContainer を 探す 親のContainer で見つからなかったらその親を~を繰り返す
ProjectContext がどこでもInject できるのは、このContainer の最上 位にいるから 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
Container の階層関係(Parent Contract の場合) Context を境界にContainer が繋がっている 2019/09/23 Unite Tokyo
2019 Eve2 LT Fes
1Context の内部だけでContainer の階層関係を作ることもできる 子にあたるContainer のことをSubContainer と呼ぶ 2019/09/23 Unite Tokyo 2019
Eve2 LT Fes
SubContainer にPlayer が必要とする要素をまとめてしまえば、 Container 内部で衝突しない 2019/09/23 Unite Tokyo 2019 Eve2
LT Fes
FromSubContainerResolve public class SomeInstaller : MonoInstaller { public override void
InstallBindings() { // SubContainer から解決する Container.Bind<Player>().FromSubContainerResolve() .ByMethod(InstallSubContainer); // Enemy 関連のBind } // SubContainer にInstall させる public void InstallSubContainer(DiContainer subContainer) { subContainer.Bind<Player>().AsCached(); subContainer.Bind<PlayerDependentA>().AsCached(); subContainer.Bind<Life>().AsCached(); } } 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
FromSubContainerResolve Bind するオブジェクトをSubContainer から探して解決する 今回だとSubContainer からPlayer を探して解決する このメソッドの後には「何でSubContainer を作るか」のBy~~ メソッ
ドを定義する ByMethod メソッドを呼んでInstall する ByInstaller Installer<T> なクラスを呼んでInstall する ほかにもいっぱいある 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
Installer で解決する場合 public class SomeInstaller : MonoInstaller { public override
void InstallBindings() { // SubContainer にInstaller でInstall して解決する Container.Bind<Player>().FromSubContainerResolve() .ByInstaller<PlayerInstaller>().AsCached(); // Enemy 関連のBind } } public class PlayerInstaller : Installer<PlayerInstaller> { public override void InstallBindings() { Container.Bind<Player>().AsCached(); Container.Bind<PlayerDependentA>().AsCached(); Container.Bind<Life>().AsCached(); } } 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
Player をSubContainer から引っ張って解決する Life PlayerDependentA はContainer からは見えない Container 単位で役割をグルーピングしやすい 2019/09/23
Unite Tokyo 2019 Eve2 LT Fes
GameObjectContext public class PlayerBehaviour : MonoBehaviour { [Inject] PlayerDependentA depA;
[Inject] Life life; } ↑ こういうのをSubContainer でBind したいとする SubContainer にMonoBehavior なオブジェクトをBind する場合、 GameObjectContext をアタッチする必要がある 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
GameObjectContext SceneContext ProjectContext と同じContext の一種 Context はInstall を行うエントリポイント それぞれInjection のスコープが違う
ProjectContext: ゲーム全体 SceneContext: シーン内 GameObjectContext: アタッチされたGameObject 以下 SceneContext のContainer にBind するとシーン全体にInject されるの で、GameObjectContext をSubContainer として狭い範囲へのInject を 行える 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
MonoBehavior でない場合 public class SomeInstaller : MonoInstaller { public override
void InstallBindings() { // SubContainer にInstaller でInstall して解決する Container.Bind<Player>().FromSubContainerResolve() .ByInstaller<PlayerInstaller>().AsCached(); } } public class PlayerInstaller : Installer<PlayerInstaller> { public override void InstallBindings() { Container.Bind<Player>().AsCached(); Container.Bind<PlayerDependentA>().AsCached(); Container.Bind<Life>().AsCached(); } } SubContainer の作り方が、直接Installer を指定する形になる 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
MonoBehaviour の場合 いつものSceneContext のノリで対象のPrefab にアタッチする 2019/09/23 Unite Tokyo 2019 Eve2
LT Fes
MonoBehaviour の場合 public class SomeInstaller : MonoInstaller { [SerializeField] private
GameObject _playerPrefab; public override void InstallBindings() { // GameObjectContext のついたPrefab を生成してInstall する Container.Bind<PlayerBehaviour>().FromSubContainerResolve() .ByNewContextPrefab(_playerPrefab).AsCached(); } } public class SubInstaller : MonoInstaller { public override void InstallBindings() { // Zenject Binding をアタッチするのもアリ Container.Bind<PlayerBehavior>().FromComponentOnRoot(); Container.Bind<PlayerDependentA>().AsCached(); Container.Bind<Life>().AsCached(); } } 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
Context を跨いで同じことが行われる 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
まとめ 1 つのContainer にどんどんBind していくと衝突や可読性の問題が現 れる SubContaienr に分離してスコープを狭めよう MonoBehaviour の場合はGameObjectContext
を使おう MonoBehaviour でもPureClass でもSubContainer の考え方は変わら ない。 Context があるかどうかだけ 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes
おわり 2019/09/23 Unite Tokyo 2019 Eve2 LT Fes