You can use tag extensions in MDriven Designer to add project-specific metadata to model elements, such as classes, attributes, and association ends, and make that metadata available in generated code and diagrams.
What tag extensions are
A tag extension defines extra properties that you can attach to selected kinds of model elements. Use one when the information belongs to the model but is not part of the standard UML property set.
For example, you may need to:
- mark a class as
UserFriendlyso application logic can treat it differently; - mark an association end as
HeavyToLoadto warn developers about navigation that may load many objects; - record a deletion decision such as
MustBeEmptyon an association end; - flag an attribute as a special case that must be reviewed later.
A tag extension is model metadata. It avoids scattering checks for particular classes or associations through application code, and keeps the decision visible to people working on the model.
Key terms
| Term | Meaning | Example |
|---|---|---|
| Extension | A named group of related tag types. | An extension named Project groups the project's custom tags.
|
| Tag type | A defined metadata property within an extension. A tag type applies to one kind of tag-extendable model item. | Project.BusinessDeleteValue applies to association ends.
|
| Tag value | The value assigned to a tag type on a particular model element. Tag values are stored as tagged values. | The Reserverad association end has Project.BusinessDeleteValue = MustBeEmpty.
|
| Value set | A fixed, enumerable list of values for a tag type. MDriven Designer presents it as a picker when you edit the property. | MustBeEmpty, OkEvenIfNotEmpty, and NoDecisionYet.
|
| Symbol | A XAML-defined visual marker that MDriven Designer can show in a diagram for a tag value. | A red warning symbol is shown when an attribute has a non-empty special-case tag. |
Define and use a tag extension
Define the extension before assigning its values to model elements.
- In MDriven Designer, open Functions and select Tag extensions.
- Create an extension and give it a grouping name. For example, create
Project. - Add a tag type to the extension.
- Select the target type for the tag type. The target determines where the new property is available; for example, select attributes when you need a property on every attribute, or association ends when you need a property on an association end.
- Name the tag type. The resulting tag name is qualified by its extension, such as
Project.RememberSpecialCaseorProject.BusinessDeleteValue. - If the tag must use controlled values, define its value set. For example, define
MustBeEmpty,OkEvenIfNotEmpty, andNoDecisionYetfor a deletion-policy tag. - Select a model element of the target type. The extension appears in the property inspector as an ordinary property group.
- Set the tag value on that element. For a value set, select the appropriate value from the picker.
After you define an extension, its name appears on each applicable model element. A tag type targeted at attributes therefore becomes available when you select an attribute; a tag type targeted at association ends becomes available when you select an association end.
Example: document an association-end deletion rule
Suppose an association end must be empty before business logic allows deletion.
- Create an extension named
Project. - Add the
BusinessDeleteValuetag type and target association ends. - Define the allowed values
MustBeEmpty,OkEvenIfNotEmpty, andNoDecisionYet. - Select the relevant association end and set
Project.BusinessDeleteValuetoMustBeEmpty.
The decision is now attached to the association end rather than being an undocumented convention in code.
Read tag values in generated code
MDriven stores extension values as tagged values and translates them into .NET code attributes in generated code. A generated member can therefore include an attribute such as:
[UmlTaggedValue("Project.BusinessDeleteValue", "OkEvenIfNotEmpty")]
public Anvandare Reserverad {
get {
return ((Anvandare)
this.eco_Content.get_MemberByIndex(Eco_LoopbackIndices.Reserverad));
}
set {
this.eco_Content.set_MemberByIndex(
Eco_LoopbackIndices.Reserverad, ((object)(value)));
}
}
You can read this information through standard .NET attribute handling or through the model's extended meta-information. The following example reads a value from each association end and supplies a fallback when the tag is not set:
foreach (IStructuralFeature sf in TheObject.AsIObject().UmlClass.AllStructuralFeatures)
{
if (sf is IAssociationEnd)
{
IAssociationEnd ae = (IAssociationEnd)sf;
string tv = ae.TaggedValues.ValueForTag(
"Project.BusinessDeleteValue", "NoDecisionYet");
if (tv == "MustBeEmpty" ||
(tv == "NoDecisionYet" && ae.DeleteAction != DeleteAction.Prohibit))
{
// Apply the project's deletion rule.
}
}
}
Keep the extension and tag-type names stable when code depends on them. The string passed to ValueForTag must match the qualified tag name that you defined.
For general tagged-value concepts and maintenance, see Documentation:TaggedValues and HowTos:Adding or removing tagged values in your model - using the model debugger.
Show tag values as diagram symbols
You can make model metadata visible in diagrams by associating tag values with symbols. Symbols help reviewers identify important model decisions without opening each element's property inspector.
- Open Functions > Tag extensions.
- Select the extension and open its Symbols tab.
- Define a symbol with stand-alone XAML. For example, this XAML defines a blue diamond-shaped marker:
<Rectangle xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Fill='Blue' Width='16' Height='16'>
<Rectangle.RenderTransform>
<RotateTransform Angle='45'/>
</Rectangle.RenderTransform>
</Rectangle>
- Associate the symbol with a tag type or a specific tag-type value. You can configure a symbol to appear when a tag is non-empty, or use different symbols for different values in a value set.
- Set the tag value on a matching model element and view the element in a diagram.
When you hover over a displayed symbol, MDriven Designer shows a tooltip that identifies the tag extension and its value. If multiple applicable tags have symbols on the same element, the diagram can show multiple symbols.
For example, an attribute tagged Project.RememberSpecialCase can display a red warning marker whenever the property has a value. If the tag uses a value set such as Important and NotImportant, you can assign a different symbol to each value.
Any stand-alone XAML is valid for a symbol. You can obtain XAML-form icons from sources such as Material Design Icons, then use the icon markup in the Symbols definition.
Design guidance
- Use tag extensions for metadata that applies across the model and needs to be discoverable by both people and code.
- Target each tag type narrowly. A deletion-policy tag belongs on association ends; a classification of fields belongs on attributes.
- Use a value set when the project has a known vocabulary. This prevents spelling variations such as
MustBeEmptyandMust Be Emptyfrom becoming different values. - Use symbols for information that must be recognized during diagram review, such as ordered associations, expensive navigation paths, deprecated elements, or items that require later removal.
- Do not use a tag extension as a substitute for a regular modeled attribute when the value is business data that users must enter and persist for each runtime object.
