Showing posts with label DataSet. Show all posts
Showing posts with label DataSet. Show all posts

Sunday, April 14, 2013

Compare two DataSets for testing

There is a client that has several statements that are rendered as Crystal Reports; we bind the Crystal Reposts to ADO.NET DataSets. The DataSets are loaded from stored procedures in the usual way. Right now we are printing several of these statements and some poor clerk has to compare those values with an earlier correct one.

My idea is to store the DataSets of known good statements and then I can automatically compare them with a new DataSet loaded from the stored procedure. So, over the weekend I wrote these classes to do the comparisons:

The Code

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace CompareDS
{
    /// <summary>
    /// Compare the contents of DataSets or DataTables 
    /// </summary>
    public class CompareDataSet
    {
        /// <summary>
        /// Compare the content of two DataSets
        /// </summary>
        /// <param name="fromDS">The DataSet considered to be correct</param>
        /// <param name="toDS">The DataSet to be tested</param>
        /// <returns>A list of Inconsistencies</returns>
        public static List<CompareDifference> DoCompareDataSet(
            DataSet fromDS, DataSet toDS)
        {
            var rVal = new List<CompareDifference>();

            // Make sure that the table count match
            if (fromDS.Tables.Count != toDS.Tables.Count)
            {
                rVal.Add(new CompareDifference
            {
                Message = string.Format("Non maching # of tables: {0} != {1}", 
                    fromDS.Tables.Count, toDS.Tables.Count)
                });
            }
            // Process the smaller # of tables
            int minColCount = Math.Min(fromDS.Tables.Count, toDS.Tables.Count);
            for (int i = 0; i < minColCount; ++i)
            {
                DataTable fromTable = fromDS.Tables[i];
                DataTable toTable = toDS.Tables[i];
                string tableName = fromTable.TableName;
                rVal.AddRange(DoCompareTable(fromTable, toTable, tableName, i));
            }
            return rVal;
        }
        /// <summary>
        /// Compare the content of two DataTables
        /// </summary>
        /// <param name="fromTable">The DataTable considered to be correct</param>
        /// <param name="toTable">The DataTable to be tested</param>
        /// <param name="tableName">The Name of the table to be compared</param>
        /// <param name="tableNumber">The zero based table number</param>
        /// <returns>A list of Inconsistencies</returns>
        public static List<CompareDifference> DoCompareTable(DataTable fromTable, 
            DataTable toTable, string tableName = "Table", int tableNumber = 0)
        {
            var rVal = new List<CompareDifference>();
            // Number of Rows should match
            if (fromTable.Rows.Count != toTable.Rows.Count)
            {
                rVal.Add(new CompareDifference
                {
                    TableName = tableName,
                    TableNumber = tableNumber,
                    Message = string.Format("Non maching # of rows: {0} != {1}", 
                        fromTable.Rows.Count, toTable.Rows.Count)
                });
            }
            // Create new CompareDataTable for this table
            var compTables = new CompareDataTable     (fromTable, toTable, 
                tableName, tableNumber);
            int minColCount = Math.Min(fromTable.Rows.Count, toTable.Rows.Count);
            for (int i = 0; i < minColCount; ++i)
            {
                rVal.AddRange(compTables.CompareRow(
                    fromTable.Rows[i], toTable.Rows[i], i));
            }
            return rVal;
        }
    }
    /// <summary>
    /// Compares a DateTable row by row.
    /// I use a seperate non-static class so I don't have to re-aquire
    /// the table's Columns (Overkill, I know)
    /// </summary>
    public class CompareDataTable
    {
        private DataTable _fromTable;
        private DataTable _toTable;
        private DataColumnCollection _fromColumns;
        private DataColumnCollection _toColumns;
        private string _tableName;
        public int _tableNumber;

        /// <summary>
        /// Constructor: I use this class to avoid re-aquiring Columns
        /// for each row.
        /// </summary>
        /// <param name="fromTable">The DataTable considered to be correct</param>
        /// <param name="toTable">The DataTable to be tested</param>
        /// <param name="tableName">The Name of the table to be compared</param>
        /// <param name="tableNumber">The zero based table number</param>
        public CompareDataTable(DataTable fromTable, DataTable toTable, 
            string tableName, int tableNumber)
        {
             _fromTable = fromTable;
             _toTable = toTable;
             _tableName = tableName;
             _tableNumber = tableNumber;

             // Remember the Table Columns
             _fromColumns = _fromTable.Columns;
             _toColumns = _toTable.Columns;
        }
        /// <summary>
        /// Compare the content of two DataRows
        /// </summary>
        /// <param name="fromRow">The DataRow considered to be correct</param>
        /// <param name="toRow">The DataRow  to be tested</param>
        /// <param name="rowNumber">The zero based number of the row</param>
        /// <returns>A list of Inconsistencies</returns>
        public List<CompareDifference> CompareRow(DataRow fromRow, 
        DataRow toRow, int rowNumber)
        {
            var rVal = new List<CompareDifference>();
            int minColCount = Math.Min(fromRow.ItemArray.Count(), 
            toRow.ItemArray.Count());

            for (int i = 0; i < minColCount; ++i)
            {
                object fromCell = fromRow.ItemArray[i];
                object toCell = toRow.ItemArray[i];
                string fromColName = _fromColumns[i].ColumnName;
                string toColName = _toColumns[i].ColumnName;
                // Column names must match
                if (fromColName != toColName)
                {
                    rVal.Add(new CompareDifference
                    {
                        TableName = _tableName,
                        TableNumber = _tableNumber,
                        RowNumber = rowNumber,
                        ColumnName = fromColName,
                        ColumnNumber = i,
                        Message = string.Format(
                            "Non matching column names \"{0}\" != \"{1}\"", 
                            fromColName, toColName)
                        });
                }
                // Type must match
                if (fromCell.GetType() != toCell.GetType())
                {
                    rVal.Add(new CompareDifference
                    {
                        TableName = _tableName,
                        TableNumber = _tableNumber,
                        RowNumber = rowNumber,
                        ColumnName = fromColName,
                        ColumnNumber = i,
                        ExpectedValue = fromCell.ToString(),
                        ActualValue = toCell.ToString(),
                        Message = string.Format(
                            "No matching types \"{0}\" != \"{1}\"", 
                            fromCell.GetType(), toCell.GetType())
                    });
                }
                // And values (string compare for simplicity)
                else if (fromCell.ToString() != toCell.ToString())
                {
                    rVal.Add(new CompareDifference
                    {
                        TableName = _tableName,
                        TableNumber = _tableNumber,
                        RowNumber = rowNumber,
                        ColumnName = fromColName,
                        ColumnNumber = i,
                        ExpectedValue = fromCell.ToString(),
                        ActualValue = toCell.ToString(),
                        Message = string.Format(
                            "No matching values \"{0}\" != \"{1}\"", 
                            fromCell, toCell)
                    });
                }
            }
            return rVal;
        }
    }
    /// <summary>
    /// Simple class to store error messages
    /// </summary>
    public class CompareDifference
    {
        /// <summary>
        /// The Name of the Table 
        /// where the inconsistancy exists
        /// </summary>
        public int TableNumber { get; set; }
        /// <summary>
        /// The zero based number of the Table 
        /// where the inconsistancy exists
        /// </summary>
        public string TableName { get; set; }
        /// <summary>
        /// The zero based number of the column
        /// where the inconsistancy exists
        /// </summary>
        public int? ColumnNumber { get; set; }
        /// <summary>
        /// The name of the column
        /// where the inconsistancy exists
        /// </summary>
        public string ColumnName { get; set; }
        /// <summary>
        /// The number of the row 
        /// where the inconsistancy exists
        /// </summary>
        public int? RowNumber { get; set; }
        /// <summary>
        /// The expected value
        /// </summary>
        public string ExpectedValue { get; set; }
        /// <summary>
        ///  The actual value
        /// </summary>
        public string ActualValue { get; set; }
        /// <summary>
        /// An error message
        /// </summary>
        public string Message { get; set; }
    }
}

Friday, July 27, 2012

Dealing with DataSet’s Outdated Nullability Model

The DataSet is one of the most ancient relics of the .NET framework. DataSets were designed before the invention of Nullable<T> and came up with a different way to deal with database null values. So we are stuck with properties that throw StrongTypingException when the property is null and special IsXXXNull() methods to test whether a property is null. So you end up with ugly code like this:

if (!row.IsReportsToNull())
{
Console.WriteLine(row.ReportsTo);
}
else
{
Console.WriteLine("NULL");
}

or

try
{
Console.WriteLine(row.ReportsTo);
}
catch (StrongTypingException)
{
Console.WriteLine("NULL");
}

This code dance is necessary because you can’t even touch a null property without triggering an exception. So you can’t pass the property to a method that would check for nullability. But you can pass a Func<T> to a lamda:

private string GetStringForTFromDataRowField<T>(Func<bool> isNull, Func<T> getValue, 
string formatString = "{0}")
{
if (!isNull())
{
return string.Format(formatString, getValue());
}
else
{
return "NULL";
}
}

With this function I can reference the field in one (ugly) statement:

Console.WriteLine(GetStringForTFromDataRowField(row.IsReportsToNull, 
() => { return row.ReportsTo; }));

Here I am not referencing the actual property until I confirm that the property isn’t null. Since the property is referenced in an anonymous method it isn’t evaluated until after the IsNull() method confirms that the value isn’t null.

What do I get? Slightly cleaner code, I guess. You still need to match the property with the correct IsXXXNull() method. I find the code in the first couple of examples so ugly that this hack is worth the effort for me.

Tuesday, March 29, 2011

Generate DataSet .XDS

Last week on my Brochure, I wrote about getting the schema of a Stored Procedure or other T-SQL command. Now I am going to bring this a little farther and generate am .XDS file that can be used to generate a Typed DataSet. This T4 Template include file takes a List and a DataSet name and generates the .XSD.

<#@ assembly name="TsqlDesriptionUtil.dll" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ import namespace="TsqlDesriptionUtil" #>
<#@ import namespace="System.Collections.Generic" #>
<#+
    public class DataSetFromTSqlCmd: TextTransformation
    {
        public string dsName;
        public List<TableDescription> tables;

        public override string TransformText()
        {
#><?xml version="1.0" encoding="utf-8"?>
<xs:schema id="<#= dsName #>" 
  targetNamespace="http://jrcs3.blogspot.com/<#= dsName #>.xsd"
  xmlns:mstns="http://jrcs3.blogspot.com/<#= dsName #>.xsd" 
  xmlns="http://jrcs3.blogspot.com/<#= dsName #>.xsd" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
  xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" 
  attributeFormDefault="qualified" 
  elementFormDefault="qualified">
  <xs:annotation>
    <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
      <DataSource DefaultConnectionIndex="0" 
          FunctionsComponentName="QueriesTableAdapter"
          Modifier="AutoLayout, AnsiClass, Class, Public" 
          SchemaSerializationMode="IncludeSchema" 
          xmlns="urn:schemas-microsoft-com:xml-msdatasource">
        <Connections />
        <Tables />
        <Sources />
      </DataSource>
    </xs:appinfo>
  </xs:annotation>
 <xs:element name="<#= dsName #>" 
        msdata:IsDataSet="true" 
        msdata:UseCurrentLocale="true" 
        msprop:Generator_UserDSName="<#= dsName #>" 
        msprop:Generator_DataSetName="<#= dsName #>" 
        msprop:EnableTableAdapterManager="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
<#+ foreach(TableDescription table in tables)
    { #>
        <xs:element 
            name="<#= table.TableName #>"  
            msprop:Generator_UserTableName="<#= table.TableName #>" 
            msprop:Generator_RowDeletedName="<#= table.TableName #>RowDeleted" 
            msprop:Generator_RowChangedName="<#= table.TableName #>RowChanged" 
            msprop:Generator_RowClassName="<#= table.TableName #>Row" 
            msprop:Generator_RowChangingName="<#= table.TableName #>RowChanging" 
            msprop:Generator_RowEvArgName="<#= table.TableName #>RowChangeEvent" 
            msprop:Generator_RowEvHandlerName="<#= table.TableName 
               #>RowChangeEventHandler" 
            msprop:Generator_TableClassName="<#= table.TableName #>DataTable" 
            msprop:Generator_TableVarName="table<#= table.TableName #>" 
            msprop:Generator_RowDeletingName="<#= table.TableName #>RowDeleting" 
            msprop:Generator_TablePropName="<#= table.TableName #>">
          <xs:complexType>
            <xs:sequence>
<#+ foreach(FieldDescription field in table.Fields)
 { #>
                <xs:element 
                    name="<#= field.FieldName #>" 
                    msprop:Generator_UserColumnName="<#= field.FieldName #>"
                    msprop:Generator_ColumnVarNameInTable="column<#= field.FieldName 
                      #>" 
                    msprop:Generator_ColumnPropNameInRow="<#= field.FieldName #>" 
                    msprop:Generator_ColumnPropNameInTable="<#= field.FieldName 
                      #>Column" 
                    type="xs:<#= field.XsdTypeName #>" 
                    minOccurs="0" />
<#+ } #>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
<#+ } #>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
<#+ 
  return this.GenerationEnvironment.ToString();
 }
} #>

For Demonstration purposes I wrote a simple T4 Template that includes the template above and uses the library I wrote in this post to get a description of the data.

<#@ template language="C#v3.5" debug="true" 
#><#@ output extension="xsd" 
#><#@ include file="DataSetFromTSqlCmd.tt" 
#><#@ assembly name="TsqlDesriptionUtil.dll" 
#><#@ import namespace="TsqlDesriptionUtil" 
#><#
    // Since I'm generating XML, I will go to extreme measures to avoid new line 
    // characters like the strange formatting of declarations above ^

    // Get the table descriptions for given T-SQL
    string conString = @"Data Source=localhost;" +
      @"Initial Catalog=Northwind;Integrated Security=True";
    string commandString = "EXEC  CustOrderHist @CustomerID='ALFKI'";
    List<TableDescription> tables = 
      TsqlCommandSchema.GetFieldDescription(conString, commandString);

    // Generate XSD:
    var gen = new DataSetFromTSqlCmd();
    gen.dsName = "MyNewDS";
    gen.tables = tables;
    Write(gen.TransformText()); 
#>

Since TsqlCommandSchema.GetFieldDescription supports multiple results sets and Stored Procedure that use temporary tables.

At work I will probably integrate this template into an existing code generation tool using my TTCommucator tool.

Sunday, December 19, 2010

Generating a Nullable ADO.NET DataSet Wrapper

Part 4: The Template and putting it all together

So, I have my target code, my DataSet describer and some T4 template tools (including the ITextTemplatingEngineHost class I took from MSDN), but I haven’t generated any DataSet Wrapper code! It is Christmas time, but that’s still a lame excuse.

Running the Template

In Part 3, I created a class to pass parameters to a T4 Template and an overly simple template that echoed the parameters to the generated document. Here I’m using the same basic from the Test Console Application, but I am referencing a template that will generate the DataSet Wrapper and passing the appropriate parameters to generate the wrapper I want. This code use a DataSet in an assembly.
// Set up some parameters:
var parms = new TTCommucator.GetParams("setting.txt");
parms.CurrentParameters.Add("DsSource", "Assembly");
parms.CurrentParameters.Add("AssembyPath", @"C:\GeneratorProject\MyAssembly.dll");
parms.CurrentParameters.Add("DataSetName", "MyDS");
parms.CurrentParameters.Add("FileNameSpace", "MyAssembly.DataSets");
parms.CurrentParameters.Add("DsNameSpace", "NewProject.DataSetWrappers");
parms.Save();

// Set the Template File
string templateFileName = "DataSetX.tt";

// Set up host and engine
CustomCmdLineHost host = new CustomCmdLineHost();
Engine engine = new Engine();
host.TemplateFileValue = templateFileName;

//Read the text template.
string input = File.ReadAllText(templateFileName);

//Transform the text template.
string result = engine.ProcessTemplate(input, host);
// Save logic would be here…

Notes on the Template

In the template, I am using TTCommucator.GetParams to read the parameter I’ve written in code above; same as in Part 3. I use the DataSetDesciber.ReflectDataSet.DataSetDef from Part 2 to get the tables, columns, types, etc. from the target DataSet. The generated code will be different from the target code in Part 1. For example:
  • I’m using .NET type names (like System.Int32) instead of C# type names (like int). That’s what Type.ToString() returns and I see no reason to convert it just to look pretty.
  • I’m not using Nullable’s ? operator. No good reason, it just seems more consistent with the .NET types in code (I don’t see System.Int32? in code all that often).
The purpose of the target code is to provide a starting point for generation. The code will change as a result of what you learn in writing the generation and testing. In the Template I am generating C# code, but the template code is in VB.NET; it is easier for me to keep straight which is which.

The Template

<#@ template debug="true" hostspecific="true" language="VB" #>
<#@ output extension=".cs" #>
<#@ assembly name="C:\GeneratorProject\TTCommucator.dll" #>
<#@ assembly name="C:\GeneratorProject\DataSetDesciber.dll" #>
<#@ import namespace="DataSetDesciber.ReflectDataSet" #>
<#@ import namespace="TTCommucator" #>
<# 
    ' Load the Parameter object
    Dim parms As new GetParams(Host.ResolvePath("setting.txt"))
    parms.Load()

    ' Get the namespace
    Dim FileNameSpace As String = parms.CurrentParameters.Item("FileNameSpace")
    Dim DsNameSpace As String = parms.CurrentParameters.Item("DsNameSpace")

    ' a description of the DataSet to be wrapped
    Dim refx As DataSetDef = GetDsDescripter(parms)
#>
// Generated with DataSetWrapper Version 1
using System;
using System.Collections;
using System.Collections.Generic;
using <#=DsNameSpace #>;

namespace <#=FileNameSpace #>
{
    public class <#=refx.DataSetName #>X
    {
        private <#=refx.DataSetName #> _ds;
        public <#=refx.DataSetName #>X(<#=refx.DataSetName #> ds)
        {
            _ds = ds;
        }
<#For Each table As TableDef In refx.TableDefList #>
        public <#=table.TableName#>DataTableX <#=table.TableName#>
        {
            get { return new <#=table.TableName#>DataTableX(_ds.<#=table.TableName#>); }
            //set { _ds.<#=table.TableName#> = value; }
        }
<#Next#>
        public <#=refx.DataSetName #> DataSet
        {
            get { return _ds; }
        }
    }

<#For Each table As TableDef In refx.TableDefList #>
    public class <#=table.TableName#>DataTableX: IEnumerable<<#=table.TableName#>RowX>
    {
        private <#=refx.DataSetName #>.<#=table.TableName#>DataTable _theTable;
        public <#=table.TableName#>DataTableX(<#=refx.DataSetName #>.<#=table.TableName#>DataTable table)
        {
            _theTable = table;
        }

        public <#=refx.DataSetName #>.<#=table.TableName#>DataTable Table
        {
            get { return _theTable; }
        }

        #region IEnumerable<<#=table.TableName#>RowX> Members

        public IEnumerator<<#=table.TableName#>RowX> GetEnumerator()
        {
            //return new <#=table.TableName#>RowXEnum(_rows);
            return new <#=table.TableName#>TableEnum(_theTable);
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)this;
        }

        #endregion
    }

    public class <#=table.TableName#>TableEnum : IEnumerator<<#=table.TableName#>RowX>
    {
        private <#=refx.DataSetName #>.<#=table.TableName#>DataTable _collection;
        private int curIndex;
        private <#=table.TableName#>RowX curBox;

        public <#=table.TableName#>TableEnum(<#=refx.DataSetName #>.<#=table.TableName#>DataTable collection)
        {
            _collection = collection;
            curIndex = -1;
            curBox = default(<#=table.TableName#>RowX);
        }

        public bool MoveNext()
        {
            //Avoids going beyond the end of the collection.
            if (++curIndex >= _collection.Count)
                return false;
            else
                // Set current <#=table.TableName#>RowX to next item in collection.
                curBox = new <#=table.TableName#>RowX((<#=refx.DataSetName #>.<#=table.TableName#>Row)_collection.Rows[curIndex]);
            return true;
        }

        public void Reset() { curIndex = -1; }

        public void Dispose() { }

        public <#=table.TableName#>RowX Current
        {
            get { return curBox; }
        }

        object IEnumerator.Current
        {
            get { return curBox; }
        }
    }

    public class <#=table.TableName#>RowX
    {
        <#=refx.DataSetName #>.<#=table.TableName#>Row _row;
        public <#=table.TableName#>RowX(<#=refx.DataSetName #>.<#=table.TableName#>Row row)
        {
            _row = row;
        }
        /// <summary>
        /// The wrapped DataRow
        /// </summary>
        public <#=refx.DataSetName #>.<#=table.TableName#>Row Row
        {
            get { return _row; }
        }
        #region Properties
        <#For Each column As ColumnDef In table.ColumnDefList#>
            <#If column.AllowDBNull Then#>


        public <#=GetNullableType(column.DataType.ToString())#> <#=column.ColumnName#>
        {
            get
            {
                if (_row.Is<#=column.ColumnName#>Null())
                    return null;
                else
                    return _row.<#=column.ColumnName#>;
            }
            set
            {
                if (<#=GetHasValueFunction(column.DataType.ToString())#>)
                    _row.Set<#=column.ColumnName#>Null();
                else
                    _row.<#=column.ColumnName#> =  
                        <#=GetNullableValueName(column.DataType.ToString())#>;
            }
        }
        <#Else#>

        public <#=column.DataType.ToString()#> <#=column.ColumnName#>
        {
            get { return _row.<#=column.ColumnName#>; }
            set { _row.<#=column.ColumnName#> = value; }
        }
        <#End If#>
    <#Next#>

        #endregion Properties
    }
<#Next#>
}
<#+ 
    ''' <summary>
    ''' Gets a DataSetDef bases on parms passed in
    ''' </summary>
    ''' <param name="parms">
    ''' The GetParms object containing parameters needed to create DataSetDef:<br />
    ''' AssembyPath: The path of the assembly containig the DataSet<br />
    ''' DataSetName: The fully qualified name of the dataset
    ''' </param>
    ''' <returns>The DataSetDef</returns>
    Function GetDsDescripter(parms As GetParams) AS DataSetDef
        Dim AssembyPath As String = parms.CurrentParameters.Item("AssembyPath")
        Dim DataSetName As String = parms.CurrentParameters.Item("DataSetName")
        Return DataSetReflector.GetDataSetDef(AssembyPath, DataSetName)
    End Function
    ''' <summary>
    ''' Gets the nullable type name for a given type name
    ''' </summary>
    ''' <param name="typeName">
    ''' The type name as a string (as returned from Type.ToString())</param>
    ''' <returns>The Nullable type name</returns>
    ''' <remarks>
    ''' Since strings are nullable and not compatible with Generic Nullable class, 
    ''' must be treated special 
    ''' </remarks>
    Function GetNullableType(typeName As String) As String
        Dim rVal as string
        Select Case typeName
        Case "System.String"
            rVal = typeName
        Case Else
            rVal = "Nullable<" & typeName & ">"
        End Select
        Return rVal
    End Function
    ''' <summary>
    ''' Gets the string used to check nullablity in a set property method
    ''' </summary>
    ''' <param name="typeName">
    ''' The type name as a string (as returned from Type.ToString())
    ''' </param>
    ''' <returns>
    ''' string of an expression that will return true if value is null
    ''' </returns>
    ''' <remarks>
    ''' Most nullable types have a HasValue property but not all
    ''' </remarks>
    Function GetHasValueFunction(typeName As String) As String
        Dim rVal as string
        Select Case typeName
        Case "System.String"
            rVal = "System.String.IsNullOrEmpty(value)"
        Case Else
            rVal = "!value.HasValue"
        End Select
        Return rVal
    End Function
    ''' <summary>
    ''' Gets the string used on the right side of set property method
    ''' </summary>
    ''' <param name="typeName">
    ''' The type name as a string (as returned from Type.ToString())
    ''' </param>
    ''' <returns>The Value property name</returns>
    ''' <remarks>
    ''' Most nullable types have a Value property but not all
    ''' </remarks>
    Function GetNullableValueName(typeName As String) As String
        Dim rVal as string
        Select Case typeName
        Case "System.String"
            rVal = "value"
        Case Else
            rVal = "value.Value"
        End Select
        Return rVal
    End Function
#>

Generating a Nullable ADO.NET DataSet Wrapper: The Series

Tuesday, November 30, 2010

Generating a Nullable ADO.NET DataSet Wrapper

Part 2: Describing the DataSet

The next thing I need for code generation is a reliable source of meta data describing the code I want to generate. Here I merely ask the DataSet to describe itself and store the description into a set of simplified description objects. I am moving the metadata from the DataSet into a simpler object to make it easier to deal with when I start generating code in later parts of this series.

public static DataSetDef GetDataSetDefFromDS(DataSet ds) 
{ 
    // Code for this class is in “The Describer Classes” section below 
    var dsDef = new DataSetDef(ds.DataSetName, ds.Namespace); 
    // The DataSet has a collection of DataTables 
    foreach (var rawTable in ds.Tables) 
    { 
        DataTable tab = (DataTable)rawTable; 
        // See “The Describer Classes” 
        var tabDef = new TableDef(tab.TableName); 
        // And each DataTable has a collection of DataColumns 
        foreach (var rawColumn in tab.Columns) 
        { 
            DataColumn col = (DataColumn)rawColumn; 
            // See “The Describer Classes” 
            var colDef = new ColumnDef(col.ColumnName, col.DataType, col.AllowDBNull); 
            tabDef.ColumnDefList.Add(colDef); 
        } 
        dsDef.TableDefList.Add(tabDef); 
    } 
    return dsDef; 
} 

Getting the DataSet to Interrogate

OK, I can generate a description of the data from a DataSet, but the DataSet is in a different project. I want to make a tool that I can use

XSD Schema File

When you create a typed DataSet in Visual Studio, creates a .xsd file that it uses to generate your DataSet code. I can read the schema from an XSD file into a DataSet using the ReadXmlSchema() method:

public static DataSetDef GetDataSetDefFromXsd(string XsdPath) 
{ 
    DataSet ds = new DataSet(); 
    try 
    { 
        ds.ReadXmlSchema(XsdPath); 
        // Call the method above: 
        return GetDataSetDefFromDS(ds); 
    } 
    catch // I don’t care what it is right now 
    { 
        return null; 
    } 
} 

In an Assembly

The DataSet may exist in a previously existing assembly; we may not even have access to the source code. Using reflection, we can activate the type and cast it as a DataSet:

public static DataSetDef doRefelctDataSetFromType(Type myObjectType) 
{ 
    // Create an instance of the Type: 
    object obj = Activator.CreateInstance(myObjectType); 
    // Try to cast it as a DataSet: 
    DataSet ds = obj as DataSet; 
    if (ds != null) 
    { 
        // Call the method above: 
        return GetDataSetDefFromDS(ds); 
    } 
    return null; 
} 

The Describer Classes

These are the classes to make the objects loaded in GetDataSetDefFromDS(). They represent a simplified description of the data. I only get the data that I think I need to generate the code later. If I later need more data, I can alter GetDataSetDefFromDS() and these classes add what I need.

public class DataSetDef 
{ 
    public List TableDefList { get; private set; } 
    public DataSetDef() 
    { 
        TableDefList = new List(); 
    } 
    public DataSetDef(string dataSetName, string nameSpace) : 
        this() 
    { 
        DataSetName = dataSetName; 
        NameSpace = nameSpace; 
    } 
    public string DataSetName { get; set; } 
    public string NameSpace { get; set; } 
} 
public class TableDef 
{ 
    public List ColumnDefList { get; private set; } 
    public TableDef() 
    { 
        ColumnDefList = new List(); 
    } 
    public TableDef(string tableName) : 
        this() 
    { 
        TableName = tableName; 
    } 
    public string TableName { get; set; } 
} 
public class ColumnDef 
{ 
    public ColumnDef(string columnName, Type dataType, bool allowDBNull) 
    { 
        ColumnName = columnName; 
        DataType = dataType; 
        AllowDBNull = allowDBNull; 
    } 
    public string ColumnName { get; set; } 
    public Type DataType { get; set; }  // .NET Type, not SQL Type 
    public bool AllowDBNull { get; set; } 
} 

Generating a Nullable ADO.NET DataSet Wrapper: The Series

Wednesday, November 24, 2010

Generating a Nullable ADO.NET DataSet Wrapper

Part 1: The Target Code

Classic ADO.NET is the most successful client side database technology in the history of Microsoft. Even if the Entity Framework (officially "ADO.NET Entity Framework") is as successful as Microsoft thinks it will be, ADO.NET DataSets will be around for years.

One of the biggest weaknesses of the ADO.NET DataSet is the turn of the century way it handles nullability. Since Nullable didn’t exist until .NET 2, the designers of the DataSet handled nullability with two methods and one exception. I decided to generate a nullable wrapper for ADO.NET DataSets.

Target Code

To generate code, I need sample target code, so I know where I am going

DataRow

I plan on wrapping each typed DataRow in my own proxy class:

public class EmployeesRowX
{
    // The wrapped row
    NorthwindDS.EmployeesRow _row;
    public EmployeesRowX(NorthwindDS.EmployeesRow row)
    {
        _row = row;
    }
    // Direct access to the row, if you need something I didn’t implement
    public NorthwindDS.EmployeesRow Row
    {
        get { return _row; }
    }
    // …
}

For a non-nullable field, I just pass the data back without doing anything fancy:

public string FirstName
{
    get { return _row.FirstName; }
    set { Row.FirstName = value; }
}

For a nullable field, I have to translate ADO.NET the pre-nullable logic (IsXXXNull() & SetXXXNull()) to a modern nullable:

public DateTime? BirthDate
{
    get
    {
        if (_row.IsBirthDateNull())
            return null;
        else
            return _row.BirthDate;
    }
    set
    {
        if (!value.HasValue)
            _row.SetBirthDateNull();
        else
           _row.BirthDate = value.Value;
    }
}

DataSet and DataTables

I will wrap the DataSet and DataTables in their own proxies:

public class NorthwindDSX
{
    private NorthwindDS _ds;
    public NorthwindDSX(NorthwindDS ds)
    {
        _ds = ds;
    }

    public EmployeesDataTableX Employees
    {
        get { return new EmployeesDataTableX(_ds.Employees); }
    }

    public NorthwindDS DataSet
    {
        get { return _ds; }
    }
}

public class EmployeesDataTableX: IEnumerable
{
    private NorthwindDS.EmployeesDataTable _theTable;
    public EmployeesDataTableX(NorthwindDS.EmployeesDataTable table)
    {
        _theTable = table;
    }

    public NorthwindDS.EmployeesDataTable Table
    {
        get { return _theTable; }
    }

    #region IEnumerable Members
    public IEnumerator GetEnumerator()
    {
        return new EmployeesTableEnum(_theTable);
    }
    #endregion

    #region IEnumerable Members
    IEnumerator IEnumerable.GetEnumerator()
    {
        return (IEnumerator)this;
    }
    #endregion
}

DataTable’s IEnumberator<T>

And each Table needs an Enumerator:

public class EmployeesTableEnum : IEnumerator
{
    private NorthwindDS.EmployeesDataTable _collection;
    private int curIndex;
    private EmployeesRowX curBox;

    public EmployeesTableEnum(NorthwindDS.EmployeesDataTable collection)
    {
        _collection = collection;
        curIndex = -1;
        curBox = default(EmployeesRowX);
    }

    public bool MoveNext()
    {
        //Avoids going beyond the end of the collection.
        if (++curIndex >= _collection.Count)
            return false;
        else
            // Set current EmployeesRowX to next item in collection.
            curBox = new EmployeesRowX(
                (NorthwindDS.EmployeesRow)_collection.Rows[curIndex]);
        return true;
    }

    public void Reset() { curIndex = -1; }

    void IDisposable.Dispose() { }

    public EmployeesRowX Current
    {
        get { return curBox; }
    }

    object IEnumerator.Current
    {
        get { return Current; }
    }
}

Summing it up

So now that I know where I am going, I can get started at making generating code. The code will change as I work through the process.

Since a Typed DataSet is generated, it is possible to generate a full DataSet with nullable fields; that is a job for Microsoft.

Generating a Nullable ADO.NET DataSet Wrapper: The Series