Table of Contents

Class ToModelExtensions

Namespace
ToSic.Eav.Models
Assembly
ToSic.Eav.Data.dll

Extension methods to convert IEntity or lists of IEntity to Models. WIP v21 TODO DOCS

[WorkInProgressApi("WIP v21")]
public static class ToModelExtensions
Inheritance
object
ToModelExtensions

Methods

FirstModel<TModel>(IEnumerable<IEntity>?)

Returns the first entity that matches the specified type name, or null if not found.

public static TModel? FirstModel<TModel>(this IEnumerable<IEntity>? list) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

The collection of entities to search.

Returns

TModel

The first entity whose type matches the specified type name wrapped into the target model, or null if no matching entity is found.

Type Parameters

TModel

The target model to convert to.

FirstModel<TModel>(IEnumerable<IEntity>?, IModelFactory, NoParamOrder, ToModelOptions?)

Returns the first entity that matches the specified type name, or null if not found.

public static TModel? FirstModel<TModel>(this IEnumerable<IEntity>? list, IModelFactory factory, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

The collection of entities to search.

factory IModelFactory

A factory to create the target model.

npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

TModel

The first entity whose type matches the specified type name wrapped into the target model, or null if no matching entity is found.

Type Parameters

TModel

The target model to convert to.

FirstModel<TModel>(IEnumerable<IEntity>?, NoParamOrder, ToModelOptions?)

Returns the first entity that matches the specified type name, or null if not found.

public static TModel? FirstModel<TModel>(this IEnumerable<IEntity>? list, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

The collection of entities to search.

npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

TModel

The first entity whose type matches the specified type name wrapped into the target model, or null if no matching entity is found.

Type Parameters

TModel

The target model to convert to.

GetMetadataModel<TModel>(IHasMetadata)

Get a typed metadata from an object which has metadata. Will return null if no data found.

public static TModel? GetMetadataModel<TModel>(this IHasMetadata parent) where TModel : class, IModelFromEntity

Parameters

parent IHasMetadata

An object which has metadata.

Returns

TModel

Type Parameters

TModel

Any model object or interface.

GetMetadataModel<TModel>(IHasMetadata, IModelFactory, NoParamOrder, ToModelOptions?)

public static TModel? GetMetadataModel<TModel>(this IHasMetadata parent, IModelFactory factory, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

parent IHasMetadata
factory IModelFactory
npo NoParamOrder
options ToModelOptions

Returns

TModel

Type Parameters

TModel

GetMetadataModel<TModel>(IHasMetadata, NoParamOrder, ToModelOptions?)

Get a typed metadata from an object which has metadata. Will return null if no data found, unless specified otherwise in the options.

public static TModel? GetMetadataModel<TModel>(this IHasMetadata parent, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

parent IHasMetadata

An object which has metadata.

npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

TModel

Type Parameters

TModel

Any model object or interface.

GetModel<TModel>(IEnumerable<IEntity>?, Guid, NoParamOrder, ToModelOptions?)

Returns the first entity that matches the specified type name, or null if not found.

public static TModel? GetModel<TModel>(this IEnumerable<IEntity>? list, Guid guid, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

The collection of entities to search.

guid Guid
npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

TModel

The first entity whose type matches the specified type name wrapped into the target model, or null if no matching entity is found.

Type Parameters

TModel

The target model to convert to.

GetModel<TModel>(IEnumerable<IEntity>?, int, NoParamOrder, ToModelOptions?)

Returns the first entity that matches the specified type name, or null if not found.

public static TModel? GetModel<TModel>(this IEnumerable<IEntity>? list, int id, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

The collection of entities to search.

id int
npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

TModel

The first entity whose type matches the specified type name wrapped into the target model, or null if no matching entity is found.

Type Parameters

TModel

The target model to convert to.

GetModels<TModel>(IEnumerable<IEntity>?)

Filter data by type matching the model, and return as a collection of type TModel.

public static IEnumerable<TModel?> GetModels<TModel>(this IEnumerable<IEntity>? list) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

The collection of entities to filter and wrap.

Returns

IEnumerable<TModel>

An enumerable collection of wrapped entities of the specified model type. Returns an empty collection if the input is null or contains no matching entities.

Type Parameters

TModel

The model type to wrap each entity. Must implement IModelFromEntity and should implement IModelSetup<TSource>. The type-name being filtered is derived from the model name, or from the ModelSpecsAttribute on the model.

GetModels<TModel>(IEnumerable<IEntity>?, IModelFactory, NoParamOrder, ToModelOptions?)

Returns a collection of wrapper objects of type TModel for all entities of the specified type name.

public static IEnumerable<TModel?> GetModels<TModel>(this IEnumerable<IEntity>? list, IModelFactory factory, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

The source collection of entities to search. Can be null.

factory IModelFactory

The factory to use for creating wrapper instances.

npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

IEnumerable<TModel>

An enumerable collection of TModel instances wrapping the matching entities. Returns an empty collection if the source is null or no matching entities are found.

Type Parameters

TModel

The model type to wrap each entity. Must implement IWrapperSetup{IEntity} and have a parameterless constructor.

GetModels<TModel>(IEnumerable<IEntity>?, NoParamOrder, ToModelOptions?)

Filter data by type matching the model, and return as a collection of type TModel.

public static IEnumerable<TModel?> GetModels<TModel>(this IEnumerable<IEntity>? list, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

The source collection of entities to search. Can be null.

npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

IEnumerable<TModel>

An enumerable collection of TModel instances wrapping the matching entities. Returns an empty collection if the source is null or no matching entities are found.

Type Parameters

TModel

The model type to wrap each entity. Must implement IModelFromEntity and should implement IModelSetup<TSource>. The type-name being filtered is used from ToModelOptions, derived from the model name, or from the ModelSpecsAttribute on the model.

ToModel<TModel>(ICanBeEntity?, NoParamOrder, ToModelOptions?)

WIP Convert something which can be an entity to a model of type TModel. This is only meant for simple models that do not require a factory.

public static TModel? ToModel<TModel>(this ICanBeEntity? canBeEntity, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

canBeEntity ICanBeEntity
npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

TModel

Type Parameters

TModel

TModel must implement IWrapperSetup<IEntity> and have a parameterless constructor.

Exceptions

InvalidCastException

ToModel<TModel>(IEntity?)

WIP Convert an IEntity to a model of type TModel. This is only meant for simple models that do not require a factory.

public static TModel? ToModel<TModel>(this IEntity? entity) where TModel : class, IModelFromEntity

Parameters

entity IEntity

Returns

TModel

Type Parameters

TModel

TModel must implement IWrapperSetup<IEntity> and have a parameterless constructor.

Exceptions

InvalidCastException

ToModel<TModel>(IEntity, IModelFactory, NoParamOrder, ToModelOptions?)

WIP, v21 requiring factory...

public static TModel? ToModel<TModel>(this IEntity entity, IModelFactory factory, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

entity IEntity
factory IModelFactory
npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

TModel

Type Parameters

TModel

Exceptions

ArgumentNullException

ToModel<TModel>(IEntity?, NoParamOrder, ToModelOptions?)

WIP Convert an IEntity to a model of type TModel. This is only meant for simple models that do not require a factory.

public static TModel? ToModel<TModel>(this IEntity? entity, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

entity IEntity
npo NoParamOrder

see Convention: Named Parameters

options ToModelOptions

Conversion options

Returns

TModel

Type Parameters

TModel

TModel must implement IWrapperSetup<IEntity> and have a parameterless constructor.

Exceptions

InvalidCastException

ToModels<TModel>(IEnumerable<IEntity?>?)

public static IEnumerable<TModel> ToModels<TModel>(this IEnumerable<IEntity?>? list) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

Returns

IEnumerable<TModel>

Type Parameters

TModel

ToModels<TModel>(IEnumerable<IEntity?>?, IModelFactory, NoParamOrder, ToModelOptions?)

public static IEnumerable<TModel> ToModels<TModel>(this IEnumerable<IEntity?>? list, IModelFactory factory, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>
factory IModelFactory
npo NoParamOrder
options ToModelOptions

Returns

IEnumerable<TModel>

Type Parameters

TModel

ToModels<TModel>(IEnumerable<IEntity?>?, NoParamOrder, ToModelOptions?)

public static IEnumerable<TModel> ToModels<TModel>(this IEnumerable<IEntity?>? list, NoParamOrder npo = default, ToModelOptions? options = null) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>
npo NoParamOrder
options ToModelOptions

Returns

IEnumerable<TModel>

Type Parameters

TModel