Posts

Showing posts from November, 2025

ViewModel Factory (Old vs. Newer DSL)

Image
Old vs. New IMPORTANT If you use big Dependency Injection frameworks like Hilt or Koin, you can skip this. But if you're a fan of cleaner, boilerplate-free code in smaller projects, or just hate writing ViewModelProvider.Factory classes, you'll want to see this massive improvement in how we handle ViewModel injection. Important Note: Please see this newer blog post ViewModel Factory (Class Instances)ViewModel Factory (Class Instances)  when using class instances as arguments. Summary I used to use an older-style Factory method to create my ViewModel which I have now moved away from completely. I'll show you my current pattern using DSL.  Situation I tend to avoid Dependency Injection frameworks in my project as generally they are quite small. I was often able to create ViewModels without needing any external input and so things were quite easy. When I did need to inject something into my ViewModel, I simply used a Factory method. This was no big deal and it was quite easy t...