question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Service dependencies in systems

See original GitHub issue

Hi,

What is the best way to pass services into an Entitas systems? There is a discussion in #609 with some pros and cons that @sschmid have point out but still could not decide on what ways to do it.

  1. Singleton, like match-one example

  2. Using a meta context to pass registered service to system. Just like the Game Architecture With Entitas in the wiki tutorial made by @FNGGames

  3. Use dependencies injection technique like Zenject.

Cheers

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
kdrzymalacommented, Apr 26, 2018

Hey, here’s my 2c.

  1. Singleton. Yep, it’s singleton, we all know this one all too well 😃 Not testable, poor maintainability and modularity. In particular: it’s not easy do IMyService and then swap implementations with singletons, which can be really useful.

  2. Kind of reminds me of the service locator antipattern. Much better than singleton, but still has issues. What if service A depends on B and B depends on C? You have to manually construct the objects in the correct order. Small change ( B no longer depends on C but on D) can result in need to rewrite stuff. Rewriting stuff -> potentially introducing bugs.

  3. This is what we do and IMO Entitas works sweet with Zenject. You don’t need to worry on the correct order of initializing things - Zenject keeps track of the dependency graph and constructs everything in the correct order.

This is how we glue Entitas and Zenject together. You need an InjectableFeature:

https://gist.github.com/kdrzymala/23d37c745377957cf9e63aeb55ceb6b5

Then in your GameController you do:

[Inject]
DiContainer _container;
[Inject]
Contexts _contexts;
void Start()
{
  _systems = new InjectableFeature( "Root system" );
  CreateSystems( _contexts ); // all your _systems.Add(new ASystem); goes here
  _systems.IncjectSelfAndChildren( _container );
  _systems.Initialize();
}

And that’s it. Magic can now happen 😃 You can use [Inject] in your systems now. We abstract quite a few things that way: saving (we have a couple strategies that can be swapped), logging, analytcs (mutliple providers), ads (same as analytics), object pooling, etc.

Also - you no longer need to use Contexts.sharedinstance (which is a singleton) - you can inject Contexts into your MonoBehaviours (this is where you’d normally use Contexts.sharedinstance).

Zenject and Entitas are the top 2 awesome things happened to me in the past 2-3 years 😃 Together they help to create loosely coupled, testable and maintainable code.

0reactions
YimingIsCOLDcommented, Apr 27, 2018

@kdrzymala Okay. Zenject documentation is really awesome and with the code snippet you shared, I had already setup and started using Zenject.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Service Dependency - an overview | ScienceDirect Topics
In this section, we'll review some of the network client roles and operating system services required for the ISA firewall to fulfill those...
Read more >
How to add dependency on a Windows Service AFTER the ...
You can add service dependencies by adding the "DependOnService" value to the service in the registry using the regedit command, ...
Read more >
Service Dependencies
Dependencies are the services that the current service is dependent upon while Dependents is the reverse. An alert icon will be visible on...
Read more >
How to identify and map service dependencies - Gremlin
Developers can identify dependencies by reviewing their code for network requests to other services or that include third-party libraries ...
Read more >
Automatic service start up and service dependency
When Kiwi CatTools is installed on a server as a service, the program is set to start automatically when the system is restarted...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found