Replies: 1 comment 3 replies
-
First of all, thanks for the suggestion @runt9. As for automatic classpath scanning and similar features, I wanted to avoid them specifically because of the reflection usage and how it complicates the internal implementation. It certainly can be done for every platform, including GWT - you can check out my previous attempt at a cross-platform LibGDX DI framework here. Fair warning though, code quality is probably much lower there, as I was still inexperienced when I made the LML libraries. I wanted to avoid reflection-based classpath scanning as it causes some problems outside of desktop applications. It basically requires custom code for each platform in order to work correctly, and some LibGDX backends (like GWT or iOS, if I recall correctly) require additional registration of classes that should be available via reflection, which kind of defeats the purpose of automatic classpath scanning in the first place. Not to mention that any third-party backends for platforms such as HTML likely won't support reflection at all, which makes porting even harder. That being said, the snippet that you have posted would be a rather simple utility that wouldn't require any additional dependencies or much setup on user side. It would have to be rewritten with LibGDX reflection API, but other than that I could see it being useful to avoid repeating too many |
Beta Was this translation helpful? Give feedback.
-
First off, thank you so incredibly much for KTX, it's made my first foray into LibGDX in my favorite language an absolute blast!
I've spent the last decade doing full-stack development in Spring and so coming to using ktx-inject for my DI has been different than what I'm used to. I'm getting ready to rewrite most (all?) of my game to prepare the code base for future growth and one of the things that ended up on my list was seeing if there was a way to make DI a bit more automatic like I'm used to in Spring.
After jumping through some Google searches, it seems like the full package/annotation scanning way would add a lot of coding time and also have some startup overhead, so I went with just seeing what I could do to make it to where I didn't have to call a real constructor with many
inject()
calls so I didn't have to go and update the DI registry every time I added or removed an injected field from a class.Here's the few lines of code I came up with and wanted to throw it out there as a potential option for ktx-inject, even though it's explicitly stated in the readme that it's reflection-free:
This makes it to where in your
injector.register {
block, you simply call something likeautoBindSingleton<ClassToBind>()
and it will always inject the correct arguments even if you change the order, add or remove some, etc.This would also open things up to a path-scanning possibility with Reflections, but that's a step I'm not sure I need right now. But I wanted to share the snippet and see if it might be useful for KTX?
I'm sure as I put this game together there will be many more snippets like this I could potentially contribute so expect to see my name around a bit :)
Beta Was this translation helpful? Give feedback.
All reactions