This is the fourth article on some of the features in the upcoming 4.6.0 release. In this article I continue the discussion on the new interface - IHydratable. In the previous blog I discussed the use of the new "Fill" method. In this article I focus on the new property KeyID.
If you implement IHydratable on your Business Entity class (xxxInfo) then you will be required to add the KeyID property. This property should be the property that maps to the Primary Key field in the corresponding database table. In most situations (especially when you already have an entity class) you will already have a property that represents the primary key in your database. In this scenario the KeyID will need to "wrap" the existing property. eg for Annoucnements whose primary key property is currently ItemID this would be:
Public Property KeyID() As Integer Implements IHydratable.KeyID
Get
Return ItemId
End Get
Set(ByVal value As Integer)
ItemId = value
End Set
End Property
So what does this new property do? Why is it included in this interface? The answer lies in the new CBO method FillDictionary(Of T As IHydratable). A Dictionary is a generic keyed collection which allows you to lookup the value based a key. This new CBO class allows you to fill a generic dictionary in the same way as the other CBO methods allow you to return ArrayLists or generic Lists.
Public Shared Function FillDictionary(Of T As IHydratable)(ByVal dr As IDataReader, ByRef objToFill As IDictionary(Of Integer, T)) As IDictionary(Of Integer, T)
Dim objFillObject As T
' iterate datareader
While dr.Read
' fill business object
objFillObject = CreateObject(Of T)(dr)
' add to dictionary
If objFillObject IsNot Nothing Then
objToFill.Add(objFillObject.KeyID, objFillObject)
End If
End While
' close datareader
If Not dr Is Nothing Then
dr.Close()
End If
Return objToFill
End Function
The KeyID property provides a known property that can be used as the key to this Dictionary.
Ok - so that is why IHydratable has the KeyID. So what? Well the answer to this question is that a Dictionary is very useful when implementing caching in a Module. In this scenario a developer could create a Business Layer method to fetch all the items, fill a Dictionary and cache the dictionary. To retrieve an individual item now involves fetching the Dictionary from the cache, and using the KeyID to retrieve a specific item.
BTW - I will be providing more information on this interface and how it can help module developers in my OpenForce Europe talk on Module Development - so see you there.