Kentico DataQuery API – Hydrating a datarow
Posted 20 Oct 2016
When working with Kentico’s APIs you sometimes end up with a DataSet. A good example of this is the Search API. For use with transformations in the PortalEngine that’s usually not an issue. MVC however is very much geared towards dealing with strongly typed data. Fortunately you can easily go from a data row to a page or module class using the InfoDataSet class.
Under the hood
At the core of Kentico’s data access implementation is a key architectural descision: all data objects rely on a dictionary internally for the actual values of the properties.
So in the end, all the code you generate from Kentico for custom classes and page types is just a wrapper around that dictionary. You can see this when inspecting the code; the properties don’t have backing fields but instead use the GetValue / SetValue methods to interact with the dictionary.
The nice thing about this architecture is that it makes databinding really easy. There is no need to use reflection to dig up the PropertyInfo, you can call GetValue with the property name.
This is how databinding in XML transforms works. And it’s also the reason that you cannot bind POCO’s in XML transformations directly.
The DataRows in a Table in a DataSet are in effect aldo dictionaries so, given the architecture of Kentico’s data objects, you can probably imagine how easy it is to go from a datarow to string typed Kentico content. To make it even easier there’s the InfoDataSet class. You create the class with a Kentico data object type as the parameter and it can then turn a DataRow into a typed object:
var set = new InfoDataSet<mypage>(); var typedObject = set.GetNewObject(dataRow);
If you use InfoDataSet like this, it is ofcourse up to you to ensure that the data in the row matches with the data required by the class.