Single Codebase and Cross Platform - ntierontime/Log4Net GitHub Wiki

Single Codebase strategy relies on having one codebase that may be compiled to multiple platform-specific formats. One technique is conditional compilation. With this technique, code that is common to all platforms is not repeated. Blocks of code that are only relevant to certain platforms are made conditional, so that they are only interpreted or compiled when needed. Another technique is separation of functionality, which disables functionality not supported by client browsers or operating systems, while still delivering a complete application to the user. The complexities of client differentiation are hidden behind a single, unified API.

A lot of efforts already are contributed to make this dream come true:

  • JVM can be deployed to Linux/Unit and Windows, developers can develop application using Java
    • 3rd party platforms or APIs can make Java code run on iOS, e.g. RoboVM
  • .Net Platform, C# developers can write single codebase which officially support all Windows related OS.
    • 3rd party platforms or APIs can make C# run on Android, iOS and MacOS, e.g. Mono, MonoGame

Single Language and Single Codebase

C# is one of the most popular programming languages in developer world. Developers can develop all types of applications: web applications, mobile applications, desktop applications, and game applications. C# can, not only develop applications for windows OS, but also for iOS, Android, and Playstation and Wii with help from 3rd part APIs and platforms.

Front-end and back-end are terms used to characterize program interfaces and services relative to the initial user of these interfaces and services. (The "user" may be a human being or a program.)

Backend Single Language and Single Codebase

A "back-end" application or program serves indirectly in support of the front-end services, usually by being closer to the required resource or having the capability to communicate with the required resource, e.g. databases.

In n-Tier architecture, a "back-end" application includes Data Layer, Business Layer and Services Layer. Abstract efforts can be applied to these layers, e.g. IoC (Inversion of Control)

Web and desktop applications can connect to backend either directly to database or remotely via web services. Most of backend code can be re-use in Single Codebase by Single Language C#. Service Layer may need different markup, e.g. Wcf, Xml/JSON using WebApi 2 hosted in Asp.Net Mvc

Multiple Frontend Markup Languages

Frontend applications is Presentation Layer in n-Tier architecture, which separate into User Interface Components and Presentation Logic Components.

A lot of technologies are used in web, mobile, desktop and game User Interface applications development, e.g.

And best practices of user experience also vary across different types of applications:

  • Touch screen of Mobile applications.
  • Mouse and Keyboard controlled user interfaces.
  • Styles and Layouts, and even UI components vary based on size of screen, e.g. Switch control for On/Off. Presentation Logic Components can be shared across same type of technologies:
  • Mvc for Asp.Net engine and Rasor engine for Asp.Net Mvc.
  • MVVM for Wpf, Silverlight, and Windows Phone applications.
  • MVP + Data binding for Asp.Net Web Form and Windows Form Applications. So, both User Interface Components couldn’t share between different types of applciations.

Presentation Logic Components may be re-used in same type of application, e.g. mobile applications.

Applications Tree of Single Codebase

In Single Codebase systems:

  • Backend is Tree Trunk. Developer can have Single Codebase for most Backend Applications.
  • Frontends are Tree Leaves. Developer can’t have Single Codebase for most Frontend Applications. See upper cross-platform application tree. Single Language C# and Single codebase can provide:
  • Web application, Asp.Net Mvc
  • Android/IOs/Universal Windows Plaform Mobile applications using Xamarin.Forms 2.4.
  • Desktop application:
    • Standard WPF or windows form on Windows OS
    • Linux and Mac OS on top of Mono
  • Game
    • Standard XNA Xbox games on Xbox One or Xbox 360 operating system (Windows like)
    • Playstation or Wii games on top of MonoGame

Visual Studio Key Features for Single Codebase And Cross Platform

Two Visual Studio tools and one compilation feature are used the accomplish Single Codebase and Cross Platform.

Nuget can automatically get online packages and add assembly references.

Project Linker helps developer share same code file in projects for different platforms. C# #if Preprocessor Directives is used to control code snippets in shared code file to be compiled to target platforms via symbol defined in project settings.

Nuget

NuGet is a free and open source package manager for the .NET Framework. Some popular Nuget packages are used in generated C# solutions: e.g. Log4Net, AjaxControl Toolkit, MVVMLight, etc.

Please goto Nuget, get Nuget onto Visual Studio.

Project Linker

The original purpose of Project Linker is synchronizing files for WPF and Silverlight, now extended not only WinRT(Windows App Store) and Windows Phone, and any projects need share code files. This tool helps to automatically create and maintain links from a source project to a target project to share code that is common to any platform. Therefore, shared code can be written once and built for the target platform.

Please goto Project Linker in Visual Studio Gallery, get Project Linker onto Visual Studio.

Please refer Project Linker tutorial.

C# #if Preprocessor Directives

Preprocessor directives are commands that are interpreted by the compiler and affect the output or behavior of the build process.

There is some code snippets only needed in one platform, some code snippets not needed in one platform. There are some pre-defined symbols:

  • XAMARIN, IOS, ANDROID

In generated source code, which will be used together with #if to determine whether a code snippet should be compiled in one target platform.

Please refer C# Preprocessor Directives for details.