Xamarin Essentials is a great recent initiative from the Xamarin team to provide an endorsed, comprehensive cross-platform set of APIs in a single package (read more about it on the docs page). One interesting aspect of the library is the lack of interfaces; all APIs provided by Essentials are exposed through static methods, members and events. This is a deliberate design decision - there are size, performance, simplicity and maintenance considerations that the team made for this - but if you use dependency injection you'll probably want some interfaces, and the official guidance is to create your own.
If you're like me, you get most value from your interfaces matching the core features you'll be injecting and mocking - in which case they'll map straight to the APIs surfaced by Essentials, given that they themselves abstract the native platform functionality. I figured I'd post the ones I've generated and hopefully save someone the trouble of making their own.
If there's enough interest, pinning a .Interfaces package to matching versions of the core Xamarin.Essentials package might be a good idea. Hit me up on twitter if that's interesting to you.
Update: I'm generating packages here. Note that the usage is slightly different to the below - packages contain implementation classes for each interface, rather than a combined implementation for all classes, so use the registration technique described in the readme rather than the below.
The below gist contains a set of interfaces under Xamarin.Essentials.Interfaces
, and an implementation EssentialsImplementation
under Xamarin.Essentials.Implementation
that forwards calls to the static Xamarin.Essentials members. In your favourite DI container, you'll register using something like builder.RegisterType<EssentialsImplementation>().SingleInstance().AsImplementedInterfaces()
, and then use your IAccelerometer
, IAppInfo
, IBattery
, or other friends in constructors and mocks as needed.
Caveats: This code is generated off the source and I haven't used every one of these in my own apps - so there is no warranty provided! Additionally, I haven't checked whether the mono linker plays nice with this. Beyond that, the Essentials library itself is still in preview.
If you're still down to give it a shot - Happy injecting!
Latest: Xamarin.Essentials 0.8.0-preview (#26c14c7)
Interfaces for previous versions of Essentials:
- Xamarin.Essentials 0.6.0-preview (#480cac0)