Chuck Stein

    Chuck Stein

    1 year ago
    What are the advantages of having a stateless class injected via Dagger/Hilt which houses methods that operate on input data according to business logic, vs just using a file with top-level functions for this purpose? I have an app which recommends albums to listen to from your library based on your current mood. To do this recommendation, is there any downside to just passing the data set to pick from along with the mood parameters into top-level functions? Why use a class/object if it holds no state?
    Alex Prince

    Alex Prince

    1 year ago
    the big one is dependency inversion, so your "recommend" function isn't relied on directly by, say android specific code if you wanted to do something like KMM.
    I'd probably still just use a functional interface rather than wrapping it in a class, but
    you end up depending on abstraction instead of implementation that way
    I've done something similar for commands lately, the thing calling it relies on an interface that's defined at the edge of my domain, and then composed/implemented within the domain itself ( technically that compose would be edge'y too ), and that compose function gets called from Dagger
    Chuck Stein

    Chuck Stein

    1 year ago
    If using KMM (which I do plan to in the future), what is the issue with android-specific code relying on the top-level "recommend" function, rather than an interface/class? If the function's input and output are objects in my domain (platform-agnostic), then can't the android-specific code and iOS-specific code call it just as easily as if it were behind an interface?
    Alex Prince

    Alex Prince

    1 year ago
    yea you could, you're just depending on concretes at that point ( which isn't necessarily bad to do, if you want to, in general I try to not depend on concretes), with the interface it allows you to have that seam if you need it. really
    the other nice part w/ dagger being the thing that calls compose, is any deps you need, you have and can pass into the compose there, rather than needing to pass them in at the callsite itself. That said, you could do that w/ a closure or something too and do more of a "poor man's DI" type thing if you wanted to as well, and just end up w/ the function call you want