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.
To find a link in the store
- Use methods in the IElementDirectory interface to find and get links in the store.
To get roles that are assigned to a link
- 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;