Skip to main content
Home  ›  Blog

Custom DataSource API - AsEntity

Many data sources generate new content items - either because they deliver some kind of information, or because they convert data from another source into standardized entities. This is where AsEntity helps.

This is part of the new API for DataSources released in 2sxc 9.13.

How To Use AsEntity

Here's a simple example of the tutorial DateTime DataSource:

Using AsEntity in Code

// create demo 1 entity with values related to today
// first place all values in a dictionary 
// then convert into an Entity
var today = new Dictionary<string, object>
{
    {"Title", "Date Today"},
    {"Date", DateTime.Today},
    {"DayOfWeek", DateTime.Today.DayOfWeek.ToString()},
    {"DayOfWeekNumber", DateTime.Today.DayOfWeek}
};

// ...now convert to an entity, and add to the list of results
var basic = AsEntity(today); 
var recommended = AsEntity(today, "Title", "DateTimeInfo");

This example shows how an entity-object is build using the basic syntax AsEntity(today) or with more configuration.

Concept Behind AsEntity

Internally, AsEntity will generate an IEntity object which is a simpler version of the normal Entity, because it's actually an IEntityLight, meaning it's missing some advanced features like multi-language and repository identity (which would be important in edit-scenarios).

The simplest way is to just use AsEntity(someDictionary), more advanced uses also tell the system which field is the title, some numeric or Guid IDs and more. You can read about the advanced use cases in the wiki on AsEntity.

Love from Switzerland
iJungleboy


Daniel Mettler grew up in the jungles of Indonesia and is founder and CEO of 2sic internet solutions in Switzerland and Liechtenstein, an 20-head web specialist with over 800 DNN projects since 1999. He is also chief architect of 2sxc (see github), an open source module for creating attractive content and DNN Apps.

Read more posts by Daniel Mettler