Search code examples
mvvmmvvm-lightuno-platformshared-project

Where should most of my platform specific code be in MVVM? (Beside View)


I'm Using Uno-Platform with MVVMLight trying to structure my project. Should I put platform specific code inside ViewModel? or should it mostly be in Utils/Services?


Solution

  • I would recommend placing platform-specific code inside utility classes or services. Placing that code inside ViewModels would make it harder to reuse and potentially make the ViewModel impossible to run tests against.

    Ideally, (not mandatory) you'll want to use Interfaces and dependency injection, provide one implementation for each platform you want to support, and try to implement the same interface for all of them. That way, your ViewModel will not have #if PLATFORM_A code.

    Side note, remember Uno platform already offers a lot of platform abstractions through the UWP APIs. Like Geolocator, sensors etc.