Class ModelFromEntity
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 ModelFromEntity : ICanWrap<IEntity>, ICanWrapData, IMultiWrapper<IEntity>, IEquatable<IEntity>
- Inheritance
-
objectModelFromEntity
- Implements
Examples
Usage ca. like this:
- A custom data model in
AppCode.Data
which 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)
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
_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
objectprotector
NoParamOrdernullIfNull
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
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
TValue to provide if nothing was found - required
propertyName
stringThe 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
ToString()
Override ToString to give more information about the current object
public override string ToString()
Returns
Operators
operator ==(ModelFromEntity, ModelFromEntity)
Ensure that the equality check is done correctly. If two objects wrap the same item, they will be considered equal.
public static bool operator ==(ModelFromEntity a, ModelFromEntity b)
Parameters
a
ModelFromEntityfirst item to compare
b
ModelFromEntitysecond item to compare
Returns
- bool
true, if both wrappers are the same type and wrap the same entity
operator !=(ModelFromEntity, ModelFromEntity)
Ensure that the equality check is done correctly. If two objects wrap the same item, they will be considered equal.
public static bool operator !=(ModelFromEntity item1, ModelFromEntity item2)
Parameters
item1
ModelFromEntityfirst item to compare
item2
ModelFromEntitysecond item to compare
Returns
- bool
false, if both wrappers are the same type and wrap the same entity