- Nice I love D3! gag.gl/eK0juj 6 hours ago
- RT @blackgirltech: If you're a Black woman & want to learn how to code but need some financial help, you should apply for the https://t.co/… 18 hours ago
- Louie is growing so fast! https://t.co/hfvOqScGp3 20 hours ago
- RT @vibronet: Open standards should be table stakes for everyone - but won’t be enough to save you from vendor lock-in https://t.co/TbMebtG… 1 day ago
- RT @eugenio_pace: Find out why @auth0’s @vibronet says open standards alone in identity !== vendor lock-in. via @auth0 https://t.co/sRg86Rx… 1 day ago
Object Explosion Refactoring
March 28, 2008Posted by on
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
WARNING: VB.NET 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 areally have ninety-one domain objects? I think there might be even more commonality to find yet.