New XAG Feature - Better Generics

In the original release of XAG, properties could be generic only by using aliases. That is, if you wanted to have a property with a type of "Collection<Item>" you would have to create an alias like this:

using LineItemsCollection = System.Collections.ObjectModel.Collection<XagNewGenericsExample.Item>;

...

private LineItemsCollection lineitems;

public LineItemsCollection LineItems {
    get { return lineitems; }
    set { lineitems = value; }
}

which in XAG's markup looked like this:

<Aliases>
  <Alias x:Key="LineItemsCollection" Name="Whatever" Type="System.Collections.ObjectModel.Collection[{Type Item}]" />
</Aliases>
<Properties>
  <LineItems Type="{Type LineItemsCollection}" />
</Properties>

That's not really a bad deal as it really helps to enforce proper type design. For example, many times I will see people doing this:

public class MyAlias : Collection<Item> {}

MyAlias is NOT an alias for Collection<Item>. Try to compare them in code, they won't match up. However, in the first example "LineItemsCollection" and "Collection<Item>" are the same.

That said, I wanted to add something a bit more flexible. So I wrote an extension markup subsystem for XAG that allows for more complex generics. Here's an example of a property using the new generics system (assuming you have the System.Collections.ObjectModel namespace in):

<LineItems Type="{GenericType Name=Collection, Types=({Type Item})}" />

As you can see it's completely inline. Now here's a more complex example:

<ComplexItems Type="{GenericType Name=Dictionary, Types=(Int32, {GenericType Name=Collection, Types=({Type Item})})}" />

This XAG property translates to the following C# property:

private Dictionary<Int32, Collection<Item>> complexitems;

public Dictionary<Int32, Collection<Item>> ComplexItems {
    get { return complexitems; }
    set { complexitems = value; }
}

Here's a complete example you can try on your own.

<Assembly xmlns:x="http://www.jampadtechnology.com/xag/2006/11/" DefaultNamespace="XagNewGenericsExample">
  <Item x:Key="Item" Type="Class" AccessModifier="Public" />
  <ItemSet x:Key="ItemSet" Type="Class" AccessModifier="Public">
    <Imports>
      <Import Name="System.Collections.Generic" />
      <Import Name="System.Collections.ObjectModel" />
    </Imports>
    <Properties>
      <Items Type="{GenericType Name=Collection, Types=({Type Item})}" />
      <ComplexItems Type="{GenericType Name=Dictionary, Types=(Int32, {GenericType Name=Collection, Types=({Type Item})})}" />
    </Properties>
  </ItemSet>
</Assembly>
So now you an have more complex generic types in your XAG structure.

Links