Custom Dynamic DataSources - Introduction
Here we'll give you a first taste of Custom Dynamic DataSources - a new feature in 2sxc 15.
This allows you to create DataSources directly in your App, without compiling to DLL or anything.
My First Custom Dynamic DataSource
This is a 10-line super-simple example of a DataSource called Basic101
.
It will only return a single item with the answer to the meaning of life đŸ˜‰.
Afterwards we'll explain in more detail what's happening.
List of Data in the CSV DataSource (1)
- Hello from Basic101 - the Answer: 42
How Custom Dynamic DataSources Work
This is the same sample as before, but with a lot more explanations. Here's what's happening:
- Using
Kit.Data.GetSource(name: "...")
we retrieve the DataSource using the name - The name references a file with the same name
Basics101.cs
located in theDataSources
folder of the current App. - The rest of the magic is explained in the source code of the DataSource - see below.
List of Data in the CSV DataSource (1)
- Hello from Basic101-Commented - the Answer: 42
Source Code of this file
Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code
@inherits Custom.Hybrid.Razor14 @using ToSic.Razor.Blade; @using System.Linq; @using ToSic.Eav.DataSources; <!-- unimportant stuff, hidden --> <h2>Custom Dynamic DataSources - Introduction</h2> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <p> Here we'll give you a first taste of <strong>Custom Dynamic DataSources</strong> - a new feature in 2sxc 15. </p> <p> This allows you to create DataSources directly in your App, without compiling to DLL or anything. </p> </div> </div> My First Custom Dynamic DataSource This... <!-- unimportant stuff, hidden --> @{ // Create the Dynamic DataSource with the name "Basic101" var basic101 = Kit.Data.GetSource(name: "Basic101"); } <h3>List of Data in the CSV DataSource (@basic101.List.Count())</h3> <ul> @foreach (var item in AsList(basic101)) { <li> <strong>@item.Title</strong> - the Answer: @item.TheAnswer </li> } </ul> How Custom Dynamic DataSources Work This... <!-- unimportant stuff, hidden --> @{ // Create the Dynamic DataSource with the name "Basic101Commented" var basic101c = Kit.Data.GetSource(name: "Basic101Commented"); } <h3>List of Data in the CSV DataSource (@basic101c.List.Count())</h3> <ul> @foreach (var item in AsList(basic101c)) { <li> <strong>@item.Title</strong> - the Answer: @item.TheAnswer </li> } </ul> @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })