Welcome to tankete.com Sign in | Join | Help

José Lema

Inside the Community Server development team

Shared Posts


Badges



  • www.flickr.com
    This is a Flickr badge showing public photos from tankete. Make your own badge here.
Merlin: Tackling the Easy Stuff

This is part seven of a series on Merlin. I recommend you read from the beginning.

So, when we left off it was time to start parsing and creating object representations of our cleanly configured xml. But first, I couldn't handle my simple definition of a rule not supporting multiple conditions or actions. I needed some collections or I would go nuts...okay, maybe not that bad...

Since I was still targeting CS 2.1 (and ASP.NET 1.1) I stayed away from generics and opted to extend the simple CollectionBase class by making it type-safe. Armed with ConditionCollection, ActionCollection, and even RuleCollection classes, the parsing could begin.

I opted to use a factory method for converting the xml to an object, following the standard Parse method that's available to many primitives. No rocket-science here:

public class ConditionCollection
{
    ...
    public static ConditionCollection Parse(XmlNode node)
    {
        ConditionCollection conditions = new ConditionCollection();

        foreach (XmlNode conditionNode in node.SelectNodes(conditionXml))
            conditions.Add(Condition.Parse(conditionNode));

        return conditions;
    }
    ...
}

As you would expect, each rule-part was responsible for it's own parsing:

public class Condition
{
    ...
    public static Condition Parse(XmlNode node)
    {
        string typeName = node.Attributes["type"].Value;
        Type type = Type.GetType(typeName);
        Condition condition = Activator.CreateInstance(type) as Condition;
        condition.Init(node);

        return condition;
    }
    ...
}

Again, as you would imagine the code would attempt to instantiate the named type (using a default, parameterless constructor). It would then call an abstract method used for additional initialization. After all, if a future condition or action wanted to pull more data from the xml configuration, I wanted to make sure if was not only possible, but easy.

With the parsing and object creation code complete, things were looking good. Unfortunately for me, Friday had rolled around, which left only the weekend to wrap up my work. No worries, there was less than two days worth of work remaining...right?

Posted: Thursday, September 28, 2006 10:00 AM by Jose
Filed under: ,

Comments

Daily News Faq List said:

José Lema in the seventh part of his series on creating the rules-based Merlin Community Server Component

# November 22, 2006 11:09 AM

Community Server Bits said:

José Lema in the seventh part of his series on creating the rules-based Merlin Community Server Component

# March 12, 2007 11:19 AM
New Comments to this post are disabled