July 2004 - Posts - Reyn's Blog
in

dotnet.org.za

South African .NET Developer Portal

Reyn's Blog

Divide and Conquer !
Remember, pain is just fear leaving your body !

July 2004 - Posts

  • My first javascript :-P

    I'v been trying to avoid javascript like the plaque, always delegating it to the javascript guru or living without the functionality(bad! bad!),but it seems inevitable. Yesterday I made my first javascript alert popup :-o

    Now to study what javascript can actually do, so that I'll know when it should be used.

  • Interviews

    My current contract is nearing an end so a coworker told me that flux was looking for contractors. I'v got an interview there friday, it isnt often one gets the chance to look at your interviewer's blog :-) Which makes me ask the question: Do you give a reference to your blog in your CV ? Would you consider it something an employer should see ? Well ... the rest of the world does ...
    Posted Jul 28 2004, 04:31 PM by reyn with 3 comment(s)
    Filed under:
  • OpenSource CMSs

    Since we'r on an opensource roll here :)

    http://www.opensourcecms.com/ is a website which has 29 demos of different opensource cms sites. You can login as admin and change whatever you like, in most instances. The sites are recreated every 2 hours. These sites were all built using php and mysql.

    Also have a look at www.plone.org which is built with www.zope.org which uses www.python.org

    Posted Jul 27 2004, 11:53 AM by reyn with 1 comment(s)
    Filed under:
  • Doxygen

    Here is an example of an inheritance graph generated by doxygen, its shows the sps query builder from a previous post. This works wonders when reverse engineering things.
    Check www.doxygen.org, to generate graphs you would also have to download dot, www.graphviz.org
    Posted Jul 15 2004, 01:27 PM by reyn with 2 comment(s)
    Filed under:
  • SharePoint query builder.

    I’ve been working with Microsoft SharePoint, on and off, for about 3 months, so don’t bite me if something in that ball of yarn already does this :-)

     

    If you’ve been working with SharePoint you would have come across it’s lists. Lists are like tables in a database, they have columns/records etc. These list are queried via an xml based language called CAML.

     

    A typical query would look like this:

     

    <Where>

    <Eq>

    <FieldRef Name="SomeColumn1" />

    <Value Type="Text" >SomeValue1</Value>

    </Eq>

    </Where>

     

    This would return all the records which have a value in column “SomeColumn1” equal to “SomeValue1”. Simple, isn’t it.

     

    The agonizing pain only surfaces when the queries get more complex eg,

     

    <Where>

    <Or>

    <And>

    <Eq>

    <FieldRef Name="SomeColumn1" />

    <Value Type="Text" >SomeValue1</Value>

    </Eq>

    <Eq>

    <FieldRef Name="SomeColumn2" />

    <Value Type="Text" >SomeValue2</Value>

    </Eq>

    </And>

    <And>

    <Eq>

    <FieldRef Name="SomeColumn2" />

    <Value Type="Text" >SomeValue2</Value>

    </Eq>

    <Eq>

    <FieldRef Name="SomeColumn3" />

    <Value Type="Text" >SomeValue3</Value>

    </Eq>

    </And>

    </Or>

    </Where>

     

    Logically it is equivalent to this:

     

    ((SomeColumn1 == “SomeValue1”) AND (SomeColumn2 == “SomeValue2”)) OR ((SomeColumn3 == “SomeValue2”) AND (SomeColumn3 == “SomeValue3”))

     

    Clearly writing this by hand will become unmanageable very quickly, especially if you want to exclude parts of the query based on, say, if a textbox have been left blank.

     

    A simple framework

     

    The solution I choose works like this. All the query keywords are represented by classes and implement the IQueryInterface interface:

     

     

    interface IQueryElement

    {

          // The output of the element

          string Output

          {

                get;

          }

         

          // Must return true if the element is to be included

          bool Evaluate

          {

                get;

          }

    }

     

     

    Thus something simple like equals is then implemented like this:

     

     

    class QEq : ITestOperator

    {

          IQueryElement qe;

         

          // Constructor

          public QEq(IQueryElement queryElement)

          {

                qe = queryElement;

          }

               

          public string Output

          {

                get

                {

                      return "<Eq>"+qe.Output+"</Eq>";

                }

          }

     

          public bool Evaluate

          {

                get

                {

                      if (qe == null)

                            return false;

                      return qe.Evaluate;

                }

          }

    }

     

     

    Note that it implements ITestOperator and not IQueryElement, infact ITestOperator itself implements IQueryElement. This is just to help classify Eq as something that does a test (for equality in this case), why would we want to do this ? For compile time type checking of course, lets take a look at where this comes into play.

     

    The And keyword is a bit more complex, have a look at the downloadable code.

     

     

    class QAnd : IBinaryOperator, ITestOperator

          {

                public QAnd(ITestOperator _qe1,ITestOperator _qe2)

                .

                .

          }

     

     

    The And operator is a binary operator, as you can see it takes two ITestOperator parameters in its constructor. If a developer tries to use anything but an ITestOperator he/she would get a compile time error.

     

    Ok, that’s nice and all, but lets have a look at complete example:

     

     

    // Define our tests

    QEq someColumn1 = new QEq(new QFieldRefValue("SomeColumn1","SomeValue1",SPQueryTypes.Text));

                     

    // Specify the combination of our tests

    QQuery query = new QQuery(new QWhere(someColumn1));

                     

    // Get the query

    System.Console.WriteLine(query.Output);

     

    Query 1

     

    Yes, you guessed it, it’s the first query right at the top of this page. What’s that QFieldRefValue you ask ? Well, its:

     

     

    class QFieldRefValue : IQueryElement

          {

                public QFieldRefValue(string _fieldname, string _qvalue, SPQueryTypes _type)

                .

                .

          }

     

               

    The QFieldRefValue class takes a column name a value and a type at it’s constructor, this represents the column and what it will be tested against. The QEq class wraps the QFieldRefValue class to test the “SomeColumn1” column for equality against “SomeValue1” which is of type Text, an enum (Add some more, I haven’t included them all).

     

    The second xml query would look like this:

     

     

    // Define our tests

    QEq someColumn1 = new QEq(new QFieldRefValue("SomeColumn1","SomeValue1",SPQueryTypes.Text));

    QEq someColumn2 = new QEq(new QFieldRefValue("SomeColumn2","SomeValue2",SPQueryTypes.Text));

    QEq someColumn3 = new QEq(new QFieldRefValue("SomeColumn3","SomeValue3",SPQueryTypes.Text));

                           

    // Specify the combination of our tests

    QQuery query = new QQuery(

                      new QWhere(

                            new QOr(

                                  new QAnd(someColumn1,someColumn2),

                                  new QAnd(someColumn2,someColumn3)

                               )));

                     

    // Get the query

    System.Console.WriteLine(query.Output);

     

    Query 2

    That’s quite a bit more maintainable.

     

    An added bonus

     

    I can hear you cry, “It would never stay that simple because I have a dynamic beast of a query!”, fear not, the evaluate method you saw in the IQueryElement interface has a purpose.

     

    So you have a query which lets the user search on 3 columns, name, surname and hobby. The query uses ANDs like in query 2 to search for records which have all three the fields, but what happens when the name textbox is left empty ? If you decide to exclude the name for the query all you need to change (just taking the name for now) is :

     

     

    // Define our tests

    QEq name = new QEq(

                new QFieldRefValue("Name",

                                   txtName.Text.Equals(“”)? null : txtName.Text,

                                   SPQueryTypes.Text));

     

    Query 3

     

    This will set the value of the test to null if the textbox’s text is empty, and guess what ? The query will sort itself out. That means that you don’t have to pay special attention to the final query you build with all the composite ANDs and ORs.

     

    Give the downloadable code a try(http://www.reyn.co.za/index.php?pr=SP_Query_Builder). If you find a bug, found a better way of doing this or start using this for your work/own project, drop a comment.

    Posted Jul 13 2004, 12:57 AM by reyn with 36 comment(s)
    Filed under:
  • The singleton pattern VS static classes

    I was attending the “Microsoft patterns and practices” lectures the other day when the subject of the singleton pattern came up.

     

    One of the questions a friend asked, which weren’t discussed at the time, was: “Why would I want to use the singleton pattern when I can simply declare all the methods and variables in my class as static?”

     

    Well, of course you can. If you have a self contained class that doesn’t have any special initialization. Something like:

     

    public class Test1

    {

          static int someValue = 100;

     

          static public int someMethod()

          {

                return someValue;

          }

     

          static Test1()

          {

                System.Console.WriteLine(“Test1 static constructor”);

          }

    }

     

    Test1 is a simple self contained class with a static constructor. The static constructor will be called right before the first use of the static class.

     

    So if we have something like:

    System.Console.WriteLine("First Line");

    System.Console.WriteLine(Test1.someMethod());

    The output will be: 

    First Line

    Test1 static constructor

    100

     

    You might have already spotted the problem with static classes, copy and paste the following code into your IDE, check if you can see what the output should be before running it:

     

          public class A

          {

                public static string staticVariableCopiedFromB = B.staticVariable;

                public static string staticVariable = "Thing is a string defined in A";

          }

     

          public class B

          {

                public static string staticVariableCopiedFromA = A.staticVariable;

                public static string staticVariable = "Thing is a string defined in B";

          }

     

          public class Test

          {

                static void Main(string[] args)

                {

                      System.Console.WriteLine(A.staticVariableCopiedFromB);                 

                      System.Console.WriteLine(B.staticVariableCopiedFromA);

                }

          }

     

    The output you should get is:

     

    Thing is a string defined in B

     

    Why is this happening? Lets look at what the JIT compiler is doing:

     

    1. A is being called, so start initializing A
    2. The static variable in A needs a value from B, so start initializing B
    3. B needs a static value in A, which isn’t set yet

     

    This prevents infinite circular initialization.

     

    Changing the order so that the string assignment happens before the static assignment from the other class resolves the issue:

     

          public class A

          {

                public static string staticVariable = "Thing is a string defined in A";

                public static string staticVariableCopiedFromB = B.staticVariable;

          }

     

          public class B

          {

                public static string staticVariable = "Thing is a string defined in B";

                public static string staticVariableCopiedFromA = A.staticVariable;

          }

     

    An even better way is to use the static constructor to explicitly initialize it in the correct order:

     

          public class A

          {

                public static string staticVariable;

                public static string staticVariableCopiedFromB;

                static A()

                {

                      staticVariable = "Thing is a string defined in A";

                      staticVariableCopiedFromB = B.staticVariable;              

                }

          }

     

          public class B

          {

                public static string staticVariable;

                public static string staticVariableCopiedFromA;

                static B()

                {

                      staticVariable = "Thing is a string defined in B";

                      staticVariableCopiedFromA = A.staticVariable;

                }

          }

     

    The compiler will actually generate the static constructors itself; it just feels safer to do it explicitly.

     

    That might have been an elegant solution for a very small example, but can you see what will happen when we start working with 5 or more classes with dependencies.

     

    This is why we can’t always depend on the order of static initialization. By explicitly initializing our singleton classes we can be sure that someone doesn’t destroy our carefully crafted static initialization bootstrap code by accidentally calling something when it shouldn’t be. As you can see, there isn’t a compile or run time error, just an unexpected result, these problems can be very difficult to track down.

     

    Please drop a comment if I’m missing something or unaware of a dot net specific feature.

    Posted Jul 07 2004, 12:27 AM by reyn with 6 comment(s)
    Filed under:
Powered by Community Server (Commercial Edition), by Telligent Systems