Table of Contents

Class ModelOfEntityClassic

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

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

[InternalApi_DoNotUse_MayChangeWithoutNotice("Still beta, name may change")]
public abstract class ModelOfEntityClassic : IDataWrapper, ICanBeEntity, IModelSetup<IEntity>, IModelFactoryRequired, IMultiWrapper<IEntity>, IEquatable<IEntity>
Inheritance
object
ModelOfEntityClassic
Implements
Extension Methods

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 int Id => Entity.EntityId;
    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)
  • Stabilizing in v21 (now first class citizen, part of ToSic.Eav.Models)

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

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

GetThis<T>(T?, string?)

Get a value from the underlying entity, whose name matches the property requesting this. So if your C# property is called Birthday it will also get the field Birthday in the entity.

protected T? GetThis<T>(T? fallback, string? propertyName = null)

Parameters

fallback T

Value to provide if nothing was found - required

propertyName string

The property name - will be autofill by the compiler

Returns

T

The typed value

Type Parameters

T

Optional type, usually auto-detected because of the fallback value

SetupModel(IEntity?)

Add the contents to use for the wrapper. We are not doing this in the constructor, because the object needs to have an empty or DI-compatible constructor.

public bool SetupModel(IEntity? source)

Parameters

source IEntity

Returns

bool

true if all is ok, false if not. A typical example would be a wrapper which is setup with null and it cannot work that way. On the other hand, if a wrapper is ok with a null source, it would return true.

ToString()

Override ToString to give more information about the current object

public override string ToString()

Returns

string

Operators

operator ==(ModelOfEntityClassic, ModelOfEntityClassic)

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

public static bool operator ==(ModelOfEntityClassic a, ModelOfEntityClassic b)

Parameters

a ModelOfEntityClassic

first item to compare

b ModelOfEntityClassic

second item to compare

Returns

bool

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

operator !=(ModelOfEntityClassic, ModelOfEntityClassic)

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

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

Parameters

item1 ModelOfEntityClassic

first item to compare

item2 ModelOfEntityClassic

second item to compare

Returns

bool

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