In response to Scott's Mitchell's posting "Why I Don't Use DataSets in My ASP.NET Applications", I decided to document my decision process for determining whether or not to use a DataSet or a DataTable as opposed to a DataReader.

I decided to document this process in a code format, just because that was fun :-)


Private _SortingThreshold As Integer = 50
Private _CachingThreshold As Integer = 50
Private _NumberDevelopersOnProject As Integer = 1

Public NumberSortableDataScreens As Integer
Public NumberCacheableResultsets As Integer

Public Property NumberDevelopersOnProject As Integer
    Get
        Return _NumberDevelopersOnProject
    End Get
    Set(Value As Integer)
        _NumberDevelopersOnProject = Value

        'The Number of developers on the project helps
        'drive the level of sorting and caching that
        'can be done the old-fashioned way. These
        'numbers aren't solid, but the help
        'demonstrate the decision process.

        If _NumberDevelopersOnProject = 1 Then
            _SortingThreshold = 50
            _CachingThreshold = 50
        ElseIf _NumberDevelopersOnProject <= 5 Then
            _SortingThreshold = 25
            _CachingThreshold = 50
        Else 'More than 5 developers on the project
            _SortingThreshold = 25
            _CachingThreshold = 25
        End If
    End Set
End Property

Function ShouldUseDataSet(ControlToBind As Control, _
    HasNestedDataBinding As Boolean,
    IsCacheable As Boolean) As Boolean

    If ControlToBind Is DataGrid Then
        'If the datagrid allows sorting, we'll use a
        'DataView to apply the sort dynamically.
        'However, we'll retrieve the data from the
        'database each time, and apply sorting in
        'the BLL before the DataView is returned to
        'the Web.UI
        '
        'I know that this type of dynamic sorting can
        'be off-loaded to the stored procedure,
        'but you really have to jump through some hoops
        'to do that. With a large number of stored
        'procedures that can be GREATLY simplified by
        'off-loading the sorting to the .NET code... so
        'this parameter is really determined by the
        'size of the Application

        If NumberSortableDataScreens > _
        _SortingThreshold AndAlso _
        DirectCast(ControToBind, _
        DataGrid).AllowSorting Then
            Return True
        End If
    End If

    'If the page is rendering some nested databound
    'controls, then the DataSet is a no-brainer.
    'For example, I once had to create a display that
    'had many levels of nested data. The result of
    'the ASPX code was something like this:
    '
    '    DataList
    '        Repeater
    '            DataGrid
    '            DataGrid
    '
    'Having 2 DataGrids inside a Repeater inside
    'a DataList... the screen was working in less
    'than 4 hours, and looked pretty snazzy. I used
    'the RowFilter property of the DataView to pull
    'off the Nested DataBinding, and that has been
    'my typical approach with nested databinding
    'ever since

    If HasNestedDataBinding Then
        Return True
    End If

    'The current project I'm working on is a very
    'large ASP.NET application, with over 600 ASPX
    'screens for data maintenance. There are hundreds
    'of "lookup tables" in the application, except
    'this data can be configured by each client of the
    'system. So a lookup table may contain 100,000
    'records,however only 10 apply to each client.
    'For many of the dropdowns in the application that
    'are driven from these "lookup tables", the middle
    'layer of the application will cache the data for
    'each client. The client's ID (guid) is used as
    'part of the cache key to ensure that each client
    'has their own copy of the cached data.
    '
    'DataTables are used to cache this data because of
    'their simplicity mainly. We could create the
    'hundreds of custom objects, or find a design for
    'a generic object, and cache the data using those;
    'but that just really seems like reinventing the
    'wheel to me since the DataTable can support
    'all of these structures out of the box

    If NumberCacheableResultsets > _CachingThreshold _
    AndAlso IsCacheable Then
        Return True
    End If

    'If the data isn't sortable by the user, there
    'isn't nested databinding, and the data can't
    'be cached then we default back to a DataReader

    Return False

End Function