Mapping and saving data with CloudDB

I worked some more on this last night and made some good progress. Our models previously were manually extracting values from the TablularData for the CloudDB data row.  This meant we hard coded which column index to read.  It’s not flexible.  I could write code to generate a proxy class derived from the model which would be marked a virtual.  However, that would cause problems on medium trust, so I went with basic Reflection to find properties and a custom attribute to specify the CloudDb column name. Here’s what that looks like:

   1:  public class CloudDBColumnAttribute : Attribute
   2:  {
   3:      public string Name { get; set; }
   4:   
   5:      public CloudDBColumnAttribute(string name)
   6:      {
   7:          Name = name;
   8:      }
   9:  }

 

Use that to tag your properties:

   1:  [CloudDBColumn("id")]
   2:  public Guid Id { get; set; }

 

Just use GetType().GetProperties() to find the properties for the class, and then check if that property is tagged like this:

   1:  object[] columnAttributes = info.GetCustomAttributes(typeof(CloudDBColumnAttribute), true);
   2:  if (columnAttributes == null || columnAttributes.Length == 0 || !(columnAttributes[0] is CloudDBColumnAttribute)) continue;

 

You can then loop through each of the tagged properties and find the corresponding column in the TabularData to find the right column.  Then take that index to get the right value from the Row fields.  The end result is an easy way to set property values from CloudDB data.

   1:  public static T Open(TabularData data, Row row)
   2:  {
   3:      T item = new T();
   4:      item.Map(data, row);
   5:      return item;
   6:  }

 

I’ll post more later, and I’ll provide code on request.

 
February 25, 2010 11:05 by josh
E-mail | Permalink
blog comments powered by Disqus

Comments



about josh

another programmer blogging about his misadventures in writing code.

Contact

contact us for website & software consulting

Decide

decide on pragmatic solutions

Develop

develop your product together

Succeed

achieve your goals with our services