Object Explosion Refactoring

I have inherited an application that uses some interesting design choices. I have been tasked with adding functionality. I would like to put my mantra of “improve everything you touch” in this process.

The application uses a standard nTier design pattern based on a collection of domain specific entities contained in a Buisness.Entities.dll. This library contains two types of objects: a BusinessObjectEntity and a corresponding BusinessObjectEntityCollection. The objects do not really model any real world items, but instead contain all of the data
elements needed to render a specific view.

Each object inherits from EntityBase which contains no implementation. Object collection classes inherit from EntityCollectionBase which appears to be a wrapper around List(Of T).

Here is an example of the implementation of one of the collection classes:

__ WARNING: vbnet CODE AHEAD __

Public Class InboxWorkItemCollection
    Inherits EDF.Entities.EntityCollectionBase(Of EntityBase)

    Public Overrides Function SetValues(
    ByVal DtReader As System.Data.IDataReader) As Boolean
        While (DtReader.Read())
            Dim objWI As New InboxWorkItem
            objWI.SetValues(DtReader)
            Me.Add(objWI)
        End While
        Return True
    End Function
End Class

There are ninety-one collection classes in the library that follow this pattern of reading from a Data Reader object, creating an instance of a specific class and adding it to the List collection.

Clearly there is an opportunity here to improve the design.

I came up with this:

Public Class TypedEntityCollection

    Private entities As List(Of EntityBase)

    Public Sub New(ByVal reader As System.Data.IDataReader, 
    ByVal type As Type)
    
    entities = New List(Of EntityBase)

        While (reader.Read())
            Dim item As EntityBase
            item = Activator.CreateInstance(type)
            item.SetValues(reader)
            entities.Add(item)
        End While
    End Sub
End Class

This new collection type still wraps List(Of T) but now expects a type parameter in its constructor. It uses this extra parameter to use create an instance of a specific class via reflection.

This appears to eliminate 90 classes and reduce the complexity of the library dramatically. How would you have approached the problem? How would you improve on my solution?

Now on to determine the level of duplication in the entity classes. Do we really have ninety-one domain objects? I think there might be even more commonality to find yet.

Follow me on Mastodon!