#10 Conversion Factory Options
Custom Dynamic DataSources - Factory Options
Requirements
In the previous examples, the custom DataSources simply returned anonymous objects. This is great, but sometimes you need more control. Some examples: For example,
-
All the data has the same Content Type Name
unspecified
. This would prevent you from filtering the data by Content Type Name when you're using multiple dynamic sources. - By default, all data with ID 0 (zero) would get an automatic ID assigned - counting up from one. But in rare cases, an ID 0 is really expected.
-
The title field name is
Title
by default. But in some cases you would preferName
or something else.
To handle all this, you can use special FactoryOptions which configure how the data is converted to a real IEntity
.
Convert with Custom DataFactoryOptions
This example will change how the automatic ID is generated, what the ContentTypeName is, and which field to use for the EntityTitle.
Data in the DataSource (4)
- Greeting from FactoryOptions (id: 1000, type: MyContentType)
- Greeting from FactoryOptions (id: 1001, type: MyContentType)
- Greeting from FactoryOptions (id: 1002, type: MyContentType)
- Greeting from FactoryOptions (id: 1003, type: MyContentType)
#10 Conversion Factory Options
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 - Factory Options</h2> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <p> In the previous examples, the custom DataSources simply returned anonymous objects. This is great, but sometimes you need more control. Some examples: For example, </p> <ul> <li> All the data has the same Content Type Name <code>unspecified</code>. This would prevent you from filtering the data by Content Type Name when you're using multiple dynamic sources. </li> <li> By default, all data with ID 0 (zero) would get an automatic ID assigned - counting up from one. But in rare cases, an ID 0 is really expected. </li> <li> The title field name is <code>Title</code> by default. But in some cases you would prefer <code>Name</code> or something else. </li> </ul> <p> To handle all this, you can use special FactoryOptions which configure how the data is converted to a real <code>IEntity</code>. </p> </div> </div> Convert with Custom DataFactoryOptions... <!-- unimportant stuff, hidden --> @{ var dsWithOptions = Kit.Data.GetSource(name: "FactoryOptions"); } <h3>Data in the DataSource (@dsWithOptions.List.Count())</h3> <ul> @foreach (var item in AsList(dsWithOptions)) { <li> <strong>@item.EntityTitle</strong> (id: @item.EntityId, type: @item.EntityType) </li> } </ul> @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })