Table of Contents

Class DataModel

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 IEntity and provides a simple way to access the data.

[InternalApi_DoNotUse_MayChangeWithoutNotice("Still beta, name may change to CustomModelOfItem or something")]
public abstract class DataModel : ICanWrap<IEntity>, ICanWrapData, IEquatable<IEntity>
Inheritance
object
DataModel
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 : DataModel
  {
    public string Name => _entity.Get<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

_entity

The underlying entity - for inheriting classes to access.

protected IEntity _entity { get; }

Property Value

IEntity

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 _entity 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 ==(DataModel, DataModel)

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

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

Parameters

item1 DataModel

first item to compare

item2 DataModel

second item to compare

Returns

bool

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

operator !=(DataModel, DataModel)

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

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

Parameters

item1 DataModel

first item to compare

item2 DataModel

second item to compare

Returns

bool

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