Introduction to Sitecore Data Providers

This is a quick follow-up post after my presentation on Data Providers at the Cardiff Sitecore User Group last night (P.S. the photo is from Ehab Elgindy – Ehab, I hope that’s OK!)

The PowerPoint presentation can be found here: INTRODUCTION TO DATA PROVIDERS

I’ve also been working on a set of helper classes for creating Data Providers more easily. I’ve just kind of splurged what I’ve got so far up to GitHub: Sitecore Base Data Provider.

It includes a simple example integration with a test JSON service.

Basically, to create a new Data Provider, all you need to do is create a few classes:

(1) A Helper Class (see ExampleDataProviderDamHelper.cs in the example)
(2) A Service Class (see ExampleServiceHelper.cs in the example).

The service class is just where you get you data (so in our example we just have a single method which gets all the users:

public List<IUser> GetUsers()
{
if (this.CacheHandler != null)
{
var cacheHit = this.CacheHandler.GetCachedItem("ExampleServiceHelper_GetUsers");
if (cacheHit != null)
{
return cacheHit;
}
}

var url = this.SettingsHelper.GetSetting("ExampleIntegration.ServiceUrl", "https://jsonplaceholder.typicode.com/users");

var response = this.ServiceHelper.CallService<List, IError>(url, new TypeConverter(),
new TypeConverter(), new TypeConverter(), new TypeConverter(), new TypeConverter());

if (response.Success)
{
if (this.CacheHandler != null)
{
this.CacheHandler.SaveCachedItem("ExampleServiceHelper_GetUsers",
response.ResponseItem, DateTime.UtcNow.AddMinutes(5));
}

return response.ResponseItem;
}
else
{
this.LogHelper.LogError("Web service call was unsuccessful", this);
return null;
}
}

The Helper Class abstracts the domain bits away from the Sitecore stuff – so the Sitecore stuff is fully separated from your data source. This does stuff like getting a list of Child keys, getting a collection of fields, getting the item name etc.

I’m planning on rewriting this with the Sitecore DI stuff (when I was first tinkering with this is was pre Sitecore DI. I also need to tidy it up and if you have any suggestions for features and functionality – please let me know.

My favourite bits so far is a pattern I’ve used on other integrations I’ve done – where you get NV fields containing the fields from your Data Source (which you can separate in to Public and Private (and set appropriate security) and also these fields can be mapped to custom Sitecore fields on your item (with an associated convertor class if required).

For example:

Public and Private Fields

Settings

Although it’s 6 years old – if you want to create your own Data Provider from scratch – I would thoroughly recommend starting with Nick Wesselman’s blog The Black Art of Sitecore Data Providers – it’s the best post on Data Providers I’ve seen.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s