Class CodeTyped
Base class for v16 Typed CSharp files. Use it to create custom CS code in your App.
It provides the ServiceKit16 on property Kit
which contains all the popular services to create amazing stuff.
[PublicApi]
public abstract class CodeTyped : CustomCodeBase, ICanDebug, IHasLog
- Inheritance
-
CodeTyped
- Implements
Remarks
Important: This is very different from Razor12 or Razor14, as it doesn't rely on dynamic
code.
Be aware of this since the APIs are very different - see Typed Code.
Constructors
CodeTyped()
Main constructor. Doesn't have parameters so it can easily be inherited.
protected CodeTyped()
Properties
AllResources
Stack of all Resources in the System, merging Resources of View, App, Site, Global etc. Will retrieve values by priority, with View-Resources being top priority and Preset-Resources being the lowest.
Tip
If you know that Resources come from the App, you should prefer App.Resources
instead.
That is faster and helps people reading your code figure out where to change a value.
public ITypedStack AllResources { get; }
Property Value
AllSettings
Stack of all Settings in the System, merging Settings of View, App, Site, Global etc. Will retrieve values by priority, with View-Settings being top priority and Preset-Settings being the lowest.
Tip
If you know that Settings come from the App, you should prefer App.Settings
instead.
That is faster and helps people reading your code figure out where to change a value.
public ITypedStack AllSettings { get; }
Property Value
App
The current App object (with strictly typed Settings/Resources).
Use it to access App properties such as Path
or any data in the App.
public IAppTyped App { get; }
Property Value
Kit
public ServiceKit16 Kit { get; }
Property Value
Link
Link helper object to create the correct links
public ILinkService Link { get; }
Property Value
- ILinkService
A ILinkService object.
Log
The logger for the current Razor / WebApi which allows you to add logs to Insights.
public ICodeLog Log { get; }
Property Value
MyContext
This Context tells you about the environment, such as
- the current User
- the Page
- the View
- the Site
It's supposed to replace direct access to Dnn or Oqtane object in Razor and WebAPI code, allowing hybrid code that works everywhere.
public ICmsContext MyContext { get; }
Property Value
Remarks
New in v11.11
MyData
All the data which the current Template received, based on the View configuration. There are a few common scenarios:
- If it's a simple view, then this will just contain streams with the main Item(s) and Header
- If the view expects no data, it will just contain a
Default
stream containing no items - If the view has a Query behind it, then MyData will have all the streams provided by the Query
public IBlockInstance MyData { get; }
Property Value
MyHeader
The Header-Item belonging to this Template/Module. This data is edited by the user directly on this specific module. In some cases it can also be a pre-set item configured in the View to be used if the user has not added any data himself.
public ITypedItem MyHeader { get; }
Property Value
MyItem
The main Item belonging to this Template/Module. This data is edited by the user directly on this specific module. In some cases it can also be a pre-set item configured in the View to be used if the user has not added any data himself.
If this view can have a list of items (more than one) then this contains the first item. To get all the items, see MyItems
public ITypedItem MyItem { get; }
Property Value
MyItems
List of all Items belonging to this Template/Module. This data is edited by the user directly on this specific module. In some cases it can also be a pre-set item configured in the View to be used if the user has not added any data himself.
If this view is configured to only have one item, then this list will only contain one item. Otherwise it will have as many items as the editor added.
public IEnumerable<ITypedItem> MyItems { get; }
Property Value
MyModel
Data passed to this Razor template by a caller.
This is typical for Razor components which are re-used, and called from other Razor templates using @Html.Partial("filename.cshtml", new { thing = 7 })
.
public ITypedModel MyModel { get; }
Property Value
MyPage
Information about the current Page (called Tab in DNN). It's especially useful to get current URL Parameters.
public ICmsPage MyPage { get; }
Property Value
MyUser
Information about the current user. It's especially useful to see if the user has any kind of Admin privileges.
public ICmsUser MyUser { get; }
Property Value
MyView
View-information such as the view Name, Identity or Edition.
public ICmsView MyView { get; }
Property Value
Remarks
New in v12.02
UniqueKey
A unique, random key for the current module. It's recommended for giving DOM elements a unique id for scripts to then access them.
It's generated for every content-block, and more reliable than Module.Id
since that sometimes results in duplicate keys, if the many blocks are used inside each other.
It's generated using a GUID and converted/shortened. In the current version it's 8 characters long, so it has 10^14 combinations, making collisions extremely unlikely. (currently 8 characters)
Tip
To get a unique key which is based on additional objects such as Entities, use the UniqueKeyWith(params object[]) method.
public string UniqueKey { get; }
Property Value
Remarks
If you get a fresh IKeyService it will also create a new UniqueKey.
So your code should usually use the built-in property UniqueKey
which comes from the shared ServiceKit Key.
Methods
AsEntity(ICanBeEntity)
Unwraps a dynamic entity or ITypedItem back into the underlying IEntity
public IEntity AsEntity(ICanBeEntity thing)
Parameters
thing
ICanBeEntity
Returns
- IEntity
A normal IEntity
AsItem(object, NoParamOrder, bool?, bool?)
Convert something to a ITypedItem. This works for all kinds of IEntitys, IDynamicEntitys as well as Lists/IEnumerables of those.
Will always return a single item. If a list is provided, it will return the first item in the list. If null was provided, it will return null.
public ITypedItem AsItem(object data, NoParamOrder noParamOrder = default, bool? propsRequired = null, bool? mock = null)
Parameters
data
objectAn original object which can be converted to a TypedItem, such as a IEntity .
noParamOrder
NoParamOrderpropsRequired
bool?make the resulting object strict, default
true
mock
bool?Specify that the data is fake/mock data, which should pretend to be an Item. Default is
false
Returns
Remarks
New in v16.02
AsItems(object, NoParamOrder, bool?)
Convert an object containing a list of Entities or similar to a list of ITypedItems.
public IEnumerable<ITypedItem> AsItems(object list, NoParamOrder noParamOrder = default, bool? propsRequired = null)
Parameters
list
objectThe original list which is usually a list of IEntity objects.
noParamOrder
NoParamOrderpropsRequired
bool?make the resulting object strict, default
true
Returns
Remarks
New in v16.01
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
.
public IEnumerable<T> AsList<T>(object source, NoParamOrder protector = default, bool nullIfNull = false) where T : class, ITypedItemWrapper16, ITypedItem, new()
Parameters
source
objectthe source object - a List/Enumerable of
IEntity
orITypedItem
protector
NoParamOrdernullIfNull
boolif
true
will return null whensource
isnull
- otherwise a wrapper item with empty-contents
Returns
- IEnumerable<T>
Type Parameters
T
the target type
Remarks
Release in v17.05
AsStack(params object[])
Create a typed object which will provide all the properties of the things wrapped inside it. The priority is first-object first, so if multiple items have the property, the first in the list will be returned.
public ITypedStack AsStack(params object[] items)
Parameters
items
object[]objects to stack together
Returns
AsStack<T>(params object[])
Create a custom-typed object which will provide all the properties of the things wrapped inside it. The priority is first-object first, so if multiple items have the property, the first in the list will be returned.
public T AsStack<T>(params object[] items) where T : class, ITypedItemWrapper16, ITypedItem, new()
Parameters
items
object[]objects to stack together
Returns
- T
Item of the custom type
Type Parameters
T
Remarks
New in 17.07
AsTyped(object, NoParamOrder, bool?)
Creates a typed object to read the original passed into this function. This is usually used to process objects which the compiler can't know, such as anonymous objects returned from helper code etc.
If you have an array of such objects, use AsTypedList(object, NoParamOrder, bool?).
public ITyped AsTyped(object original, NoParamOrder noParamOrder = default, bool? propsRequired = null)
Parameters
original
objectnoParamOrder
NoParamOrderpropsRequired
bool?make the resulting object strict, default
true
Returns
AsTypedList(object, NoParamOrder, bool?)
Create a list
public IEnumerable<ITyped> AsTypedList(object list, NoParamOrder noParamOrder = default, bool? propsRequired = null)
Parameters
list
objectList/Enumerable object containing a bunch of items to make typed
noParamOrder
NoParamOrderpropsRequired
bool?make the resulting object strict, default
true
Returns
As<T>(object, NoParamOrder, bool)
Convert an Entity or TypedItem into a strongly typed object.
Typically, the type will be from your AppCode.Data
.
public T As<T>(object source, NoParamOrder protector = default, bool mock = false) where T : class, ITypedItemWrapper16, ITypedItem, new()
Parameters
source
objectthe source object - an
IEntity
orITypedItem
protector
NoParamOrdermock
boolif
true
will return a fake whensource
isnull
- otherwise a wrapper item with empty-contents
Returns
- T
Type Parameters
T
the target type
Remarks
Released v17.05
GetCode(string, NoParamOrder, string)
Create an instance of a class in a .cs
code file.
Note that the class name in the file must match the file name, so MyHelpers.cs
must have a MyHelpers
class.
public dynamic GetCode(string path, NoParamOrder noParamOrder = default, string className = null)
Parameters
path
stringThe path, like
Helper.cs
,./helper.cs
,../../Helper.cs
or/SomeFolderInApp/Helper.cs
(new 16.05)noParamOrder
NoParamOrderclassName
stringOptional class name, if it doesn't match the file name (new 16.03)
Returns
- dynamic
,
Remarks
- Created in 16.02
className
added in 16.03- Ability to give a path beginning with
/
as app-root in 16.05
In older code there was a similar CreateInstance
method
GetService<TService>()
Get a service from the Dependency Injection. The service can come from 2sxc, EAV or the underlying platform (Dnn, Oqtane).
public TService GetService<TService>() where TService : class
Returns
- TService
An object of the type or interface requested, or null if not found in the DI.
Type Parameters
TService
Interface (preferred) or Class which is needed