You can use tag extensions in MDriven Designer to record model-specific metadata on classes, associations, and other supported model elements, for developers and teams that need the model to carry decisions that do not belong in the ordinary UML properties.
Keep decisions in the model
A business model often needs information that is meaningful to your application but is not part of a standard class, attribute, or association definition. For example, you may need to mark a class as user friendly, identify an association end as expensive to load, or state the allowed business-delete behavior for an association.
Do not rely on code that recognizes individual classes or association names for these decisions. That spreads model knowledge into code and makes changes harder to maintain. Use tag extensions to define the metadata once and apply it where it belongs in the model.
A tag extension is metadata that you define. A tagged value is the value assigned to a particular model element through that extension.
Understand the parts
| Term | Meaning | Example |
|---|---|---|
| Extension | A named group of related tag types. | Kasper
|
| Tag type | One metadata field in an extension. A tag type applies to one kind of tag-extendable model item. | Kasper.BusinessDeleteValue
|
| Target | The kind of model element on which the tag type is available. | Association endpoint |
| Tagged value | The value assigned to a tag type on a specific model element. | OkEvenIfNotEmpty
|
| Value set | An optional list of allowed values for a tag type. | PSCheckMustBeEmpty, MustBeEmpty, OkEvenIfNotEmpty
|
For example, an extension named Kasper can contain these tag types:
Kasper.AutoSeekAndPickKasper.AllowUserNewKasper.UserFriendlyClassKasper.BusinessDeleteValue
If Kasper.BusinessDeleteValue targets association endpoints, it is available on an association endpoint but not on unrelated model elements. Define the target carefully: it prevents a tag from being assigned in places where it has no meaning.
Define and apply a tag extension
- Open the Tag Extensions definition dialog in MDriven Designer.
- Create an extension to group related metadata. Use a stable, descriptive name. For example, create
Kasperfor metadata owned by that model area. - Add a tag type. Give it a name that states the decision it records, such as
Kasper.BusinessDeleteValue. - Set the tag type's tag-extendable item type. For the delete-behavior example, select association endpoints.
- If the decision must be selected from known alternatives, define the tag type as an enumerable value set and add each permitted value. For example:
PSCheckMustBeEmptyMustBeEmptyOkEvenIfNotEmptyNoOneHasMadeADecisionForThisYet
- Select an applicable model element. Its property inspector now exposes the extension as a property.
- Set the tagged value. For example, set
Kasper.BusinessDeleteValuetoOkEvenIfNotEmptyon the relevant association endpoint.
Use an enumerable value set when consumers of the metadata must handle a fixed set of cases. A free-text value is less suitable for business rules because variations in spelling or wording become separate values.
Example: record an association delete decision
Assume an association endpoint named Reserverad needs an explicit business decision about deletion when related objects exist.
- Define
Kasper.BusinessDeleteValuefor association endpoints. - Add the allowed values listed above.
- Select the
Reserveradassociation endpoint. - Set
Kasper.BusinessDeleteValuetoOkEvenIfNotEmpty.
The choice is now visible in the model instead of being implicit in a special case in application code. Reviewers can see both the association and the decision together.
Use tagged values in generated code and metadata
Tagged values are translated to .NET code attributes in generated code. In the preceding example, the generated association-end property includes a UmlTaggedValue attribute:
[UmlTaggedValue("Kasper.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)));
}
}
Your code can read the generated .NET attributes using standard .NET techniques. It can also read the extended model metadata through the ECO meta-information represented in the generated model. The following pattern gets the tagged value for an association end and uses it in a rule:
foreach (IStructuralFeature sf in
TheObject.AsIObject().UmlClass.AllStructuralFeatures)
{
if (sf is IAssociationEnd)
{
IAssociationEnd ae = (IAssociationEnd)sf;
string tv = ae.TaggedValues.ValueForTag(
"Kasper.BusinessDeleteValue",
"NoOneHasMadeADecisionForThis");
if (tv == "PSCheckMustBeEmpty" ||
(tv == "NoOneHasMadeADecisionForThis" &&
!(ae.DeleteAction == DeleteAction.Prohibit)))
{
// Apply the rule for this association end.
}
}
}
The second argument to ValueForTag supplies a default when the tag has no assigned value. Choose a default that makes missing decisions visible. In this example, NoOneHasMadeADecisionForThis lets the code distinguish an undecided association from one that was explicitly configured.
Show tag values in diagrams
You can make important tagged values visible directly on diagrams. The Tag Extensions dialog includes a Symbols tab where you define a symbol and associate tag-type values with it.
- Open the Tag Extensions dialog and select the Symbols tab.
- Define a symbol with stand-alone XAML.
- Associate one or more tag-type values with that symbol.
- Set the corresponding tagged value on a model element.
- View the diagram. MDriven Designer displays the symbol when that value is set.
For example, this XAML defines a blue, rotated square:
<Rectangle xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Fill='Blue' Width='16' Height='16'>
<Rectangle.RenderTransform>
<RotateTransform Angle='45'/>
</Rectangle.RenderTransform>
</Rectangle>
Any stand-alone XAML is valid for a symbol. When you hover over a displayed symbol, the tooltip identifies the tag extension and its assigned value.
Use symbols for decisions that must be noticed while reading a diagram, such as a load warning or an unresolved business decision. Do not use them as a substitute for a clear tag name and well-defined allowed values.
Choose the right place for information
Use tag extensions for structured metadata that should be attached to a model element and consumed by code, model review, or diagram symbols.
| Need | Prefer | Example |
|---|---|---|
| Record a machine-readable decision on a class or association | Tag extension | An association endpoint has Kasper.BusinessDeleteValue set to MustBeEmpty.
|
| Explain a model element to a human reader | Model documentation with Documtr | Document why a delete rule exists and what business process it supports. |
| Implement behavior or enforce a rule | Model behavior and code, using the tagged value where appropriate | Code reads the delete-behavior tag and applies the selected rule. |
Keep the tag type focused on one decision. For example, use BusinessDeleteValue for delete behavior rather than a broad tag named Notes that mixes several unrelated decisions. Document the meaning of every allowed value so that modelers and code use the same interpretation.
