RelationMapping - Lookup an ADO.NET DataSet's DataRelation by DataColumns

Click here for project page


License

Apache License 2.0

Summary

A small class meant to be used with any ADO.NET DataSet in order to create a quick lookup of a DataRelation based upon its ParentColumns and ChildColumns.

Background

ADO.NET Typed DataSets have properties and methods for getting their related DataRows, which are the rows accessible through the defined DataRelations. These properties and methods are auto-generated by the MSDataSetGenerator tool.

In the simple case, when there is only one DataRelation linking two DataTables, this works cleanly. The child row is given a property named "Row", and the parent row is given a method named "GetRows()". For example:

Dim address as AddressRow
Dim person as PersonRow

' auto-generated accessors via the DataRelation
person = address.PersonRow
person.GetAddressRows()

This is easy to read, and the developer needs no knowledge of the DataRelation's name (e.g. Person_FK04).

However, what if there are two or more DataRelations linking the same two DataTables? In that case, the auto-generated properties get named "RowBy" (e.g. AddressRowByPerson_FK04). This is not as convenient, as getting the correct AddressRow requires knowing the name of the DataRelation that it is linked by. Many developers will then make an alias property whose name is more descriptive of the meaning of the relationship (e.g. a HomeAddressRow). For example:

Property HomeAddressRow As PersonRow
    Return Me.AddressRowByPerson_FK04
End Property

This makes the code much more readable and manageable. However, this is tedious and requires looking up what the Database creation tool decided to name the relationship of interest. Furthermore, since the DataRow is being looked up by its DataRelation name, if the name of that DataRelation ever gets changed, the alias Property would then be out of sync, and bugs are easily introduced this way.

As a solution, RelationMapping is a class meant to be used with any DataSet in order to create a quick lookup tables of the DataSet's DataRelations by their ParentColumns and ChildColumns. This removes the need for developer to hard-code the RelationName into any of the code, increasing the readability and robustness of the code. For example:

Property HomeAddressRow As AddressRow
    Dim relation = Me.RelationMapping.GetRelation(MyDataSet.PersonTable.ID, _
                                                  MyDataSet.AddressTable.personID)
    Return Me.GetParentRow(relation)
End Property

Key Features

Usage

  1. Get a DataRelation via its ParentColumns and ChildColumns

    Dim ds = New TypedDataSet1()
    Dim rm = New RelationMapping(ds)
    Dim foundRelation = rm.GetRelation(ds.DataTable1.Column1, ds.DataTable2.Column3)
    
  2. Get a DataRow by its DataRelation

    Dim dr1 As TypedDataRow1
    Dim dr2 as TypedDataRow2
    Dim foundRelation = rm.GetRelation(ds.DataTable1.Column1, ds.DataTable2.Column3)
    dr2 = dr1.GetParentRow(foundRelation)
    dr2.GetChildRows(foundRelation)
    

How to Build

This library and associated unit tests were developed in VS 2008 as standard Desktop projects.

Releases


Author:  Chris Tossing <ctossing -at- kdsecure.com>
Copyright: (c) 2010 by KD Secure, LLC
NO WARRANTY, EXPRESS OR IMPLIED.  USE AT-YOUR-OWN-RISK.