devxlogo

Meet the Future of Data Head-on with Comega

Meet the Future of Data Head-on with Comega

first saw Comega about a year an a half ago when the lab that was developing it gave me a sneak preview and asked me for some feedback. The only feedback I could give at the time was “Wow!” Now, everybody can get access to this add-on to C# as Microsoft Research has released the preview for public consumption. What is it, you might ask? Comega is an experimental add-on to C# with the intent to make data a first-class citizen of the language. And it succeeds.

In this article you’ll get a whistle-stop tour of some of the features that Comega adds to C#, and build an example application that uses some of these features!

Comega is a strongly-typed, data-oriented programming language intended to bridge the gap between semi-structured hierarchical data (XML), relational data (SQL), and the common type system (CTS?the root of all variable and type declaration .NET languages). It spans XML, SQL, and the CTS by generalizing how they are used. This is difficult to get your head around just reading about it?and best learned through practical and hands-on examples. A little later you will build an application that uses some of these features and gives you a taste for what you can do in Comega.

Getting Started
You can download Comega from Microsoft Research. You’ll need Visual Studio.NET 2003 to be able to use it. Once it’s installed, when you fire up Visual Studio and open the Create New Project dialog, you’ll see a new item in the Project Types list for Comega, and if you select it you can see the Comega project templates. (See Figure 1.)

Go ahead and create a new ‘Windows Application’ with Comega. This creates a new project of type ‘cwproj‘ containing ‘form.cw‘ which is a simple form written with Comega.

Open up this form and take a look at its code. You can see some of this code in Listing 1.

The first thing that you’ll notice is that you have XML inline in your code! It’s not hidden within a string where syntax coloring cannot be interpolated?it’s right there in your code and is used to set the properties of the controls. It’s a little weird at first, because as a developer you are so used to having the XML outside of your code and having to parse the XML to do something meaningful. With Comega, data is a first-class citizen, so XML is used like any other keyword.

Figure 1. Creating a New Comega Project: Once you’ve installed Comega, you create a new Comega project like you would any project in Visual Studio?by selecting a project template from the New Project dialog.

Run this application and you’ll get a basic white window with a salmon-colored button that closes the window if you press it.

Using Data in Comega
The core type systems of Comega are:

  • Streams:
    These are central to how Comega works. A stream is (not to be confused with File Streams or the like) a homogenous collection of a particular type that is constructed when it is needed, and as such, is much more flexible than an array. A stream is used similarly to a struct, but what is special about it is that it can contain programming logic. So, for example, if you want to create a stream that contains even numbers, you can declare it like this:
    int* staticEvenNumbers()		{			yield return 2;			yield return 4;			yield return 6;	} 

    This is very similar to how you would declare a struct. C++ programmers will think pointers when they see the ‘*’ but don’t worry, you’re not going back into pointer trouble! The neat thing about the Comega stream is that you don’t need to statically declare the numbers, it can also be done programmatically, such as:

    int* dynamicEvenNumbers(int nCount)		{			for (i=0; i<=nCount; i++) yield return i*2;	}

    What you get is the best of both worlds between a function call and a data structure! DynamicEvenNumbers is an unusual, and very flexible beast now. It behaves like a function call?returning nCount number of even numbers, a data structure that contains those numbers, and a dynamic array that sizes itself without you having to worry about it. Cool huh?

  • Content Classes:
    A content class is a class that is used to specify the schema or structure of a document type. It is analogous to an XML DTD or an XSD sequence. In practical use, this means that if you want to generate XML documents in your Comega program, you have the additional flexibility of automatic validation of their types. Traditionally you would use a DOM to build the document manually, twiddling with nodes and attributes, and then hope for the best. In Comega, life is a lot easier.

    For example, if you want to create a Content Class for an e-mail message, it needs to contains a header and a body. The header must have a 'From' and a 'To' field, and optionally a 'Subject' field. The body is a string containing the text. You would declare the content class like this:

    public class Email {   //content of Email message  struct{    struct{       string From;       string To;       string? Subject;    } Header;    struct{        string P;    }+ Body;}

    The usefulness of this isn't immediately apparent, but, when you combine it with a function for generating an e-mail message that uses inline XML, it'll reveal itself. Here's an example of such a function:

    public static Email Vacation(DateTime d, TimeSpan s, string to){    return              
    John Doe {to} OOF

    I am OOF from {d} until {d+s}.

    ;}

    Now you are generating an XML document of type 'Email,' which will be validated by the content class. This is much easier than twiddling around with XMLDocuments, XMLNodes, and such. In addition, you enter the custom data for the e-mail message directly inline in the XML by passing the parameters such as 'to' to the XML using the {} syntax, so {to} will create the correct node.

  • SQL-like Selection:
    You are almost certainly accustomed to using SQL for getting data from tables in a database, and have found that there is some wonderful logic that can be applied in SQL to filter and sort information. With Comega, you can now do this with your in-memory data structures. You'll start with a simple stream, for example:
    :struct{ string Title; string Actor; string Genre;}* DVDs = new{ new{Title="The Matrix", Actor="Keanu", Genre="SciFi"},     new{Title="Battlestar Galactica", Actor="Edward James Olmos", Genre="TVSciFi"},     new{Title="Blade Runner", Actor="Edward James Olmos", Genre="SciFi"},     new{Title="Pride and Prejudice", Actor="Colin Firth", Genre="Mushy"}};

    And next, you can query this stream:

    results = select * from DVDs where Actor=="Keanu";results.{Console.WriteLine("Title = {0}, Genre={1}", it.Title, it.Genre);}; 

    So, you can now start using your SQL skills to pull out data instead of writing your own iterators!

These are but a few of the features that are offered in the language and are here to whet your appetite. Comega offers a lot more, including, but not limited to, a new concurrency model for multithreaded applications (formerly known as polyphonic C#), transactions support in SQL, choice-types, nullable value types, and anonymous types. You can learn a lot more in the Comega documentation that comes with the install.

Putting it Together in a Simple Application
The source code download has the full code for this sample article, or you can step through what's covered here. The first thing that you need to understand when building a database application is that Comega can take the metadata from a database into a managed assembly using the Sql2Comega.exe tool that is in the BIN directory of the Comega install. You'll see how this helps you in a moment. First, create a new 'Northwind' Comega application, and follow the instructions in the comments at the top of the generated code. This will get the Northwind connection working using the Sql2Comega tool mentioned earlier. Next, add a new class called 'GetData' to the project.

The entire GetData file is shown in Listing 2.

Change the output type for the project from Console Application to Class Library. You do this by right-clicking on the project in solution explorer, selecting properties, and then selecting 'General' under 'Common Properties'. On the right-hand side you set the project Output. (See Figure 2.)

If you compile your project now, a DLL assembly will be created for you.

Author's Note: Sometimes you will get strange database errors when compiling this (and other) Comega applications. If you encounter these, regenerate the Northwind reference as you did earlier, and try again. If that fails, close Visual Studio, then reopen it and compile again. This software is not a release software, so bugs are inevitable.

You can now consume this assembly within a traditional C# application. Create a new C# Web service and create a reference to the DLL. Note that you cannot create a multi-project solution, mixing C# and Comega in the current version. When you have referenced this DLL, add the following Web method to your application.

 [WebMethod]public string getData(){   ComegaGetData.DataBlock[] x = new ComegaGetData.DataBlock[32];   x.Initialize();   ComegaGetData.GetData dService = new ComegaGetData.GetData();   try   {     x = dService.getData();   }   catch (Exception e)   {     Console.Write(e.Message);   }     return "Hello"; }
Figure 2. Setting the Output Type for a Project: The right column in this table allows you to change many properties of your project. In this case, change the output type to Class Library.

This will consume your Comega content classes (the DataBlocks) and load them into an array for C# to use. You can access the data using (for example) the following syntax. You can see (thanks to the struct) that it is strongly-typed.

x[1].sequence.Body.ProductName

The method just returns 'Hello' when it is done?you could extend it to process the data, now stored in X as you see fit.

This article gave you a whistle-stop tour of some of the features of Comega, and showed you how to build Comega applications today that may be consumed by your front-end applications built in traditional C# or VB.NET. The examples clearly show the power of the language, but have barely scratched the surface of what is possible. Use the techniques shown here to start using Comega meaningfully today to build a data layer into your applications. This is an excellent and innovative language that has a bright future and it will be very interesting to see it unfold and evolve. It isn't likely to hit the mainstream for some time yet?probably not until C# version 3 at the earliest?but you can use it today!

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist