2006-07-25

Creating a hybrid Class Library

This post explains how to create a hybrid Class Library: a Class Library that is used by both a desktop application (regular .NET Framework) and a mobile application (Compact Framework).

I'm currently working on a mobile version of my Calctor application. To make it easier to maintain, I decided to refactor the base functionality into a class library that I could reference in both the desktop version and the mobile version. The refactoring part was easy, but when I tried to add a reference from the device project to the class library, I got an error message that you can't do that. Which actually makes sense: the device project uses the Compact Framework, which contains only a subset of the BCL. So I asked how to solve this on Channel 9. As I expected, I soon got a useful answer from Sven Groot. After trying out several options, I ended up with the following solution.

This works out great, you can compile and debug just fine. One disadvantage is that you can only use the subset of BCL functionality that is in the Compact Framework, but that is only logical. Another disadvantage: if you have all projects (desktop and mobile) in 1 solution, and you're trying to debug the desktop application, the mobile project(s) will also be built and deployed, which slows down the building considerably. So if you're debugging only one of the application projects, I suggest you unload the other application project(s) that are not referenced. Just right-click the project, and choose Unload project. Later, you can just reload it by right-clicking the disabled project and choosing Reload project.

Comments:
In my own VB.NET solution, I could not add a Project reference from my desktop project to the Compact class library. Instead, I added a file reference. This works great, until I update the class library. Visual Studio builds the class library, but does not automatically refresh the desktop project's copy of the class library output. To work around this, I added an XCOPY to the desktop project's "Build Events" (located on project properties' Compile tab).
 
I thank you for your comment.
 
Post a Comment

Links to this post:

Create a Link