My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Julian Ewers-Peters

6 likes

·

18.0K reads

5 comments

Simon O'Rorke
Simon O'Rorke
Dec 31, 2023

Thanks. This is much clearer than Microsoft's documentation on dependency injection in Maui.

·
Richard Lavallee
Richard Lavallee
Feb 6, 2024

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.

·
·1 reply
Richard Lavallee
Richard Lavallee
Feb 6, 2024

I just figured out how to do this, it was less difficult than I anticipated. Thanks.

·
Renatas Laužadis
Renatas Laužadis
Mar 3, 2024

Hi, Julian, why, when registering singleton, are you using Instance for DeviceService, but not for AudioService?

·
·1 reply
Julian Ewers-Peters
Julian Ewers-Peters
Author
·Mar 28, 2024

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

·