Use the Untyped Model Access API Using C#
Introduction
The Untyped Model Access API is designed for advanced users familiar with the internals of the Mendix platform. It allows access to the rich data of model elements.
- For model elements, units, and property names, see the Mendix Model SDK.
- Type names used by the Untyped Model Access API are available under the structureTypeName property of any model element.
Prerequisites
Before using the examples in this how-to:
- Create a microflow named
MyFirstLogicwith an action - Add an entity to the domain model
Getting Started
Begin by importing the Untyped Model Access API service:
class Sample(IUntypedModelAccessService untypedModelAccessService)
{
}Access the Model Root
Use the Untyped Model Access AP to gain access to the model Root:
class Sample2(IUntypedModelAccessService untypedModelAccessService, IModel currentApp)
{
public IModelRoot GetUntypedModelRoot() => untypedModelAccessService.GetUntypedModel(currentApp);
}Requesting Top-level Model Elements
To access the model elements, such as Apps and Modules, choose a starting point:
class Sample3(IUntypedModelAccessService untypedModelAccessService, IModel currentApp)
{
public IModelUnit GetProjectData() =>
untypedModelAccessService.GetUntypedModel(currentApp)
.GetUnitsOfType("Projects$Project")
.Single();
public IModelUnit GetMyModuleData() =>
untypedModelAccessService.GetUntypedModel(currentApp)
.GetUnitsOfType("Projects$Module")
.Single(unit => unit.Name == "MyFirstModule");
}Accessing Child Elements
You can access the child elements of a model element, such as microflow actions or entities of a domain model.
Use GetElements or GetElementsOfType to analyze the element properties and implement custom validation rules.
class Sample4(IUntypedModelAccessService untypedModelAccessService, IModel currentApp)
{
public IReadOnlyList<IModelElement> GetMicroflowActionActivities() =>
untypedModelAccessService.GetUntypedModel(currentApp)
.GetUnitsOfType("Projects$Module")
.Single(unit => unit.Name == "MyFirstModule")
.GetUnitsOfType("Microflows$Microflow")
.Single(unit => unit.Name == "MyFirstLogic")
.GetElementsOfType("Microflows$ActionActivity");
public IReadOnlyList<IModelElement> GetDomainModelEntities() =>
untypedModelAccessService.GetUntypedModel(currentApp)
.GetUnitsOfType("Projects$Module")
.Single(unit => unit.Name == "MyFirstModule")
.GetUnitsOfType("DomainModels$DomainModel").Single()
.GetElementsOfType("DomainModels$Entity");
}Getting Model Unit's Properties
To extract data out of a model element or unit, access its properties:
class Sample5(IUntypedModelAccessService untypedModelAccessService, IModel currentApp)
{
private IReadOnlyList<IModelProperty> GetProjectRuntimeSettingsProperties() =>
untypedModelAccessService.GetUntypedModel(currentApp)
.GetUnitsOfType("Settings$ProjectSettings")
.Single()
.GetElements()
.Single(element => element.Type == "Settings$RuntimeSettings")
.GetProperties();
public string? GetAfterStartupMicroflowId() =>
GetProjectRuntimeSettingsProperties()
.Single(property => property.Name == "AfterStartupMicroflowId")
.Value?
.ToString();
}