Entity definitions client
The Entity definitions client can be used to perform CRUD operations on entity definitions.
Use extreme caution. Adding, removing, or deleting entity definitions can have serious consequences.
Get entity definitions
Entity definitions can be retrieved by name or id:
IEntityDefinition assetDefinition = await MClient.EntityDefinitions.GetAsync("M.Asset");
IEntityDefinition assetDefinition = await MClient.EntityDefinitions.GetAsync(<ASSET_DEFINITION_ID>);
If the entity definition does not exist, the method returns null.
There are many more overloads for retrieving entity definitions. It's also possible to resolve names to IDs and vice versa. See the API docs for a complete overview.
Create entity definitions
The SDK lets you create entity definition objects directly, without a factory. A very simple entity definition can be created as follows:
IEntityDefinition definition = new EntityDefinition
{
Name = "M.Demo.Definition",
DisplayTemplate = "{M.Demo.Definition.Name}"
};
var defaultGroup = new MemberGroup { Name = "Default" };
defaultGroup.MemberDefinitions.Add(new StringPropertyDefinition
{
Name = "M.Demo.Definition.Name",
IsUnique = true
});
definition.MemberGroups.Add(<DEFAULT_GROUP>);
long id = await MClient.EntityDefinitions.SaveAsync(<DEFINITION>);
The returned id is the ID of the newly created entity definition.
The EntityDefinition can be imported from Stylelabs.M.Sdk.Models.Base, and the property member definitions from Stylelabs.M.Sdk.Models.Base.PropertyDefinitions.
Update entity definitions
Use the save method on the entity definitions client after making a change.
long id = await MClient.EntityDefinitions.SaveAsync(<DEFINITION>);
This returns the ID of the updated entity definition.
Delete entity definitions
Entity definitions can be deleted by name or by id:
await MClient.EntityDefinitions.DeleteAsync(<DEFINITION_ID>);
await MClient.EntityDefinitions.DeleteAsync("M.Demo.Definition");