Interface ITyped
Objects which usually wrap other objects to provide strictly typed access to properties.
[PublicApi]
[JsonConverter(typeof(DynamicJsonConverter))]
public interface ITyped
Remarks
The object will have typed Methods to read properties like .String(propName).
It is usually the result of a AsTyped(something) or AsItem(...) command.
It is meant to help Razor etc. access Entity and/or dynamic objects in a typed way.
History: Introduced in 16.02.
Methods
Attribute(string, NoParamOrder, string?, bool?)
Return a value as a raw HTML string for using inside an attribute.
Usage like title='@item.Attribute("Title")'
It will do a few things such as:
- Ensure dates are in the ISO format
- Ensure numbers are in a neutral format such as
14.27and never14,27 - Html encode any characters which would cause trouble such as quotes
IRawHtmlString? Attribute(string name, NoParamOrder noParamOrder = default, string? fallback = null, bool? required = null)
Parameters
namestringName of the property
noParamOrderNoParamOrderfallbackstringValue to use if the property specified by
namedoesn't existrequiredbool?throw error if
namedoesn't exist, see Convention: Property Required Name Checks
Returns
- IRawHtmlString
Bool(string, NoParamOrder, bool, bool?)
Get a property and return the value as a bool.
If conversion fails, will return default false or what is specified in the fallback.
bool Bool(string name, NoParamOrder noParamOrder = default, bool fallback = false, bool? required = null)
Parameters
namestringproperty name
noParamOrderNoParamOrderfallbackbooloptional fallback if conversion fails
requiredbool?throw error if the
namedoesn't exist, see Convention: Property Required Name Checks
Returns
- bool
Value as
bool
ContainsKey(string)
Check if this typed object has a property of this specified name. It's case-insensitive.
bool ContainsKey(string name)
Parameters
namestringthe name like
Image; some objects also support path to sub-property likeAuthor.Name
Returns
Remarks
Adding in 16.03 (WIP)
DateTime(string, NoParamOrder, DateTime, bool?)
Get a property and return the value as a DateTime.
If conversion fails, will return default 0001-01-01 or what is specified in the fallback.
DateTime DateTime(string name, NoParamOrder noParamOrder = default, DateTime fallback = default, bool? required = null)
Parameters
namestringproperty name
noParamOrderNoParamOrderfallbackDateTimeoptional fallback if conversion fails
requiredbool?throw error if the
namedoesn't exist, see Convention: Property Required Name Checks
Returns
- DateTime
Value as
DateTime
Decimal(string, NoParamOrder, decimal, bool?)
Get a property and return the value as a decimal.
If conversion fails, will return default 0 or what is specified in the fallback.
decimal Decimal(string name, NoParamOrder noParamOrder = default, decimal fallback = 0, bool? required = null)
Parameters
namestringproperty name
noParamOrderNoParamOrderfallbackdecimaloptional fallback if conversion fails
requiredbool?throw error if the
namedoesn't exist, see Convention: Property Required Name Checks
Returns
- decimal
Value as
decimal
Double(string, NoParamOrder, double, bool?)
Get a property and return the value as a double.
If conversion fails, will return default 0 or what is specified in the fallback.
double Double(string name, NoParamOrder noParamOrder = default, double fallback = 0, bool? required = null)
Parameters
namestringproperty name
noParamOrderNoParamOrderfallbackdoubleoptional fallback if conversion fails
requiredbool?throw error if the
namedoesn't exist, see Convention: Property Required Name Checks
Returns
- double
Value as
double
Float(string, NoParamOrder, float, bool?)
Get a property and return the value as a float.
If conversion fails, will return default 0 or what is specified in the fallback.
float Float(string name, NoParamOrder noParamOrder = default, float fallback = 0, bool? required = null)
Parameters
namestringproperty name
noParamOrderNoParamOrderfallbackfloatoptional fallback if conversion fails
requiredbool?throw error if the
namedoesn't exist, see Convention: Property Required Name Checks
Returns
- float
Value as
float
Get(string, NoParamOrder, bool?, string?)
Get a property.
object? Get(string name, NoParamOrder noParamOrder = default, bool? required = null, string? language = null)
Parameters
namestringthe property name like
Image- or path to sub-property likeAuthor.Name(new v15)noParamOrderNoParamOrderrequiredbool?throw error if
namedoesn't exist, see Convention: Property Required Name CheckslanguagestringOptional language like
de,de-chorde,ento determine which values to check. Will ignore languages not in the data model. On items that don't have ML data it will be ignored. new v17.10
Returns
- object
The result if found or null; or error if the object is in strict mode
Remarks
- parameter
languagesadded in 17.10
Get<TValue>(string, NoParamOrder, TValue?, bool?, string?)
Get a value using the name - and cast it to the expected strong type. For example to get an int even though it's stored as decimal.
Since the parameter fallback determines the type TValue you can just write this like
`something.Get("Title", fallback: "no title")
TValue? Get<TValue>(string name, NoParamOrder noParamOrder = default, TValue? fallback = default, bool? required = null, string? language = null)
Parameters
namestringthe property name like
Image- or path to sub-property likeAuthor.Name(new v15)noParamOrderNoParamOrderfallbackTValuethe fallback value to provide if not found
requiredbool?throw error if
namedoesn't exist, see Convention: Property Required Name CheckslanguagestringOptional language like
de,de-chorde,ento determine which values to check. Will ignore languages not in the data model. On items that don't have ML data it will be ignored. new v17.10
Returns
- TValue
The typed value, or the
defaultlikenullor0if casting isn't possible.
Type Parameters
TValueThe expected type, like
string,int, etc. Note that you don't need to specify it, if you specify thefallbackproperty.
Remarks
- Added in v15
- parameter
languagesadded in 17.10
Int(string, NoParamOrder, int, bool?)
Get a property and return the value as a int.
If conversion fails, will return default 0 or what is specified in the fallback.
int Int(string name, NoParamOrder noParamOrder = default, int fallback = 0, bool? required = null)
Parameters
namestringproperty name
noParamOrderNoParamOrderfallbackintoptional fallback if conversion fails
requiredbool?throw error if the
namedoesn't exist, see Convention: Property Required Name Checks
Returns
- int
Value as
int
IsEmpty(string, NoParamOrder, string?)
Check if this typed object has a property of this specified name, and has real data.
The opposite version of this is IsNotEmpty(...)
Important
This method is optimized for use in Razor-like scenarios. It's behavior is super-useful but maybe not always expected.
- If the value is a string, and is empty or only contains whitespace (even
) it is regarded as empty. - If the returned value is an empty list (e.g. a field containing relationships, without any items in it) it is regarded as empty.
If you need a different kind of check, just .Get(...) the value and perform the checks in your code.
bool IsEmpty(string name, NoParamOrder noParamOrder = default, string? language = null)
Parameters
namestringthe property name like
Image; some objects also support path to sub-property likeAuthor.NamenoParamOrderNoParamOrderlanguagestringOptional language like
de,de-chorde,ento determine which values to check. Will ignore languages not in the data model. On items that don't have ML data it will be ignored. new v17.10
Returns
- bool
trueif the property exists and has a real value. If it returned an empty list, it will also returnfalse
Remarks
- Added in 16.03
languageparameter added in 17.10
IsNotEmpty(string, NoParamOrder, string?)
Check if this typed object has a property of this specified name, and has real data.
The opposite version of this is IsEmpty(...)
Important
This method is optimized for use in Razor-like scenarios. It's behavior is super-useful but maybe not always expected.
- If the value is a string, and is empty or only contains whitespace (even
) it is regarded as empty. - If the returned value is an empty list (e.g. a field containing relationships, without any items in it) it is regarded as empty.
If you need a different kind of check, just .Get(...) the value and perform the checks in your code.
bool IsNotEmpty(string name, NoParamOrder noParamOrder = default, string? language = null)
Parameters
namestringthe property name like
Image; some objects also support path to sub-property likeAuthor.NamenoParamOrderNoParamOrderlanguagestringOptional language like
de,de-chorde,ento determine which values to check. Will ignore languages not in the data model. On items that don't have ML data it will be ignored. new v17.10
Returns
- bool
trueif the property exists and has a real value. If it returned an empty list, it will also returnfalse
Remarks
- Added in 16.03
languageparameter added in 17.10
Keys(NoParamOrder, IEnumerable<string>?)
Get all the keys available in this Model (all the parameters passed in). This is used to sometimes run early checks if all the expected parameters have been provided.
IEnumerable<string> Keys(NoParamOrder noParamOrder = default, IEnumerable<string>? only = null)
Parameters
noParamOrderNoParamOrderonlyIEnumerable<string>Only return the keys specified here, if found. Typical use:
only: new [] { "Key1", "Key2" }. Useful to check if all or any specific keys exist.
Returns
Remarks
Added in 16.03
Long(string, NoParamOrder, long, bool?)
Get a property and return the value as a long.
If conversion fails, will return default 0 or what is specified in the fallback.
long Long(string name, NoParamOrder noParamOrder = default, long fallback = 0, bool? required = null)
Parameters
namestringproperty name
noParamOrderNoParamOrderfallbacklongoptional fallback if conversion fails
requiredbool?throw error if the
namedoesn't exist, see Convention: Property Required Name Checks
Returns
- long
Value as
long
String(string, NoParamOrder, string?, bool?, object?)
Get a property and return the value as a string.
If conversion fails, will return default null or what is specified in the fallback.
string? String(string name, NoParamOrder noParamOrder = default, string? fallback = null, bool? required = null, object? scrubHtml = null)
Parameters
namestringproperty name
noParamOrderNoParamOrderfallbackstringoptional fallback if conversion fails
requiredbool?throw error if the
namedoesn't exist, see Convention: Property Required Name ChecksscrubHtmlobjectIf
true, will remove all HTML tags from the string. Ifpwill remove allptags, ifdiv,spanwill remove these tags. This is the same as usingKit.Scrub.All(...)or.Only(...). For more detailed scrubbing, use theKit.Scrub`
Returns
- string
Value as
string
Url(string, NoParamOrder, string?, bool?)
Get a url from a field. It will do sanitation / url-corrections for special characters etc.
On TypedItems it will also auto-convert values such as file:72 or page:14.
string? Url(string name, NoParamOrder noParamOrder = default, string? fallback = null, bool? required = null)
Parameters
namestringThe field name.
noParamOrderNoParamOrderfallbackstringoptional fallback if conversion fails
requiredbool?throw error if the
namedoesn't exist, see Convention: Property Required Name Checks
Returns
- string
A url converted if possible. If the field contains anything else such as
hellothen it will not be modified.