Dependency injection in akka typed

Hello Here,
I need help with regards to dependency injection in akka-typed; Aside of constructor injection, do we have any other injection type that can be used without the need for an external library? e.g (property injection).
It becoming overwhelming to have only constructor injection everywhere, especially when the project is getting bigger(code wise).

Thanks in advance.

What is the lifecycle of the dependencies and the actor?
The equivalent for actor property injection would be via sending / telling a message as long as the value passed in is thread-safe.
Are you using Scala or Java or …?
You could always use a singleton that implements one or more traits wrapping all your dependencies and default it in the constructor so it is only replaced in tests.
You could also make the constructor dependencies implicit.
Do you have some example code?
I know this is an external library, and you might have already found it, but there is a issue discussing it.

Thanks so much @sam, Im using Scala. I kind of like the option for using implicits but knowing fully that implicits are no more in scala 3, do think its a wise thing to do?

Hi @binkabir,

Implicits still exist in Scala 3, but it’s not a wise thing to do, IMO. Not for dependency injection.

It’s hard to judge without knowing your system, but if you have so many dependencies to inject that make it hard to follow, then maybe you should rethink the design.

What are you injecting? Other actors? Clients to some external systems?

If you are mainly injecting other actors, then try to instead pass then in the messages. This align better with the interaction patterns explained here.

Thanks @octonato, primarily, what I wanted to mitigate injecting was utility actors e.g emailServiceActor,
NotificationServiceActor - for publishing errors to discord and sms. Since these actors are needed in almost 90% of the other actors, I thought of how they can be injected not via the constructor.

Since those seem like they’d be instance singletons, you could have actors discover them at startup by interacting with the ActorSystem's guardian actor (the receptionist would also be viable, but in a cluster scenario I’m not aware of an easy means of preferring a local instance). Spawn an actor which needs some dependency, it asks the guardian or the receptionist and might stash incoming messages until it gets the dependency.

the receptionist approach should work, since for now Im not using Akka clusters.
Thanks so much @leviramsey