Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Microsoft-Compatible APIs in Mono 2.4

Mono is a cross-platform development framework that allows developers to build Linux apps with improved productivity.


advertisement
Mono is an open source project led by Novell to create a .NET-compatible set of tools that include, among others, a C# compiler and a Common Language Runtime. Mono can be run on Linux, BSD, UNIX, Mac OS X, Solaris, and Windows operating systems.

Mono's current version is 2.4 (as of March 30). This version provides the core API of the .NET Framework, as well as support for Visual Basic.NET and C# versions 2.0 and (partially) 3.0. LINQ to objects and XML is part of the distribution, but not LINQ to SQL. C# 3.0 is now the default mode of operation for the C# compiler. Windows Forms 2.0 is also now supported.

Implementation of .NET Framework 3.0 is under development under an experimental Mono subproject called "Olive", but the availability of a Mono framework supporting .NET 3.0 is still not planned yet. In Mono 2.4 release few major milestones have been achieved, specially introducing advanced Microsoft Compatible API's.



Mono 2.4 and Microsoft-Compatible APIs

In the following sections I will discuss and show you implementation of these APIs.
    * ADO.NET 2.0 API for accessing databases.

    * ASP.NET 2.0 API for developing Web-based applications.

    * Windows.Forms 2.0 API to create desktop applications.

    * System.XML 2.0: An API to manipulate XML documents.

    * System.Core: Provides support for the Language Integrated Query (LINQ).

    * System.Xml.Linq: Provides a LINQ provider for XML.

    * System.Drawing 2.0 API: A portable graphics rendering API.

ADO.NET 2.0 API for Accessing Databases

For accessing database the Mono Framework has Mono.Data.dll assembly. This assembly has all the classes and Data Tools for Mono ADO.NET database connectivity. This is the abstract data provider access within Mono. Mono.Data.dll provides ability for .net 1.0, .net 1.1, .net 2.0, mono 1.0, and mono 1.1. Like ASP.NET application Mono's web application supports configuration files & following are the components of the configurable sections of a configuration file related with Database connection.
    * ProviderFactory Class is used to create new Connections, Commands, DataAdapters, or Commands. All objects are returned using the provider interfaces such as IDbConnection, IDbCommand, IDbDataAdapter, or IdataParamter.

    * DataTools contains Static methods for doing common tasks like filling a Dataset with the contents of a select statement.

    * ProviderCollection is the List of providers configured in the system. Initially loaded from app.config, but can be modified at run-time.

    * Provider represents a given provider (factory) and holds information needed to create the types.

    * ProviderSectionHandler is used for loading the list of providers from the app.config into a ProviderCollection.

To see an example of a configuration file with a different connection string and provider information, see Listing 1. Here's an example of C# source code for creating a connection with SQL Server database using Mono:

IDbConnection conn; String connectionString = "Factory=System.Data.SqlClient;" + "Server=speedy;database=pubs;uid=sa"; conn = ProviderFactory.CreateConnection(connectionString);

Here's a C# sample for creating a dataAdapter and filling a dataset.

IDbConnection conn = ProviderFactory.CreateConnectionFromConfig("testConnectionString"); IDbCommand cmd=conn.CreateCommand(); cmd.Text="select * from author"; DataSet ds=new DataSet(); IDbDataAdapter adapter=ProviderFactory.CreateDataAdapter(cmd); adapter.Fill(ds, "Table1");

Sample C# code for displaying a list of configured ADO.NET providers:

Console.WriteLine("Configured Providers:"); foreach (Provider p in ProviderFactory.Providers) Console.WriteLine(p.Description);

ASP.NET 2.0 API for Developing Web-based Apps

Mono Framework also provides API's for developing Web Based application like ASP.NET.Mono supports ASP.NET 2.0, ASP.NET AJAX and a handful of 3.5 controls.ASP.NET applications can be executed using Mono, developed mono application supports 3 (three) types of application/web hosing options:
    * For Apache hosting Use mod_mono a module that allows Apache to serve ASP.NET applications.

    * For FastCGI hosting Use the FastCGI if you have a web server that supports the FastCGI protocol for extending the server.

    * XSP is a simple way to get started with Mono hosting, a lightweight and simple web server written in C#. XSP supports SSL and TLS Client Certificates.
Advanced users can also use the HttpListener and the ASP.NET hosting to create their own hosts for ASP.NET applications.

Mono Web Applications can be configured through the web.config file. Additionally, developers can configure Mono-specific ASP.NET settings using the ASP.NET_Settings_Mapping engine. While debugging to obtain line numbers in stack traces & debug the application you need to do two things:

1. Enable Debug code generation in your page. 2. Run Mono with the debug command line option.

You must enable debug code generation in your page using the Debug="true" in the top of your page, or setting the compilation flag in Web.config (compilation option). Use the --debug command line option to Mono, this is done by setting the MONO_OPTIONS environment variable, For example:

$ MONO_OPTIONS=--debug xsp2 Listening on port: 8080 (non-secure) Listening on address: 0.0.0.0 Root directory: /tmp/us Hit Return to stop the server.

Windows.Forms 2.0 API to Create Desktop Apps

For Graphical User Interface development, Mono has System.Windows.Forms namespace this supports Winforms 1.1 and 2.0. System.Windows.Forms in Mono is implemented using System. Drawing. Mono developers GUI has a designed where developers can design, execute windows form based applications.

Figure 1: Here's the Mono Graphical User Interface for desktop-based application development.

To start development work the following package and library is required: Mono package and libgdiplus library. Not required for Windows XP.

The following is a C# example of a Form based application.

using System; using System.Drawing; using System.Windows.Forms; public class TestApplication : Form { static public void Main () { Application.Run (new TestApplication  ()); } public TestApplication  () { Button b = new Button (); b.Text = "Test Application!"; b.Click += new EventHandler (Button_Click); Controls.Add (b); } private void Button_Click (object sender, EventArgs e) { MessageBox.Show ("You have Clicked me!"); } }

LINQ to SQL

Mono Provides System.Data.Linq.dll that supports collaboration of the DbLinq. Mono's LINQ to SQL is not limited to SQL server. System.Data.Linq.dll can be used to many other database providers by specifying the database provider on the connection string. See the following examples. (This is not a C# sample code; you can treat this code snipped as pseudo code).

variable db = new DataContext("DbLinqProvider=Sqlite;" +    "DbLinqConnectionType=Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite, " +        "Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756; " +    "Data Source=Northwind.db3");

DbLinqConnectionType can be skipped if you provide your own IDbConnection. See the pseudo code.

variable conn = new SqliteConnection (    "DbLinqProvider=Sqlite;" +    "Data Source=Northwind.db3" // Name of the database ); var db = new DataContext (conn);

System.XML 2.0: An API to Manipulate XML Documents

System.XML.dll assembly is provides the entire API's and namespaces for creating, editing and manipulating XML files.XML had DOM (Document Object Model) parser for working with XML. Like ASP.NET Mono also provide following Classes for manipulating XML object.

    * Xml Writer is almost equivalent to XmlTextWriter of Microsoft .NET used for Creating XML documents.

    * The XmlSecureResolver, which is introduced in MS .NET Framework 1.1 is implemented in Mono as well.

    * XmlTextReader, XmlNodeReader and XmlValidatingReader are also implemented in mono.
This is an example of a Reading XML document.

XmlTextReader myreader = new XmlTextReader ("Test.xml"); // XML in my local M/c myreader.Read (); Console.WriteLine (myreader.NodeType); // What's the type of the Node myreader.MoveToContent (); // Move Next Console.WriteLine (myreader.NodeType);  // What's the next node type Console.WriteLine (myreader.Name); Console.WriteLine (myreader.GetAttribute ("version"));  // version is name of the attribute while (!myreader.EOF && myreader.Name != "item") // looping through the XML file myreader.Read (); myreader.Read while (myreader.NodeType == XmlNodeType.Whitespace) // 4 attributes are there Name,ID,                                                                                    //Department & Description myreader.Read (); Console.WriteLine ("Name : " + myreader.ReadString ()); myreader.Read (); myreader.Read ();   Console.WriteLine ("Id  : " + reader.ReadString ()); myreader.Read (); myreader.Read (); Console.WriteLine ("Department  : " + myreader.ReadString ()); myreader.Read (); myreader.Read (); Console.WriteLine ("Description : " + myreader.ReadString ());

In Monodevelop, developers can also draw graphs using 2 different namespace 1. System.Drawing namespace that is compatible with Microsoft API. For Windows-based Mono, the GDI+ library is included with the operating system, and there is a specific Dll named GDIPLUS.DLL, The C# code that implements System.Drawing is the same for Windows and UNIX platforms. 2. Mono Cairo library (Mono.Cairo.dll) for 2D vector drawing. This assembly exposes Cairo API to managed applications; this is also very close to the OpenGL model.

Conclusion

Mono is a cross platform, open source .NET development framework. It allows developers to build Linux and cross-platform applications with improved developer productivity. Mono's .NET implementation is based on the ECMA standards for and the Common Language Infrastructure. This open source product is still under constant improvement phase and hopefully the open source and .NET development community will be benefited by this product.


   
Tapas Pal is a Microsoft Platform technical professional with Tata Consultancy Services, India. He has with seven years of experience, holds Microsoft certifications in .NET 1.1 and .NET 2.0.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap