65848

Choose interface implementation in universal app depending on platform

Question:

In my universal app I have a MainViewModel that I share between both the windows and the Phone projects. Depending on what project is run, I would like the MainViewModel to implement a different interface. Lets say these are my two classes and the interface that they both implements:

public class PhotoPhone : IPhotograph { //some methods } public class PhotoWin : IPhotograph { //some methods } public interface IPhotograph { //some signatures }

And here is my MainViewModel:

public class MainViewModel : ViewModelBase { private IPhotograph _phot; public MainViewModel(IPhotograph photo) { // Depending on what platform is running //I want the proper implementation } }

But how can I make my MainViewModel implement the correct implementation of IPhotograph? I'm thinking that maybe some code should be added to my App.Xaml file. Is this maybe the wrong way to go about this issue? I would like to learn the proper way by following MVVM as far as possible.

Answer1:

Two words: dependency injection. Basically, you want your view model to be as ignorant of the details of phone or desktop/laptop or web environment as the rest of the code, so you need to have a different component handling which implementation of IPhotograph is actually instantiated. This component is called the dependency injector; there are a lot of frameworks in .NET for this, like StructureMap, Spring.NET, Ninject, Autofac, Unity, Castle Windsor, and so forth.

In Unity, you'd probably set up a pair of configuration files, one for phone and one for Windows, and register types in those, and in particular register different implementations of IPhotograph in each. Then make sure the only part of your application that knows about Unity or any concrete types is the initialization point. Seems like you've already gotten that mostly done, using interfaces for most things.

Recommend

  • Choosing between bound ViewModel properties or messaging to communicate between ViewModel and View u
  • Wix doesn't remove previous version of burn exe during major upgrade
  • UserControl DataContext Binding
  • Binding UserControl in XAML
  • MvvmLight in Portable Class Library - How to do Unit Testing
  • iOS: How to convert the self-drawn content of an UIView to an image (widespread general solution ret
  • Extending enums
  • Should this be giving a compile error?
  • Intermediate and return values in continuation-passing style
  • Observable.forkJoin() TS2322 error after updating to TypeScript 2.4.1 and Rxjs 5.4.2
  • On which thread JavaFX change listeners are executed ?
  • Consuming a web service with the Netbeans Platform
  • What's the syntax to inherit documentation from another indexer?
  • How to use Windows Media Foundation with UWP without a topology
  • abstracting over a collection
  • Can't remove headers after they are sent
  • Center align outputs in ipython notebook
  • Implicit joins and Where in Doctrine - how?
  • C++ pointer value changes with static_cast
  • Web.config system.webserver errors
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • x64 applications using gdi+: what are the consequences on performance?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • output of program is not same as passed argument
  • Javascript Callbacks with Object constructor
  • Javascript + PHP Encryption with pidCrypt
  • ActionScript 2 vs ActionScript 3 performance
  • How can I estimate amount of memory left with calling System.gc()?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Apache 2.4 - remove | delete | uninstall
  • 0x202A in filename: Why?
  • Proper folder structure for lots of source files
  • Getting Messege Twice Using IMvxMessenger
  • File not found error Google Drive API
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • How can I use threading to 'tick' a timer to be accessed by other threads?