Creating a Report Data Model – PriceMatcher

Ok, so I’ve spoken briefly on the subject of separation of concerns in report creation. Let’s now start working through an example.

Pretend I’m starting a new web product — call it pricematcher.com (I’ve not done the brief check to see if this exists. This site collects data from local grocery store flyers and stores information on where grocery products are the cheapest in your city.

What information is needed on a site like this? Product, price, store, flyer date. These are just some simple pieces of data that would be required. Let’s lay it out into an XML structure that makes sense for this data:

<PricingData>
  <City></City>
  <Prices>
    <Price>
      <Date></Date>
      <Product></Product>
      <Cost></Cost>
    </Price>
  </Prices>
</PricingData>

As you can see, we are tracking the city we’re looking at, a list of prices for that city, the date of the price, the product, and the cost. This is all we need for a simple report. But how do we create an object structure that can easily be turned into that structure?

We have a couple options if we are going to use C# and .Net (right now, I am). We could either make a class implementing System.Linq.Xml and create an actual XML structure. This works ok. Instead, we are going to using System.Xml.Serialization and create a serializable structure. This provides us with a little bit more flexibility and less initial overhead.

Here is the code that will implement the data model:

[Serializable()]
[XmlRoot]
public class PricingData
{
    public PricingData()
    {
        Prices = new List<Price>();
    }
    
    [XmlElement]
    public string City { get; set; }
    
    [XmlArrayItem("Price")]
    public List<Price> Prices { get; set; }

    public class Price
    {
        [XmlElement]
        public DateTime Date { get; set; }

        [XmlElement]
        public string Product { get; set; }

        [XmlElement]
        public double Cost { get; set; }
    }
}

So, we have a desired data model, and a couple classes that implement that structure. What is the optimal way to utilize these classes? That’s a discussion for another day.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s