Table of Contents

Class DataModelOfItem

Namespace
ToSic.Sxc.Data.Model
Assembly
ToSic.Sxc.dll

BETA / WIP: Base class for plain custom data models and can be used in Razor Components. It wraps a ITypedItem and provides a simple way to access the data.

[InternalApi_DoNotUse_MayChangeWithoutNotice("Still beta, name may change to DataModelWithItem or something")]
public abstract class DataModelOfItem : ICanWrap<ITypedItem>, ICanWrapData, IEquatable<ITypedItem>
Inheritance
object
DataModelOfItem
Implements

Examples

Usage ca. like this:

  1. A custom data model in AppCode.Data which inherits from this class (usually generated by 2sxc Copilot)
  2. Razor code which uses it to convert typed items into this custom data model

Example trivial custom plain data model:

namespace AppCode.Data
{
  class MyPerson : DataModelOfItem
  {
    public string Name => _item.String("Name");
  }
}

Example usage in Razor:

@inherits Custom.Hybrid.RazorTyped
@using AppCode.Data
@{
  var person = As<MyPerson>(MyItem);
}
<span>@person.Name</span>

Remarks

This is much lighter than the CustomItem which also wraps data, as it doesn't have any predefined properties and doesn't have the ITypedItem APIs.

History

  • Released in v19.01 (BETA)

Properties

_item

The underlying item - for inheriting classes to access.

protected ITypedItem _item { get; }

Property Value

ITypedItem

Remarks

  • this property is protected, not public, as it should only be used internally.
  • this also prevents it from being serialized in JSON, which is good.
  • it uses an unusual name _item to avoid naming conflicts with properties generated in inheriting classes.

Methods

AsList<T>(object, NoParamOrder, bool)

Convert a list of Entities or TypedItems into a strongly typed list. Typically, the type will be from your AppCode.Data.

protected IEnumerable<T> AsList<T>(object source, NoParamOrder protector = default, bool nullIfNull = false) where T : class, ICanWrapData

Parameters

source object
protector NoParamOrder
nullIfNull bool

Returns

IEnumerable<T>

Type Parameters

T

As<T>(object)

Convert an Entity or TypedItem into a strongly typed object. Typically, the type will be from your AppCode.Data.

protected T As<T>(object item) where T : class, ICanWrapData

Parameters

item object

Returns

T

Type Parameters

T

Equals(object)

Ensure that the equality check is done correctly. If two objects wrap the same item, they will be considered equal.

public override bool Equals(object b)

Parameters

b object

Returns

bool

ToString()

Override ToString to give more information about the current object

public override string ToString()

Returns

string

Operators

operator ==(DataModelOfItem, DataModelOfItem)

Ensure that the equality check is done correctly. If two objects wrap the same item, they will be considered equal.

public static bool operator ==(DataModelOfItem item1, DataModelOfItem item2)

Parameters

item1 DataModelOfItem

first item to compare

item2 DataModelOfItem

second item to compare

Returns

bool

true, if both wrappers are the same type and wrap the same entity

operator !=(DataModelOfItem, DataModelOfItem)

Ensure that the equality check is done correctly. If two objects wrap the same item, they will be considered equal.

public static bool operator !=(DataModelOfItem item1, DataModelOfItem item2)

Parameters

item1 DataModelOfItem

first item to compare

item2 DataModelOfItem

second item to compare

Returns

bool

false, if both wrappers are the same type and wrap the same entity