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")]
[WorkInProgressApi("beta 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, new()

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, string?)

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, string? typeName = 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

typeName string

The name of the type to match.

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, string?, ModelNullHandling)

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, string? typeName = null, ModelNullHandling nullHandling = ModelNullHandling.Undefined) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

The collection of entities to search.

npo NoParamOrder

see Convention: Named Parameters

typeName string

The name of the type to match.

nullHandling ModelNullHandling

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, NoParamOrder)

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, NoParamOrder npo = default) where TModel : class, IModelFromEntity

Parameters

parent IHasMetadata

An object which has metadata.

npo NoParamOrder

see Convention: Named Parameters

Returns

TModel

Type Parameters

TModel

Any model object or interface.

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

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, bool skipTypeCheck = false, ModelNullHandling nullHandling = ModelNullHandling.Undefined) where TModel : class, IModelFromEntity, new()

Parameters

list IEnumerable<IEntity>

The collection of entities to search.

guid Guid
npo NoParamOrder

see Convention: Named Parameters

skipTypeCheck bool
nullHandling ModelNullHandling

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, bool, ModelNullHandling)

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, bool skipTypeCheck = false, ModelNullHandling nullHandling = ModelNullHandling.Undefined) where TModel : class, IModelFromEntity, new()

Parameters

list IEnumerable<IEntity>

The collection of entities to search.

id int
npo NoParamOrder

see Convention: Named Parameters

skipTypeCheck bool
nullHandling ModelNullHandling

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, string?)

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, string? typeName = 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

typeName string

The name identifier of the entity type to filter by. This value is used to select entities of a specific type.

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, string?, ModelNullHandling)

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, string? typeName = null, ModelNullHandling nullHandling = ModelNullHandling.Undefined) where TModel : class, IModelFromEntity

Parameters

list IEnumerable<IEntity>

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

npo NoParamOrder

see Convention: Named Parameters

typeName string

The name identifier of the entity type to filter by. This value is used to select entities of a specific type.

nullHandling ModelNullHandling

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 typeName, derived from the model name, or from the ModelSpecsAttribute on the model.

ToModel<TModel>(ICanBeEntity?, NoParamOrder, bool, ModelNullHandling)

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, bool skipTypeCheck = false, ModelNullHandling nullHandling = ModelNullHandling.Undefined) where TModel : class, IModelFromEntity

Parameters

canBeEntity ICanBeEntity
npo NoParamOrder

see Convention: Named Parameters

skipTypeCheck bool

allow conversion even if the Content-Type of the entity doesn't match the type specified in the parameter T

nullHandling ModelNullHandling

How to handle nulls during the conversion - default is ToSic.Eav.Models.ModelNullHandling.Default

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)

WIP, v21 requiring factory...

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

Parameters

entity IEntity
factory IModelFactory

Returns

TModel

Type Parameters

TModel

Exceptions

ArgumentNullException

ToModel<TModel>(IEntity?, NoParamOrder, bool, ModelNullHandling)

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, bool skipTypeCheck = false, ModelNullHandling nullHandling = ModelNullHandling.Undefined) where TModel : class, IModelFromEntity

Parameters

entity IEntity
npo NoParamOrder

see Convention: Named Parameters

skipTypeCheck bool

allow conversion even if the Content-Type of the entity doesn't match the type specified in the parameter T

nullHandling ModelNullHandling

How to handle nulls during the conversion - default is ToSic.Eav.Models.ModelNullHandling.Default

Returns

TModel

Type Parameters

TModel

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

Exceptions

InvalidCastException

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

public static IEnumerable<TModel> ToModels<TModel>(this IEnumerable<IEntity?> entities, NoParamOrder npo = default) where TModel : class, IModelFromEntity, new()

Parameters

entities IEnumerable<IEntity>
npo NoParamOrder

Returns

IEnumerable<TModel>

Type Parameters

TModel