How It's Done: SO Integration Part 1 - The Mighty Macro Data Source

   —   

In one of my previous articles, I promised I'd bring you some details about how the StackOverflow integration on DevNet has been done. Today, I'll focus on how the Macro Data Source web part saved us a lot of time and how flexible it is, compared to traditional data sources.

The Challenge

The task was to enrich the DevNet Q&A section with questions from StackOverflow. As I mentioned in the previous article about the StackOverflow integration, questions in the Q&A section are implemented as a special page type. It comes as no surprise that the presentation part consisted of a repeater bound to a Pages Data Source web part. The repeater ensured the initial page load and then there was a WCF service that handled all subsequent actions done via AJAX, such as searching or loading more questions. Because we didn't want to copy content from StackOverflow to DevNet, it was obvious we wouldn't be able to use Pages Data Source anymore, and the WCF service would have to be adjusted as well.

The Solution

We had to extend the code behind the WCF service and find a way to reuse it also in the repeater. From a code maintenance and cleanness perspective, it was desirable that we used a single source of data for both the repeater and the service.

Extending the service went smoothly, and we faced the issue of finding something more flexible to replace the Pages Data Source with. We found two options:

Well, it was decided that we would go with the second option, and we were really happy with the choice. Exposing a method to a macro engine is only a matter of adding a few attributes to it and configuration of the web part didn't take us more than a minute.

The architecture was changed as follows:

Q&A Architecture change

Macro Data Source

Data sources in Kentico are web parts that produce data in the form of collections of items that are typically consumed by other web parts like repeaters or datalists, which display the data. Macro Data Source emits the data by evaluating a given macro expression.

Choosing the right return type for a macro

What type of data should the Macro Data Source return to be accepted by a repeater? All Kentico's repeater web parts, including Basic Repeater, are based on the standard .NET's System.Web.UI.WebControls.Repeater. This means the same rules apply to its DataSource property. The MSDN says:

The DataSource can be any System.Collections.IEnumerable collection, such as a System.Data.DataView for accessing databases, a System.Collections.ArrayList, or an array, or an IListSource object.

E.g. in the case of the DevNet's Q&As, we are returning IEnumerable<Question>.

Configuring the web part

In terms of configuration, the Macro Data Source is one of the simplest web parts. Apart from the standard caching options, there's really only one thing to configure - the macro expression itself. You can enter any built-in or custom expression that returns one of the above-mentioned types. But beware, in Kentico versions prior to 9, you have to enter the macro without enclosing brackets {%%}. From version 9, this is no longer the case, and you have to wrap the macro in the curly brackets with a percent sign.

Getting more flexibility with parameters

Classic data source web parts offer you sets of parameters relevant to given data sources. With a Macro Data Source, no extra fields are necessary as the macro method behind can be parametrized directly - you just add and register the parameters of the method and use them in your expressions. Consider the following example, where we adjust number of returned items by letting the macro engine evaluate a query string value as a parameter of a TopN method:

Parameterizing Macro Data Source

Examples

You can find a great example of the Macro Data Source on the Dancing Goat sample site, where it's used to list orders under the "Your account" page.

Macro Data Source example on Dancing Goat sample site

Summary

The Macro Data Source web part presents a very flexible way of binding data by repeaters, datalists, and datagrids. Every time you are about to create a new data source web part, consider whether you cannot achieve the same result easier and faster utilizing the Macro Data Source.

Share this article on   LinkedIn Google+

Petr Svihlik

I listen to the voice of community and act in its interest.

Comments