6 likes
·
18.0K reads
5 comments
Thanks. This is much clearer than Microsoft's documentation on dependency injection in Maui.
Excellent article. This solved my resolution problem as I needed to resolve a Dependency from a c# Class library. Yes, I would like to know how to mock IDependencyService also, and would appreciate if you wrote a piece on that. Thanks.
I just figured out how to do this, it was less difficult than I anticipated. Thanks.
Hi, Julian, why, when registering singleton, are you using Instance for DeviceService, but not for AudioService?
Hi Renatas,
sorry, I only saw your comment now. Not sure what exactly you mean. In the sample repository, the AudioService isn't implemented, yet, it doesn't use the Singleton Design (anti-) Pattern. I just want to demonstrate different ways to register and use dependencies.
What matters most is that the SOLID principles are applied wherever possible. Now, the code in the samples repository isn't perfect, it's a showcase of possibilities.
Generally, any service with a public @Object or factory method can be registered as a singleton, transient or scoped dependency. Note that the term "singleton" is somewhat overloaded here, because in the context of dependency injection, it means that always the same instance will be resolved by the DI container, it doesn't mean the Singleton Design Pattern (SDP). Classes that use the SDP can still also be registered as a singleton dependency, in which case you don't register the @Object (because it's private) or a factory (because it doesn't exist), but rather the static singleton instance.
I hope this clears it up a little.
Cheers Julian