Skip to content

Instantly share code, notes, and snippets.

@AxGord
Last active June 4, 2023 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AxGord/2b65e46fa3dd69e2ef0f6720fa9b6ade to your computer and use it in GitHub Desktop.
Save AxGord/2b65e46fa3dd69e2ef0f6720fa9b6ade to your computer and use it in GitHub Desktop.
Dependency injection
import haxe.Timer;
import pony.Logable;
import pony.magic.WR;
import pony.magic.DI;
// https://github.com/AxGord/Pony/commit/a0c68a0f5d92798b2054fd39dc9e370c0977f2a4
class Main implements DI {
@:service private final l: Logable = new Logable('Module:');
@:service private final myService: MyService = new MyService();
@:service private final cat: User = new User('Cat');
@:service private final dog: ModUser = new ModUser('Dog');
// public function destroy(): Void trace('Complete');
private static function main(): Void Main.create(main -> main.destroy());
}
class ModUser extends User {
@:service private final l: Logable = new Logable('ModModule:');
@:service private final myService: MyService = new MyService();
public function new(name: String) super('$name!');
}
class User implements DI {
@:service private final myService: MyService;
public function new(name: String) myService.print('Hello $name');
}
class MyService implements DI implements WR {
@:service private final l: Logable;
public function new() {
Timer.delay(init, 500);
}
private function init(): Void {
l.traceAll();
ready();
}
public function print(text: String): Void l.log(text);
}
@AxGord
Copy link
Author

AxGord commented May 22, 2023

Output:

04:29:20.648 src/Main.hx:49: Module: Hello Cat
04:29:20.648 src/Main.hx:49: ModModule: Hello Dog!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment