posted by Jayson Knight on Mon 16th Feb 2004 19:05 UTC
"Intro to Xen, Page 2/3"
While this example is functionally equivalent, it's actually much more verbose and harder to read due to the fact that we lost out querying language and had to basically roll our own by using a nested foreach loop, and an "if" test. Xen is attempting to merge the two programming models together, and here is a sample (syntax will probably change by the time it's released, look at the concepts more than anything else) written in Xen, with key code in red:

public decimal AddTotal(Order orders, int zip)
{
    decimal total = 0;
    foreach (Item item in order[Zip == zip].item) 
    {
        total += item.price * item.quantity;
    }
    return total;    
}

What in the heck is "Order"? what is this new syntax on the right side of the foreach loop? it's a little hard to explain without first giving a short history lesson on XSD types, and the importance of them (obviously) in the integration of Xen with C# (which is elegantly described as Xen = C# + XSD + XML + XQuery + SQL). here are the basic types in XSD that Xen will attempt to roll up:

sequence
choice 
all 
* + ? 
T , U
T | U 
T & U  
attribute 
attributeGroup 
required 
typedef 
mixed 
xml:space
T requires expr
and a short example using some of these types based on an SVG scenario:
 
<xs:complexType name="container">
   <xs:complexContent mixed="false">
    <xs:extension base="t:shape">
     <xs:sequence>
      <xs:element name="desc" type="xs:string" />
      <xs:element name="title" type="xs:string" />
      <xs:choice maxOccurs="unbounded">
          <xs:element name="rect" type="t:rect" />
          <xs:element name="line" type="t:line" />  
          <xs:element name="g" type="t:group" />
          <xs:element name="ellipse" type="t:ellipse" />
          <xs:element name="polygon" type="t:polygon"/>
          <xs:element name="styles" type="t:styles"/> 
      </xs:choice>
     </xs:sequence>
    </xs:extension>
   </xs:complexContent>
 </xs:complexType>

So the question is how to take this declarative model and model it in Xen? here it is:

public class rect : shape 
{
  attribute int x; 
  attribute int y;
  attribute int width;
  attribute int height; 
}

class container : shape 

{
sequence {
   string title;
   string desc;
   choice {
           rect;
           line;
           group g;
          ellipse;
           polygon; 

       styles
   }* 
}  
}

As you can see, the XSD constructs of "sequence" and "choice" are made first class citizens of Xen, as well as the notion of zero or more as indicated by the asterisk for the "choice" block, and attributes are strongly typed and mapped to CLR prmitives. this is quite obviously quite an improvement over the XSD way of handling things. so that covers the XSD part of the equation, now on to the XML portion and what is slated to be included. There are 5 fundamental XML Literals that are mentioned, along with their importance to the XML language, they are:

namespace "URI" { }
using prefix = "URI";
Foo f = ;
Code snippets: ;
XSD validation by the compiler
One ramification of this is that the Xen compiler will be doing the XSD validation at compile time. of course you can validate in the current version of .Net, but a realistic situation is that you will be building an XSD document at runtime to be sent to perhaps another application to be consumed, what happens if it's not valid? Trying to track down the source of the error is also time consuming and arduous. the benifits of moving this sort of validation into the compiler should be quite obvious. here is an example of some code that uses current XML technologies in .Net to build an HTML table:

protected override table GetResult() {
    table result = new Table();
    tr row  = new row();
    result.Items.Add(row);
    th cell = new th();
    cell.InnerText = "ID";
    row.Items.Add(cell);
    cell = new th();
    cell.InnerText = "Name";
    row.Items.Add(cell); 
    …
    foreach (tr row in GetCustomersByCountry(this.country)){
                   table.Items.Add(row);
    }
    return result;
}
Table of contents
  1. "Intro to Xen, Page 1/3"
  2. "Intro to Xen, Page 2/3"
  3. "Intro to Xen, Page 3/3"
e p (0)    35 Comment(s)

Related Articles

posted by Alexandru Lazar on Mon 5th Jan 2009 19:13
posted by Kroc Camen on Sat 27th Dec 2008 20:52
posted by Thom Holwerda on Fri 26th Dec 2008 11:26, submitted by poundsmack