Patterns for sharing code in Windows Phone and Windows 8 Applications

Background

With the latest batch of releases in Windows Phone 8 and Windows 8 Microsoft has been trying to close the gap between both development platforms. (The old “write once, run everywhere”-idea)
Although both platforms are based on the same Windows 8 kernel, there certainly are quite a few differences.

One key piece of interoperability between the platforms is the concept of a portable class library (or PCL). A portal class library is a component that you can link from both Windows Phone apps and Windows Store applications. Since both platforms are inherently different though, this does not allow you to code everything in the PCL.

What you can do essentially in a portable class library, is target everything that IS common between both platforms, for example:

You CAN write your validation logic because this is just plain .NET code.

You CANNOT write code against the File APIs, since both platforms have a different API for accessing the file system.

To solve this problem and to make sure that we only have to write our code once we will have to separate the implementation from the interface.

Example: unifying file system API’s

In this example I will show you how you can separate the differences and implement the common logic in one place. Since both platforms have different file system API’s, that’s where we’ll have to separate the differences. In order to do so we will be creating one interface that represents the common file operations: SaveFile and **OpenFile. **Next we will write a StorageManager that consumes this interface. In both client apps we will create an implementation of the interface so that the StorageManager can be reused on both platforms. This diagram illustrates the concept:

[![image](/content/images/2013/02/image_thumb1.png "image")](/content/images/2013/02/image1.png)Now we can implement the access to both API’s in different classes and project them onto an Interface.

Note: Although the code for both APIs is really similar, you are required to write it inside either a Windows Phone project or a Windows 8 project. That means you can’t access it from your PCL, and thus you can’t write anything in your PCL that relies on storage. However, since our interface lives in the PCL we can consume the interface from within the PCL.

With these items in place we can now write a generic StorageManager that talks to the interface. Depending on the application we will pass in either a Phone8StorageProvider or a Win8StorageProvider. In the StorageManager we could for example (de)serialization of objects:

For the sake of simplicity I have just included the basic idea in this class. You notice though how at this point the code is not focused on how to get the contents anymore but more on what to do with it.

Conclusion

In this example I showed how we can project two inherently different API’s onto the same Interface and then talk to that interface. This assures you that your logic does not rely on platform specifics. In order to do so we used the Adapter pattern:

We adapt the Windows 8 and the Windows Phone 8 File API to our interface. This is useful in a number of other cases as well such as when you want to work with a timer in a PCL.