Class ModelOfEntityClassic
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
-
objectModelOfEntityClassic
- Implements
- Extension Methods
Examples
Usage ca. like this:
- A custom data model in
AppCode.Datawhich inherits from this class (usually generated by 2sxc Copilot) - 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
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
_entityto 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
bobject
Returns
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
fallbackTValue to provide if nothing was found - required
propertyNamestringThe property name - will be autofill by the compiler
Returns
- T
The typed value
Type Parameters
TOptional type, usually auto-detected because of the
fallbackvalue
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
sourceIEntity
Returns
- bool
trueif all is ok,falseif not. A typical example would be a wrapper which is setup withnulland it cannot work that way. On the other hand, if a wrapper is ok with a null source, it would returntrue.
ToString()
Override ToString to give more information about the current object
public override string ToString()
Returns
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
aModelOfEntityClassicfirst item to compare
bModelOfEntityClassicsecond 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
item1ModelOfEntityClassicfirst item to compare
item2ModelOfEntityClassicsecond item to compare
Returns
- bool
false, if both wrappers are the same type and wrap the same entity