Thursday, September 20, 2007

Write C# code for both full .NET Framework and .NET Compact Framework

C# is fully supported on both the full .NET Framework and .NET Compact Framework and there are many times when a piece of code could have been used on both the "full framework" and "compact framework. A simple example would be the Business Logic as this would normally be the same in an enterprise project, whether the solution would be deployed on any of these platforms. I have attempted to write a simple application that addresses this issue, and I am sure there exist other ways to do this - is a faithful friend :).

The Visual Studio Solution: FrameworkApps
There are three projects in the solution, and they are:

1. BusinessLogic, a class library project, that implements the business logic;
2. DesktopApp, a desktop application on the full .net framework platform;
3. DeviceApp, a device application on the .net compact framework platform.

The BusinessLogic.cs file is used by both the desktop and device application. Thus, any source files hould be added as links to the projects, to avoid duplicate source files.

Here is a snapshot of the project structure:

Conditional compilation
Conditional branching of the code at compile time is used to determine the code to be compiled during the compilation process. To achieve this, go to the desktop project properties and under the Build tab add a new "Conditional compilation symbol", say FULL_FRAMEWORK.

Project: BusinessLogic
Source file: BusinessLogic.cs

Project: DesktopApp
Source file: Form.cs

Project: DeviceApp
Source file: Program.cs

Set DesktopApp as the startup project and run the application:

Set DeviceApp as the startup project and run the application:

Hope it helps.

No comments: