Strong typed XLinq (experiment 1)

If your are unfamiliar with LINQ and XLINQ, read this first.


When you look at how DLinq was implemented, they are using a collection of classes to describe the structure of the database in such a way that database artifacts such as Tables and Columns become first class citizens in .NET.


However, this has not been done for XLinq currently. So I decided to experiment a bit to see if this was doable for XLinq also. Having ‘strong typed’ XLinq support can be thought of on a number of levels:



  • Being able to use element names and attribute names directly
  • Have the compiler ‘validate’ the schema, so that when you are using constructs that the schema does not allow, you get compiler warnings
  • Have the above features with the same performance as without the ‘extra’ layer

My first experiment is only about doing the first item on that list. It turns out to be as simple as I expected… Normally, using XLinq, your code would look something like this:


XElement contacts =
            new XElement(“contacts”,
                        new XElement(“contact”,
                                   new XElement(“name”, “Patrick Hines”),                                                 
                                   new XElement(“phone”, “206-555-0144”),
                                   new XElement(“address”,
                                               new XElement(“street1”, 123 Main St),
                                               new XElement(“city”, Mercer Island),
                                               new XElement(“state”, “WA”),
                                               new XElement(“postal”, “68042”)
                                   )
                        )
            );


As you can see, this involves a lot of strings to give the elements a name. I wanted to fix this so that instead of XElement(”Foo”), you can just use Foo as a class.


The following sample code demonstrates this.







using System;


using System.Collections.Generic;


using System.Text;


using System.Query;


using System.Xml.XLinq;


using System.Data.DLinq;


 


using ContactsSchema;


 


namespace XLinqSchema


{


    class Program


    {


        static void Main(string[] args)


        {


            int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};


            string[] names = new string[]{“Arie”,“Bernard”,“Cornelis”,“David”,“Eduard”,


                                          “Frits”,“Gerard”,“Hendrik”,“Isaac”,“Johannes”};


 


            var expr = new Contacts(


                from i in numbers


                select new Contact(


                            new Id(i),


                            new Name(names[i-1]))


                            );


 


            Console.WriteLine(expr);


 


            Console.ReadLine();


        }


    }


}





When this code is run, it outputs the following:


<Contacts >
&nbsp; <Contact Id=”1″>
&nbsp;&nbsp;&nbsp; <Name>Arie</Name>
&nbsp; </Contact>
&nbsp; <Contact Id=”2″>
&nbsp;&nbsp;&nbsp; <Name>Bernard</Name>
&nbsp; </Contact>
&nbsp; <Contact Id=”3″>
&nbsp;&nbsp;&nbsp; <Name>Cornelis</Name>
&nbsp; </Contact>
&nbsp; <Contact Id=”4″>
&nbsp;&nbsp;&nbsp; <Name>David</Name>
&nbsp; </Contact>
&nbsp; <Contact Id=”5″>
&nbsp;&nbsp;&nbsp; <Name>Eduard</Name>
&nbsp; </Contact>
&nbsp; <Contact Id=”6″>
&nbsp;&nbsp;&nbsp; <Name>Frits</Name>
&nbsp; </Contact>
&nbsp; <Contact Id=”7″>
&nbsp;&nbsp;&nbsp; <Name>Gerard</Name>
&nbsp; </Contact>
&nbsp; <Contact Id=”8″>
&nbsp;&nbsp;&nbsp; <Name>Hendrik</Name>
&nbsp; </Contact>
&nbsp; <Contact Id=”9″>
&nbsp;&nbsp;&nbsp; <Name>Isaac</Name>
&nbsp; </Contact>
&nbsp; <Contact Id=”10″>
&nbsp;&nbsp;&nbsp; <Name>Johannes</Name>
&nbsp; </Contact>
</Contacts>




Making this happen was not really hard to do. I inherited from XElement with the strong typed counterpart, implementing all constructors, with the difference that the XName does not have to be filled anymore.


See the code below:


using System;


using System.Collections.Generic;


using System.Text;


using System.Query;


using System.Xml.XLinq;


using System.Collections;


&nbsp;


namespace ContactsSchema


{


&nbsp;&nbsp;&nbsp; public class Constants


&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public const string TargetNamespace = “urn:mycontacts”;


&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp; public class Contacts : XElement


&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static readonly XName ElementName = XName.Get(“Contacts”, Constants.TargetNamespace);


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Contacts(Contacts element)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(element)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;}


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Contacts(params object[] childItems)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(ElementName, childItems)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// Constructs an empty list


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ///


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Contacts()


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(ElementName)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp; public class Contact : XElement


&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static readonly XName ElementName = XName.Get(“Contact”, Constants.TargetNamespace);


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Contact(Contact contactElement)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(contactElement)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Contact(params object[] childItems)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(ElementName, childItems)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Contact()


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(ElementName)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp; public class Name : XElement


&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static readonly XName ElementName = XName.Get(“Name”, Constants.TargetNamespace);


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Name(Name contactElement)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(contactElement)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // No elements allowed below this


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Name(object value)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(ElementName, value)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Name()


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(ElementName)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp; public class Id : XAttribute


&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Id(Id attribute)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(attribute)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Id(object value)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(“Id”, value)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }


&nbsp;&nbsp;&nbsp; }


}





In a following experiment, I want to try to make the needed ‘Mapping code’ even more compact, and also support more Schema aware features, such as performing a validation of the object tree, or if possible, make the compiler complain about schema violations…


Until then, happy Linqing..