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.
public string getData()
ComegaGetData.DataBlock x = new ComegaGetData.DataBlock;
ComegaGetData.GetData dService = new ComegaGetData.GetData();
x = dService.getData();
catch (Exception e)
|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.
The method just returns 'Hello
' when it is doneyou 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 yetprobably not until C# version 3 at the earliestbut you can use it today!