Share via


How to: Open a Model from File in Program Code

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can open DSL models in any application.

From a Visual Studio extension, you can use ModelBus for this purpose. ModelBus provides standard mechanism for referencing a model or elements in a model, and for finding the model if it has moved. For more information, see Integrating Models by using Visual Studio Modelbus.

Target Framework

Set the Target framework of your application project to .NET Framework 4.

To set the Target framework

  1. Open the Visual Studio project for the application in which you want to read a DSL model.

  2. In Solution Explorer, right-click the project and then click Properties.

  3. In the project properties window, on the Application tab, set the Target framework field to .NET Framework 4.

Note

You might need to do this even if you selected .NET Framework 4 in the project creation dialog box. The target framework should not be .NET Framework 4 Client Profile.

References

You have to add these references to your Visual Studio application project:

  • Microsoft.VisualStudio.Modeling.Sdk.11.0

    • If you do not see this under the .NET tab in the Add References dialog box, click the Browse tab and navigate to %Program Files%\Microsoft Visual Studio 2010 SDK\VisualStudioIntegration\Common\Assemblies\.
  • Your DSL assembly, which you will find under the bin folder your DSL project. Its name is typically of the form: YourCompany.YourProject.Dsl.dll.

Important Classes in the DSL

Before you can write the code that reads your DSL, you should know the names of some of the classes generated by your DSL. In your DSL solution, open the Dsl project and look in the GeneratedCode folder. Alternatively, double-click the DSL assembly in your project References, and open the DSL namespace in Object Browser.

These are the classes that you should identify:

  • YourDslRootClass - This is the name of the root class in your DslDefinition.dsl.

  • YourDslName SerializationHelper - This class is defined in SerializationHelper.cs in your DSL project.

  • YourDslName DomainModel - This class is defined in DomainModel.cs in your DSL project.

Reading from a File

The following example is designed to read a DSL in which the important classes are as follows:

  • FamilyTreeModel

  • FamilyTreeSerializationHelper

  • FamilyTreeDomainModel

    The other domain class in this DSL is Person.

using System;
using Microsoft.VisualStudio.Modeling;
using Company.FamilyTree; // Your DSL namespace

namespace StandaloneReadDslConsole
{ class Program
  { static void Main(string[] args)
    {
      // The path of a DSL model file:
      string dslModel = @"C:\FamilyTrees\Tudor.ftree";
      // Set up the Store to read your type of model:
      Store store = new Store(
        typeof(Company.FamilyTree.FamilyTreeDomainModel));
      // The Model type generated by the DSL:
      FamilyTreeModel familyTree;
      // All Store changes must be in a Transaction:
      using (Transaction t =
        store.TransactionManager.BeginTransaction("Load model"))
      {
        familyTree =
           FamilyTreeSerializationHelper.Instance.
              LoadModel(store, dslModel, null, null, null);
        t.Commit(); // Don't forget this!
      }
      // Now we can read the model:
      foreach (Person p in familyTree.People)
      {
        Console.WriteLine(p.Name);
        foreach (Person child in p.Children)
        {
          Console.WriteLine("    " + child.Name);
        }
} } } }

Saving to a File

The following addition to the previous code makes a change to the model and then saves it to a file.

using (Transaction t =
  store.TransactionManager.BeginTransaction("update model"))
{
  // Create a new model element:
  Person p = new Person(store);
  // Set its embedding relationship:
  p.FamilyTreeModel = familyTree;
  // - same as: familyTree.People.Add(p);
  // Set its properties:
  p.Name = "Edward VI";
  t.Commit(); // Don't forget this!
}
// Save the model:
try
{
  SerializationResult result = new SerializationResult();
  FamilyTreeSerializationHelper.Instance
    .SaveModel(result, familyTree, @"C:\FamilyTrees\Tudor-upd.ftree");
  // Report any error:
  if (result.Failed)
  {
    foreach (SerializationMessage message in result)
    {
      Console.WriteLine(message);
    }
  }
}
catch (System.IO.IOException ex)
{ ... }