How to: Create Elements in Code

You can create model elements in code. This action has the same effect of adding elements to the store as if a language user had dragged an element from the Toolbox to the design surface.

All changes to the store must be done in the context of a transaction. Any changes that you made to the store are saved only if that transaction is committed.

To add an element to a model

  1. In your code, create an instance of a store, and load a domain model into it.

  2. Create a transaction.

    If you are already in a transaction, you can create a transaction that is nested in that transaction.

  3. Write the code to create one or more elements and links between them.

    Each element and link that you add must have a type defined in the domain model. In other words, you cannot add a type that is not already defined as a domain class.

  4. Commit the transaction to save changes to the store.

  5. Dispose of the store.

Example

The following example creates an instance of the store, adds elements to it, and then adds links between one element and the other elements.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Modeling;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      Store store = new Store();
      store.LoadDomainModels(typeof(LibraryModel));

      using (Transaction txCreateElem =  
        store.TransactionManager.BeginTransaction("Create elements"))

      // If you have a reference to a model or element, you can 
      // get a reference to the store and to its transaction manager
      // (an element has a Store property), rather than create a new
      // instance of a store.

      {
        Library a = new Library(store);
        // Create additional elements in the 
        // same partition of the store.
        Book b1 = new Book(a.Partition);
        Book b2 = new Book(a.Partition);
        Book b3 = new Book(a.Partition);
        Book b4 = new Book(a.Partition);

        // Commit the transaction, and add the elements to the model
        txCreateElem.Commit();
      }

      // To view all elements in the store, examine
      // the collection in the ElementDirectory of the store.
      ICollection<ModelElement> elems = store.ElementDirectory.AllElements;

      store.Dispose();
    }
  }
}

You can use code to add or change the store anywhere that you can put custom code. These locations include your custom rules, overridden events, and validation code.

In this example, changes to the model are discarded after the Main method. To save the changes, you must commit them for the model before the program exits.

You might deserialize the model into the store at the start of your custom code and then serialize the model with the changes that you made before the Main method exits.

See Also

Tasks

How to: Create Links in Code

How to: Delete Elements from the Model

Reference

Store

LoadDomainModels

TransactionManager

ElementDirectory

Other Resources

Domain-Specific Language Tools Glossary