Hi all,
Just published the latest version of my dependency container for Haxe similar to the .NET container or Google’s Wire, creatively named Haxe-Injection:
I have been using and developing this container as part of a future modular application framework. Please use it and feel free to highlight any bugs or suggested features here.
What does it do?
It allows you to quickly and easily inject dependencies between different classes and build complex dependency trees, rather than writing them out by hand. It is an alternative to using static references and supports Singleton
, Transient
, and Scoped
dependencies.
Several code examples can be found within the unit tests for the container
Hopes and dreams
I would like to get some aspects of the container moved into a macro so that dependencies can be injected at compile-time, essentially how Wire does it – this would allow for missing dependencies to be caught at compile-time and would allow for faster startup.
More importantly, by having a DI container be part of the Haxe ecosystem we can better leverage the SOLID principles and transform the development landscape away from hiding platform side-effects inside of functions, like this:
public function myFunction() : Int {
#if js
return 5;
#elseif cpp
return 8;
#else
return 3;
#end
}
which results in a cluttered and rigid codebase, into something like this:
public static function main() {
var collection = new ServiceCollection();
collection.addService(ServiceType.Singleton, IStartup, MyApplication);
#if js
collection.addService(ServiceType.Singleton, IWindow, JavascriptWindow);
#else
collection.addService(ServiceType.Singleton, IWindow, NativeWindow);
#end
var provider = collection.createProvider();
var app = provider.getService(Startup);
app.run();
}
which propagates all of the platform-specific information to the very root of your program, and leads to a cleaner and more extensible codebases.