Query and access stereotype properties on metadata elements
Stereotypes are metadata annotations applied to design elements in Intent Architect. The stereotypes API provides extension methods for querying stereotypes and accessing their property values.
// Check if element has specific stereotypeif (model.HasStereotype("REST")){ // Element is marked with REST stereotype}// Check using definition IDif (model.HasStereotype("12345678-1234-1234-1234-123456789abc")){ // Stereotype present}
// Get all stereotypes with same namevar validationStereotypes = model.GetStereotypes("Validation");foreach (var stereotype in validationStereotypes){ var rule = stereotype.GetProperty<string>("Rule"); var message = stereotype.GetProperty<string>("ErrorMessage"); // Apply validation rule}
try{ var value = model.GetStereotypeProperty<string>("Config", "Value");}catch (Exception e){ // Thrown if: // - Multiple stereotypes with same name exist // - Type conversion fails // - Element access throws exception}// Safe alternative with default valuevar safeValue = model.GetStereotypeProperty<string>("Config", "Value", "default");
using System;using System.Collections.Generic;using System.Linq;using Intent.Metadata.Models;namespace Intent.Modules.Common{ /// <summary> /// Extension methods for <see cref="IStereotype"/> and <see cref="IHasStereotypes"/>. /// </summary> public static class StereotypeExtensions { /// <summary> /// Retrieve the value of the property with the provided <paramref name="propertyName"/> /// on the provided <paramref name="stereotypeName"/> on the provided <paramref name="model"/>. /// </summary> public static T GetStereotypeProperty<T>( this IHasStereotypes model, string stereotypeName, string propertyName, T defaultIfNotFound = default) { try { return model.GetStereotype(stereotypeName) .GetProperty(propertyName, defaultIfNotFound); } catch (Exception e) { throw new Exception( $"Failed to get stereotype property for element [{model}]", e); } } /// <summary> /// Lookup only one stereotype with a given name. If more than one is found /// with the same name an exception is thrown. /// </summary> public static IStereotype GetStereotype( this IHasStereotypes model, string stereotypeNameOrId) { var stereotypes = model.Stereotypes .Where(x => x.Name == stereotypeNameOrId || x.DefinitionId == stereotypeNameOrId) .ToArray(); if (stereotypes.Length > 1) { throw new Exception( model is IMetadataModel metadataModel ? $"More than one stereotype found with the name " + $"'{stereotypeNameOrId}' on element with ID {metadataModel.Id}" : $"More than one stereotype found with the name " + $"'{stereotypeNameOrId}'"); } return stereotypes.SingleOrDefault(); } /// <summary> /// Look up multiple stereotypes by the same name or definitionId. /// </summary> public static IReadOnlyCollection<IStereotype> GetStereotypes( this IHasStereotypes model, string stereotypeNameOrId) { return model.Stereotypes .Where(p => p.Name == stereotypeNameOrId || p.DefinitionId == stereotypeNameOrId) .ToArray(); } /// <summary> /// Used to query whether or not a stereotype with a particular name or /// definition Id is present. /// </summary> public static bool HasStereotype( this IHasStereotypes model, string stereotypeNameOrId) { return model.Stereotypes.Any(x => x.Name == stereotypeNameOrId || x.DefinitionId == stereotypeNameOrId); } }}