Joeul asks an interesting question in his forum post. reference relationships' names are not normally unique across the whole model, but only to the 2 elements they link. How to change this behavior?
Here is how to do to get unique name for reference relationships across the model. This is also the opportunity of explaining how to use the Name providers.
Let’s suppose that I have a simple DSL named “GloballyUniqueRelationshipName” which I unfolded from a Minimal language:
using System;using System.Collections.Generic;using System.Linq;using Microsoft.VisualStudio.Modeling; namespace Company.GloballyUniqueRelationshipName{ /// <summary> /// Custom name provider that ensures that every link instance has a unique name /// </summary> internal class MyNameProvider : ElementNameProvider { public override void SetUniqueName(ElementLink link, DomainRoleInfo indexingDomainRole, string baseName) { if (link == null) { throw new ArgumentNullException("link"); } baseName = "MyBaseName"; IDictionary<string, ModelElement> allSiblings = link.Store.ElementDirectory.FindElements(link.GetDomainClass()) .Where(l => l != link) .ToDictionary(l => l.GetDomainClass().NameDomainProperty.GetValue(l).ToString()); if (this.DomainProperty.PropertyType == typeof(string)) { this.SetUniqueNameCore(link, baseName, allSiblings); } else { this.CustomSetUniqueNameCore(link, baseName, link.Store.ElementDirectory.FindElements(link.GetDomainClass())); } } }}
Note that this sample can be improved to apply this processing to links of a given class only (here we apply it to every links)