Share via


How to: Validate C and C++ Code Against Layer Diagrams

This Visual Studio 2010 Feature Pack lets you enforce architectural constraints against C and C++ code in Visual Studio 2010 Ultimate as the code evolves. To perform this task, run layer validation regularly to compare dependencies in code against dependencies on a layer diagram. A layer diagram describes the architecture of the system and organizes namespaces, projects, and other artifacts into logical, abstract groups, or layers. These layers describe the major components of the system or the tasks that those artifacts perform. To create a layer diagram for C or C++ code, follow the same steps for those for .NET code. For more information, see How to: Create Layer Diagrams from Artifacts and Visual Studio Feature Packs.

Note

You must install a Visual Studio 2010 Feature Pack to use this feature.

For binary files, layer validation is performed at the binary level only. Even though you can link other types of artifacts to a layer diagram, layer validation analyzes only dependencies at the binary level.

Layer validation can also help you with the following tasks:

  • See the differences between the existing code and its intended design.

  • Find code that is not organized as intended or that has unwanted dependencies.

  • Find dependencies that might be affected by proposed changes.

    For example, you can modify the layer diagram to show potential architecture changes and then validate the code to see the affected dependencies.

  • Refactor or migrate code to a different design.

    Find code or dependencies that still require work when you move the code to a different architecture.

This topic describes requirements that must be met before you run layer validation on C or C++ code.

Requirements

  • Make sure that Visual C# is installed with Visual Studio.

  • Create a modeling project with a layer diagram

  • To validate binary file dependencies, add the binary files to the modeling project

  • To validate C and C++ projects on Team Foundation Build, see Team Foundation Build Requirements.

After meeting these requirements, you can follow the same steps as those for .NET code. For these steps, see How to: Validate .NET Code Against Layer Diagrams. For issues related to modeling projects and layer diagrams created from C or C++ code, see Troubleshooting.

Create a Modeling Project with a Layer Diagram

The Visual Studio solution must include a modeling project that has a layer diagram.

To create a modeling project

  1. In Solution Explorer, right-click the top-level solution node, point to Add, and then click New Project.

  2. In the Add New Project dialog box, under Installed Templates, click Modeling Projects, and then click Modeling Project.

  3. Rename the project, and then click OK.

    Visual Studio adds a new modeling project to the solution.

  4. Make sure to save the modeling project.

To create a layer diagram

  1. On the Architecture menu, click New Diagram, and then in the Add New Diagram dialog box, click Layer Diagram.

  2. Rename the diagram, select the modeling project, and then click OK.

    Visual Studio adds a blank layer diagram to the modeling project and then displays the diagram.

  3. From Solution Explorer, drag at least one C or C++ project to the layer diagram surface.

    - or -

    From Architecture Explorer, drag at least one binary to the layer diagram surface.

    Performing either of these steps will also add the following items to the modeling project automatically:

    • Project references to the modeling project.

    • An <Import> element to the modeling project (.modelproj) file that imports a custom .targets file and enables layer validation for C or C++ code.

  4. Define other layers, link artifacts to them, and describe the intended dependencies between those artifacts.

    For more information, see How to: Create Layer Diagrams from Artifacts and Layer Diagrams: Guidelines.

  5. Make sure to save the modeling project.

    Important

    If you save only the layer diagram, but not the modeling project, and then close the solution, you must add the C or C++ project references and import the custom .targets file manually for the modeling project. For more information, see Troubleshooting.

Important

When you create a layer diagram from C or C++ code in Visual Studio 2010 Ultimate on Windows XP or Windows 2003, an absolute path is used to create the entry in the modeling project (.modelproj) file to import the custom Microsoft.VisualStudio.Progression.NativeProvider.targets file. This might cause problems if you try to open the solution on Windows 7, Windows Vista, or Windows Server 2008. If you are running Visual Studio 2010 Ultimate on Windows XP or Windows 2003, you cannot open layer diagrams that were created in Visual Studio 2010 Ultimate on Windows 7, Windows Vista, or Windows Server 2008. To resolve this issue, see Troubleshooting.

Add the Binary Files to the Modeling Project

If you want to validate binary file dependencies, you must manually add the binary files that you want to validate to the modeling project. They are not added automatically when you drag them to the layer diagram.

To add binary files to the modeling project

  1. In Solution Explorer, right-click the modeling project, and then click Add Existing Item.

  2. In the Add Existing Item dialog box, browse to the binary files, select them, and then click OK.

    The binary files appear in the modeling project.

  3. In Solution Explorer, click a binary file that you added, and then press F4 to open the Properties window.

  4. On each binary file, set the Build Action property to Validate.

Team Foundation Build Requirements

To validate C and C++ projects on Team Foundation Build server, you must follow these steps:

  1. To compile the projects, make sure that the following are installed on the server:

    Tip

    If Visual Studio 2010 Ultimate is installed on the server, you can install the feature pack on the server instead of performing steps 2 and 3. Otherwise, if the …\<InsertVisualStudioFeaturePackName>\<InsertVisualStudioFeaturePackVersionNumber> folder does not exist on the server, you might have to create it. Unless you installed the feature pack in a different location, %LocalAppData% is typically DriveName:\Users\UserName\AppData\Local. On Windows XP or Windows 2003, use %AppData% instead of %LocalAppData%.

  2. If you have Visual Studio 2010 Visualization and Modeling Feature Pack installed:

    Copy the Microsoft.VisualStudio.Progression.NativeProvider.targets file from the following feature pack installation location to the same location on the server:

    %LocalAppData%\Microsoft\VisualStudio\10.0\Extensions\Microsoft\<InsertVisualStudioFeaturePackName>\1.0

    If you have Visual Studio 2010 Feature Pack 2 installed:

    Copy the Microsoft.VisualStudio.Progression.NativeProvider.targets file from the following feature pack installation location:

    …\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\<InsertVisualStudioFeaturePackName>\<InsertVisualStudioFeaturePackVersionNumber>

    to the following location on the server:

    …\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Modeling\<InsertVisualStudioFeaturePackName>\1.0

  3. Copy the Microsoft.VisualStudio.Progression.NativeProvider.dll file from one of the following feature pack installation locations, depending on the version of the feature pack that you have installed:

    • Visual Studio 2010 Visualization and Modeling Feature Pack: %LocalAppData%\Microsoft\VisualStudio\10.0\Extensions\Microsoft\<InsertVisualStudioFeaturePackName>\1.0

    • Visual Studio 2010 Feature Pack 2: …\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\<InsertVisualStudioFeaturePackName>\<InsertVisualStudioFeaturePackVersionNumber>

    to the following server location:

    …\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\Providers

Troubleshooting

If you save only the layer diagram, but not the modeling project, and then close the solution, you must add the C or C++ project references and import the custom .targets file manually for the modeling project by following these steps:

To

Follow these steps

Add the C or C++ project references

  1. In Solution Explorer, right-click the modeling project that contains the layer diagram, and then click Add Reference.

    - or -

    In the modeling project, right-click the Layer References folder, and then click Add Reference.

  2. In the Add Reference dialog box, select the projects that you want to validate, and then click OK.

    The project references appear in the Layer References folder.

Import the custom .targets file

  1. In Solution Explorer, right-click the modeling project node, and then click Unload Project.

  2. Right-click the modeling project node, and then click Edit ModelingProjectName.modelproj.

  3. In the ModelingProjectName.modelproj file, find the following <Import> element:

    <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\ArchitectureTools\Microsoft.VisualStudio.TeamArchitect.ModelingProject.targets" />

    Under this <Import> element, add the following <Import> element:

    If you have Visual Studio 2010 Visualization and Modeling Feature Pack installed:

    <Import Project="$(LocalAppData)\Microsoft\VisualStudio\10.0\Extensions\Microsoft\<InsertVisualStudioFeaturePackName>\1.0\Microsoft.VisualStudio.Progression.NativeProvider.targets" />

    NoteNote
    %LocalAppData% is typically DriveName:\Users\UserName\AppData\Local. On Windows XP or Windows 2003, use %AppData% instead of %LocalAppData%. If the feature pack was installed in a different location, use that location instead. You can also copy the Microsoft.VisualStudio.Progression.NativeProvider.targets file from the feature pack installation location to the folder that contains the modeling project.

    If you have Visual Studio 2010 Feature Pack 2 installed:

    <Import Project="…$\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\<InsertVisualStudioFeaturePackName>\<InsertVisualStudioFeaturePackVersionNumber>\Microsoft.VisualStudio.Progression.NativeProvider.targets" />

  4. Save the .modelproj file, and then close it.

  5. Right-click the modeling project node, and then click Reload Project.

When you create a layer diagram from C or C++ code in Visual Studio 2010 Ultimate on Windows XP or Windows 2003, an absolute path is used to create the entry in the modeling project (.modelproj) file to import the custom Microsoft.VisualStudio.Progression.NativeProvider.targets file. This might cause problems if you try to open the solution on Windows 7, Windows Vista, or Windows Server 2008. If you are running Visual Studio 2010 Ultimate on Windows XP or Windows 2003, you cannot open layer diagrams that were created in Visual Studio 2010 Ultimate on Windows 7, Windows Vista, or Windows Server 2008.

To resolve this issue, use one of these workarounds:

  1. Copy the Microsoft.VisualStudio.Progression.NativeProvider.targets file from the following location to the folder that contains the modeling project:

    If you have Visual Studio 2010 Visualization and Modeling Feature Pack installed:

    %LocalAppData%\Microsoft\VisualStudio\10.0\Extensions\Microsoft\<InsertVisualStudioFeaturePackName>\1.0

    Tip

    Unless you installed the feature pack in a different location, %LocalAppData% is typically DriveName:\Users\UserName\AppData\Local. On Windows XP or Windows 2003, use %AppData% instead of %LocalAppData%.

    If you have Visual Studio 2010 Feature Pack 2 installed:

    …\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\<InsertVisualStudioFeaturePackName>\<InsertVisualStudioFeaturePackVersionNumber>

  2. In the .modelproj file, update the <Import> element for the .targets file by replacing the absolute path with a relative path for the .targets file. For example:

    <Import Project="Microsoft.VisualStudio.Progression.NativeProvider.targets" />
    

    Important

    If you use version control, you must check the .targets file into version control.

- or -

  1. Create a new environment variable that represents the installation path of the Microsoft.VisualStudio.Progression.NativeProvider.dll file.

  2. Use this variable to import the .targets file.

    For example, you can create an environment variable such as "NativeProviderTargetPath" that points to where the .targets file is located. You can then update the <Import> element as follows:

    <Import Project="$(NativeProviderTargetPath)\Microsoft.VisualStudio.Progression.NativeProvider.targets" />
    

    Important

    Make sure that you set the environment variable correctly on different operating systems.

See Also

Concepts

How to: Create Layer Diagrams from Artifacts

Layer Diagrams: Guidelines

How to: Validate .NET Code Against Layer Diagrams