Blogs

Gary's Blog

Santa Gets it Done With XPO

     

After my last post I said we’d take a look at one to many associations next time, and that’s what we are doing to do now. With it coming up to Christmas I thought we would write a little application to give Santa a hand with his deliveries – I mean the poor old guy’s got a lot of work to do so I figure he could use all the help he can get, right?

So we are going to write an application that will provide him with delivery instructions, detailing which presents to deliver to which house. To do that, the first thing we need is a class to define a house:

using System;
using DevExpress.Xpo;

namespace XPOChristmas
{
    public class House : XPObject
    {
        public House(Session session)
            : base(session)
        { }

        private string address;
        public string Address
        {
            get
            {
                return address;
            }
            set
            {
                SetPropertyValue("Address", ref address, value);
            }
        }

        private Country country;
        public Country Country
        {
            get
            {
                return country;
            }
            set
            {
                SetPropertyValue("Country", ref country, value);
            }
        }        

        [Association("House-Presents")]
        public XPCollection<Present> Presents
        {
            get
            {
                return GetCollection<Present>("Presents");
            }
        }

        public void ShowDeliveryInstructions()
        {
            Console.WriteLine(String.Format("Santa, deliver to {0} the following: ", Address));
            foreach (Present p in Presents)
            {
                Console.WriteLine(String.Format("   {0}", p.Description));
            }
            Console.WriteLine();
        }        
    }
}

As you can see, this class extends XPObject just like you’ve seen in all the XPO example so far. It also has a property detailing the address of the house. This is a simple string property that we’ve seen many times before, so we need not say any more about that.

We need not say too much about the country property either, it simply holds an enum describing the country in which the house is located, the definition of the enum is very simple:

using System;

namespace XPOChristmas
{
    public enum Country
    {
        Scotland,
        England,
        Ireland,
        Wales
    }
}

The next property however, is a little special. This property holds the child relationship with all the presents that are due to be delivered to this house. The first thing to notice about the property is that it is decorated with the Association attribute. This attribute must be unique in the database and the string name must match on both the parent and child ends of the association. Note the use of the helper function GetCollection<T> which returns all the children in the association.

The last thing to notice about this class is the ShowDeliveryInstructions() method that displays instructions for the house and then walks the graph of children and displays instructions for them.

The next thing we need then is a class to describe the presents:

using System;
using DevExpress.Xpo;

namespace XPOChristmas
{
    public class Present : XPObject
    {
        public Present(Session session)
            : base(session)
        { }

        private string description;
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                SetPropertyValue("Description", ref description, value);
            }
        }

        private House house;
        [Association("House-Presents")]
        public House House
        {
            get
            {
                return house;
            }
            set
            {
                SetPropertyValue("House", ref house, value);
            }
        }

    }
}

This class has a Description property which we need not say too much about and also a property which points to it’s parent house. Note the association attribute decorating the property and pay particular attention to the fact that the name is the same name that is on the parent end of the association.

Once we have those two classes we just need code to drive it all:

using System;
using System.Linq;
using DevExpress.Xpo;

namespace XPOChristmas
{
    class Program
    {
        static void Main(string[] args)
        {

            //GS - Persist some houses and presents for Santa
            using (UnitOfWork uow = new UnitOfWork())
            {
                House house = new House(uow)
                {
                    Address = "27 Cromarty Road",
                    Country = Country.Scotland
                };

                new Present(uow)
                {
                    Description = "Little Blue Car",
                    House = house
                };

                new Present(uow)
                {
                    Description = "Big Book For Boys",
                    House = house
                };

                House nextHouse = new House(uow)
                {
                    Address = "27 Chelsea Lane",
                    Country = Country.England
                };

                new Present(uow)
                {
                    Description = "Little Pink Car",
                    House = nextHouse
                };

                new Present(uow)
                {
                    Description = "Big Book For Girls",
                    House = nextHouse
                };

                uow.CommitChanges();
            }

            //GS - Get delivery instructions for all houses
            Console.WriteLine("Displaying all houses...");
            Console.WriteLine();
            using (UnitOfWork uow = new UnitOfWork())
            {
                XPQuery<House> houseQuery = new XPQuery<House>(uow);

                foreach (var house in houseQuery)
                {
                    house.ShowDeliveryInstructions();
                }
            }

            //GS - Get delivery instructions for the house in Scotland
            Console.WriteLine("Displaying Scottish houses...");
            Console.WriteLine();
            using (UnitOfWork uow = new UnitOfWork())
            {
                XPQuery<House> houseQuery = new XPQuery<House>(uow);

                var houses = from h in houseQuery 
                            where h.Country == Country.Scotland 
                            select h;

                foreach(var house in houses )
                {
                    house.ShowDeliveryInstructions();
                }
            }

        }
    }
}

In this code we persist some house and present objects and then retrieve them from the database before asking them to display their delivery instructions. There is nothing really special to say about this code, we have seen many such examples. The only thing to note is that the one to many associations can be specified from either end. In this case I specify it from the child side, because I found that most convenient, but either will do.

Now that I have shown you the code, all that remains is to run it:

image

And that is it really, with such good help how could Santa fail to get all his presents delivered on time? That brings this post to a close, so until next time, happy XPOing! :-)

Published Dec 16 2009, 08:29 PM by Gary Short (DevExpress)
Filed under:
Technorati tags: XPO
Bookmark and Share

Comments

 

Nate Laff said:

How is Santa supposed to get to my house in the United States :( Boo. No presents for me.

December 16, 2009 4:13 PM
 

Gary Short (DevExpress) said:

Maybe you are on the naughty list Nate. :-)

December 16, 2009 5:24 PM
 

Michael Proctor [DX-Squad] said:

Question Gary why doesn't the Setter for the Present.Description or the House.Country call the SetPropertyValue method?

December 16, 2009 6:21 PM
 

Karl Rostock_2 said:

Im on the naughty list after my last post :-(,

Nice post was looking for something similar now i better understand XPO associations.

Thanks

Karl

December 16, 2009 6:47 PM
 

Gary Short (DevExpress) said:

@Michael, well that would because my fat fingers missed the 'X' from the Coderush mnemonic and I didn't notice when I re-read the post. :-)

Well spotted, I'll fix it, thanks!

December 16, 2009 8:25 PM
 

Gary Short (DevExpress) said:

@Michael, Fixed! :-)

December 16, 2009 9:07 PM
 

Alan Taylor said:

Why does House.Country refer to "Address" in SetPropertyValue() ?

December 17, 2009 2:16 AM
 

Gary Short (DevExpress) said:

@Alan, cos I'm a "cut and paste tart". Fixed thanks. Jeez, moral of this story. Don't do stuff late at night when you are tired and your eyes see what they want to see. On the plus side, it shows my readers really do pay attention to what I write :-)

December 17, 2009 6:01 AM
 

Michael Proctor [DX-Squad] said:

LOL go Gary having a bad run there ;), I was wondering if the was some magic occurring, but glad to hear it was just burning the midnight oil a bit long :)

Also for those reading this and not sure what Gary means by Coderush mnenomic, in CodeRush it has some very handy (if not awesome) templates to make it very easy to put in XPO related properties. They are closing related to the "non" XPO templates and in general case just add an X to the front.

So for example for a String property in XPO, use: xps

If you want a collection for an association just use: xpcl

Always forget how to "correctly" put a "readonly" property? as an example for a string just use: xrs

Umm... maybe I should do up a quick blog on this incase there are people out there still hand coding this stuff.

December 18, 2009 2:40 AM
 

Santa Gets it Done With XPO - Gary's Blog Private Me said:

Pingback from  Santa Gets it Done With XPO - Gary's Blog Private Me

December 19, 2009 2:23 AM
 

Santa Gets it Done With XPO - Gary's Blog Private Me said:

Pingback from  Santa Gets it Done With XPO - Gary's Blog Private Me

December 19, 2009 2:23 AM
 

Michael Proctor [DX-Squad] said:

Just did a quick blog on CodeRush XPO template if anyone doesn't yet know what they are :)

www.alfware.com.au/.../20-coderush-is-your-best-friend-for-xpo

December 20, 2009 9:47 PM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.