Dependency injection
How to consume services with dependency injection
In MauiReactor, it's recommended to put service implementations and models in a project (assembly) different from the one containing the app. Hot-reloading a project that contains dependency-injected services requires them to be hosted in a different assembly/project.
MAUI.NET is deeply integrated with the Microsoft Dependency injection extensions library and provides a structured way to inject services and consume them inside ViewModel classes.
MauiReactor works mainly the same except you can access services through the Services
property inside your components.
For example, let's see how to consume a simple Calc service like this (created in a class library referenced by the main MAUI project):
We, first, have to register the services during the startup of the app:
Then we can access it inside our components:
At line 38 we get the reference to the singleton service and call its Add method().
Last updated