Export (0) Print
Expand All

How to: Set or Get Domain Property Values

Domain classes and domain relationships in a domain model can have domain properties.

To set the value of a domain property

  1. In your code, create an instance of a store or use a reference to a model or element in the store to access its Store property.

  2. In a transaction, write the code to change a property value.

    The syntax for setting the value of a domain property is identical to the syntax for setting or retrieving a standard Common Language Runtime (CLR) property. However, Domain-Specific Language Tools logs the previous value and the new value of the property so that it can be restored to the previous value if the transaction is not committed or an undo action occurs.

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

  4. Dispose of the store.

To set the value of a property when you create an instance

  1. Create an instance of a store or use a reference to a model or element in the store to access its Store property.

  2. Create a domain class or domain relationship using the element's constructors.

    You can also specify the values for one or more of the properties for that object in the constructors.

The following example creates an instance of a store called Book and then sets its Name property. The example then creates another Book and sets its Name property in the constructor.

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

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

      using (Transaction txCreateBooks =  
        store.TransactionManager.BeginTransaction("Create 2 books"))
      {
        Book b1 = new Book(store);
        b1.Name = "The Autobiography of Benjamin Franklin";

        Book b2 = new Book(store, new PropertyAssignment(Book.NameDomainPropertyId, "Plato's Republic");

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

      store.Dispose();
    }
  }
}


Show:
© 2014 Microsoft