How to: Access a Link and Roles at Both Ends

A link represents a domain relationship between two domain model elements in a domain model. Each relationship has two roles, source and target, which represent the two sides of the relationship. You can get information about a link in two ways: from the graphical representation of the domain model or from the store that is assigned to the domain model. The following procedure shows you how to access a link from the store.

  • Use methods in the T:Microsoft.VisualStudio.Modeling.DomainRoleInfo class to find source and target roles that are assigned to a link.

Example

In this example, you search in the store for links and roles that are assigned to them. The code does not have to run in a transaction, unless the code changes the contents of the store.

// Find all links in the Library store.
ReadOnlyCollection<ModelElement> foundlinks = store.ElementDirectory.FindElementLink(Library.DomainClassId);

// Get all links in the Library store.
ReadOnlyCollection<ModelElement> getlinks = store.ElementDirectory.GetAllElementLinks(Library.DomainClassId);

// Count the number of links that were found.
int howmany = foundlinks.Count;
string msg = string.Format("There are {0} Library links", howmany);
Windows.Forms.MessageBox.Show(msg); 

// Get a specific link in the Library store (need link's ID).
ModelElement link = store.ElementDirectory.GetElementLink(id);

// Get the source and target roles for a link (need link).
ModelElement roles = store.ElementDirectory.GetRolePlayer(link);

//This returns a link, which you can cast to a Library.
Library somelibrary = (Library)link;

See Also

Concepts

How to: Find Objects in the Store

Domain-Specific Language Tools Glossary