65.9K
CodeProject is changing. Read more.
Home

Rapid Mapping Form

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (1 vote)

Jul 17, 2009

CPOL

3 min read

viewsIcon

20801

downloadIcon

409

This article describes a form with two lists. The form is used to drag and drop items from one list to the other and create a "mapping" between items from the lists.

Introduction

This article describes a form with two lists. The form is used to drag and drop items from one list to the other and create a "mapping" between items of the lists.

Background

We were recently developing a utility to facilitate conversion of data from our legacy systems to the new generation of systems. We had the whole thing up and running and then the guy who used it said:

"Why don't you give me a screen with a list of all the old DB tables on the left side and another list of all the new DB tables on the right side and I will just drag a table from one side, drop it on a table on the other side and you will create the mapping."

"Oh, we said, we will have to evaluate the..."

"And while you are at it, why don't you make another screen with the fields from the source table on the left side and..."

"Oh, we said, we get the picture. Let us see what we can do."

Well, since it was indeed a good idea, and since we had metadata for both data bases, we went on and made the "rapid mapping form". As the form is to be reused (event without the golden rule – you never know when you will need it again – we already had two usages) we developed a general form, applicable in any mapping scenario.

Using the Code

The RapindMappingForm has a constructor that takes in an IEnumerable<object> which contains the left side object, an array of string, each the name of a property on the left side object. It takes another set for the right hand side, and a Boolean indicating whether objects mapped should be removed from both sides of the form. The form displays two lists. Each list contains a row for each object in the IEnumerable<object> and a column for each property in the string array. Each time the user creates a mapping, the MappingCreated event is fired and the two objects are supplied as properties of the RapidMappingEventArgs.

RapidMappingForm rapidMappingForm = new RapidMappingForm(
    GetLeftList()
    , new string[] { "Name", "Age" }
    , GetRightList()
    , new string[] {"Name","PetType" }
    , true);
rapidMappingForm.MappingCreated += new RapidMappingEvent(rapidMappingForm_MappingCreated);
rapidMappingForm.Show(); 

The code includes the rapid mapping form and a test project that runs it with or without removing elements after mapping.

Points of Interest

The code is mostly simple Drag and Drop and a little trivial reflection.

Type Safety

As you may have noticed, the list for both sides of the mapping are not type safe. This is a potential problem but it also opens the way for some nice improvisations. Say you are displaying tables metadata on the left hand side of your mapping form and you want to add an option of "Create New Table". All you have to do is make an object that has those specific properties you intend to display (hell, it can even be of an anonymous type). You then would have to figure out what to do with your e.LeftObject on the MappingCreated event...
It is most definitely possible to produce a generic version of this form: RapindMappingForm<TLeftList,TRightList> (including the events and all).

Loose Edges

Quality and Documentation

The code was built rapidly, with the intention of serving an in-house tool. It is properly under-designed, under-coded, under-documented and under-tested, though the guy who ordered it is very happy. Use it as an inspiration, after meticulous inspection, or at your own risk.

Mapping Button

Wouldn't it be nice if there was a button that says "Map!" and if it is pressed while an item is selected in both lists, it would invoke the Mapping event?

WPF

Aha, it would also be nice to have this in WPF.

History

  • 17th July, 2009: Initial version