Archive for August, 2005

Dynamically Mapping a Custom Entity

This post from Chris Wallace
got me thinking about dynamically mapping data results to custom
entities.  In his post Chris outlines a way to dynamically fill a
custom entity using a switch statement on the columns that are present
in the datareader.  Towards the end, he mentions dynamically
creating a method (using the CodeDom presumably) and compiling and
executing the method on the fly.

I thought of a different way to accomplish the same goal, one that
requires no knowledge of the custom entity, by using some simple
reflection.  Basically, you have a datareader (or datatable,
dataset, whatever), you loop through the columns of the datareader and
get the name of the current column.  Using reflection, check to
see if the custom entity has a property with the same name, if it does
then fill the value, if not, continue looping.

I’m going to show a small sample below which is very simplistic, but
it gets the point across.  There are a few caveats that I’m not
addressing:

  • Null values (i.e. trying to set an int property to null)
  • I’m assuming that a default parameterless constructor exists for the custom entity
  • I’m not doing anything for indexed properties (like DataReader[int ordinal])

You can download a little sample winforms application here.  Just change the “DB” value in the app.config to point to the sample access db and you should be all set.

So, heres the code.  The method below takes in a datareader and
the Type of the custom entity that we want to fill and returns a custom
entity with its properties filled in with the data from the datareader.

        public static object MapEntity(IDataRecord record, Type customType)
        {
            object customEntity = Activator.CreateInstance(customType);
            PropertyInfo[] properties = customType.GetProperties();
 
            for(int i=0;i < record.FieldCount;i++)
            {
                string columnName = record.GetName(i);
                foreach(PropertyInfo prop in properties)
                {
                    if (string.Compare(prop.Name, columnName) == 0)
                    {
                        prop.SetValue(customEntity, record.GetValue(i), null);
                        break;
                    }
                }
            }
 
            return customEntity;
        }

This works pretty slick for most cases, but there are situations
that you would run into where this break down.  If you wanted to
have property names that didn’t match the database schema for instance,
but it could certainly handle a large amount of the data access,
so long as you address the issues that I mentioned above.

[Edit: Fixed typo]

Fixin’ Up the House

Erin and I closed on our house last Thursday afternoon, so the
weekend was really busy getting the house ready to move in.  We
painted the living room, dining room, and den.  But the biggest
thing we are doing is pulling up the carpet on the main level and
exposing the hardwood floors and getting them refinished.  My
buddy Newman came down for the weekend to help us out, and my mom brought over her Paintstick, which I HIGHLY recommend. 

The painting was straightforward enough, and pulling the carpet up
wasn’t too bad.  What took the longest time was removing the tack
board that was around the perimeters of the rooms that holds the carpet
in place.  There were also a bunch of staples holding the carpet
padding in.  Later on Sunday afternoon, we took a leap of faith
that there was hardwood floors underneath the linoleum in the kitchen,
and I thought that having hardwood in the kitchen would look really
nice too, we ripped the linoleum out too.  There was two layers of
linoleum, and the bottom layer was absolutely hideous, from the 1960’s
I’m guessing :) .  So I think the floors are all set to be finished
by someone who knows how to do it (not me).  More updates (and
pictures) when thats done.

Progress at Magenic

Its been a busy week at the new job.  The first two days at work were spent doing some HR stuff and learning about the Magenic sales process, and the specific sales process for the Delivery Center that I will be working at.  The more interesting stuff started today, when we got a tutorial about CSLA.net and Magenic’s internal tools that they use to augment CSLA.  We received a 1 hour session going into detail about the CSLA, and another 1.5 hours about the internal tools, its fun to start digging into the cool technology that they have there.  We also got our new machines, which are pretty sweet, dual 3 GHz with a 19” LCD monitor, and another monitor on the way, so I’m pretty excited about that, Magenic really knows how to equip their employeees with the proper tools for the job.

My first project is going to be an internal one that will introduce me to their project development process, I’m really looking forward to it, its exciting starting something new.

Movin’ Time

So I start the new job
on Monday.  I’ve spent the past 3 weeks wrapping up some projects
and doing some documentation so that I don’t leave them with
nothing.  I’m really excited to start, its always nice to get a
clean slate to start from, and its an opportunity that you don’t get
too often, so I’m going to try and use that to my advantage. 
Nobody there knows me, they don’t know my personality (aside from what
they gleaned during the interviews), so if I wanted to, I could
completely re-invent myself.  Its kind of liberating knowing that
I can craft my own image at work now, if there was ever a time for a
life change, now would be it.  If I was passive before, I could be
overly assertive, if I was too aggressive, I can now tone it down, the
options are limitless, I’m pumped :-)

Keyboard Cover

I’m looking for a cover for my keyboard so that I don’t get dust and
crap inside, can anyone recommend a decent one?  The last time I
saw them used was a few years ago, and I’m hoping that they’ve improved
since then, thanks in advance!