Registering MAUI Essentials interfaces for MAUI

Viewed 128

Now that MAUI gives us interfaces for all the Essentials APIs I'm trying to use them in my code to make things more testable.

In this video James says we should be registering them as:

builder.Services.AddSingleton<IGeolocation, GeolocationImplementation>();

but the implementation classes are all internal now so they can't be registered this way.

Is the correct way now to use the Current or Default props on the classes as below?

builder.Services.AddSingleton<IGeolocation>(ctx => Geolocation.Default);
builder.Services.AddSingleton<IConnectivity>(ctx => Connectivity.Current);

Thanks

1 Answers

There have been a lot of changes following up fast after one another. James' video was probably correct at that time, but the implementation in .NET MAUI was then changed.

The correct way, as you indicate, is to do it as:

builder.Services.AddSingleton<IGeolocation>(ctx => Geolocation.Default);
builder.Services.AddSingleton<IConnectivity>(ctx => Connectivity.Current);
Related