Showing posts with label Layer Diagram. Show all posts
Showing posts with label Layer Diagram. Show all posts

Monday, November 23, 2009

[Tutorial] How to use Layer Diagrams in Visual Studio 2010 for Architecture Validation Part 4

Last time we defined layers in our Layer Diagram and added projects and folders. We validated the architecture and saw that there were errors. You might already have guessed it but the explanation is, that the Layer Diagram just does not reflect the real architecture inside the source code of our example project.

Two Choices: Change Source or Diagram

We now have two choices: either we have carefully designed our layers and the errors show that the source code is not compliant, in which case we would have to change the source code, or we decide to change the layer definitions (what we will be doing for our example).

One of the really strong parts of Layer Diagrams is that they help to understand source code from a very high point of view. Imagine trying to understand the source code structure without having any documentation and just the source code. This was a time consuming task without the help of Layer Diagrams but will completely change as of today with Visual Studio 2010.

Change the Layer Diagram

Ok lets change the Layer Diagram and see what the real code structure looks like in terms of layers.

  • Open the Source Code and the Layer Diagram and validate the Architecture
  • Double click on the first error and you see that there are references to Business Objects (ex: ExpenseCategory) in the Presentation Layer projects

Archi_Layer_Validation18

  • Add a new dependency between the Presentation Layer and the Business Layer

Archi_Layer_Validation19

  • Re-validate the architecture and you see that there are still 29 errors, this time there seems to be dependencies inside the Data Layer that are causing the problem

Archi_Layer_Validation20

  • Since the Data Layer also uses Business Objects from the Business Layer we need to change the dependency from a one-way to a bidirectional dependency

Archi_Layer_Validation21

  • Re-validate the architecture and you see that there are still 2 errors, this time there seems to be dependencies inside the Business Layer that are causing the problem

Archi_Layer_Validation22

  • Since the Business Layer also uses interfaces and declarations from the Service Layer we need to change the dependency from a one-way to a bidirectional dependency

Archi_Layer_Validation23

  • Re-validate the architecture and you see that there are no more errors, we finally have a Layer Diagram that perfectly reflects the code structure of the source code

Archi_Layer_Validation24

Summary

We saw how to use the Architecture Explorer and the Layer Diagram to build a layer structure that matches existing code or that may serve to assure, that new projects start and stay with a healthy design. We saw how to validate manually if the layer constraints are fulfilled or broken and how to fix problems.

You may even add the Architecture Validation to your build and check-in scripts in TFS. Thus automating the verification and assuring that your projects retain the initial design that your architects designed carefully.


Share/Save/Bookmark

[Tutorial] How to use Layer Diagrams in Visual Studio 2010 for Architecture Validation Part 3

In the last part we added a Layer Diagram, created some layers and added references to all projects within the example project. This time I will show you how to append projects namespaces or even classes  to the different layers using a new tool called Architecture Explorer. I will then show you how to validate you project based on the layer relations.

Using the Architecture Explorer

The Architecture Explorer is a new tool inside Visual Studio 2010. It serves to understand code structure and to interact with different elements in your code (namespaces, classes, methods, etc…). You can use the Class View or the Solution View to filter and  see what is inside your solution.

  • Open the Architecture Explorer (activate it from the menu if you do not see it in your IDE)
  • Click on Solution View, you will see the different folders and the new Architecture project in the window
  • Click on the Business folder and you will its containing projects

Archi_Layer_Validation12

Defining content in Layers

  • Open the Layer Diagram you created in the last part of the series
  • Display the Layer Diagram and Architecture Explorer
  • Select the contents of the Business folder (5 projects)

Archi_Layer_Validation15

  • Drag and drop them from the Architecture Explorer onto the Business Layer in the Layer Diagram
  • Do the same for all other layers (I added the Hosts & UI projects to the Presentation Layer)

Archi_Layer_Validation13

  • The layers now contain numbers on the leftmost edge, these numbers represent how many projects are contained by the layer (if you added namespaces it would be the number of namespaces, etc…)

Archi_Layer_Validation14

Validating the Architecture

  • Do a right click on the Layer Diagram to open the context menu, you will see the option to validate the architecture

Archi_Layer_Validation16

  • Validate the Architecture, a new screen will pop up and the validations starts

Archi_Layer_Validation17

  • At the end of the validation you will see a report that either says that everything was successful or that there were errors

Validation Errors (a good thing!)

  • In our case we will get 114 errors with the information “AV001: Invalid Dependency …”
  • This means that the source code does not adhere to the architecture design that was defined in the Layer Diagram

Archi_Layer_Validation18

  • If you double click on the first error it will take you to the line of code that generated the validation exception, you may now analyze the source of the problem

You might already know why this doesn’t work, anyways stay tuned until the next article, where I explain in detail what happened and what needs to be done next.


Share/Save/Bookmark

Friday, November 20, 2009

[Tutorial] How to use Layer Diagrams in Visual Studio 2010 for Architecture Validation Part 2

In this part of the series we will create the Modeling Project and the Layer Diagram that will later be used for the Architecture Validation of the example project that was introduced in the last article.

Add Modeling Project and Layer Diagram

  • Add a new project of type Modeling Project

Archi_Layer_Validation5

  • Add a new diagram of type Layer Diagram within the new Modeling Project

Archi_Layer_Validation6

  • Add 4 layers to the Layer Diagram and name them Presentation Layer, Service Layer, Business Layer and Data Layer
  • Connect each layer using a simple arrow and give each layer a different color in their properties

Archi_Layer_Validation9Archi_Layer_Validation8

  • You should now have 16 projects including the new Modeling Project with the Layer Diagram

Archi_Layer_Validation10

  • Add references into the Modeling Project from all other projects, so you will be able to access their namespaces and classes in the next parts of the tutorial

Archi_Layer_Validation11


Share/Save/Bookmark

[Tutorial] How to use Layer Diagrams in Visual Studio 2010 for Architecture Validation Part 1

Visual Studio 2010 includes many new features for architects including Layer Diagrams and Architecture Validation. Architects may use Layer Diagrams to either build a new system from scratch and assure its coherence or understand and validate existing systems. The following series of articles will show how to use these new features. You will see how powerful they can become to assure good design - and that it is really easy to do!

CodePlex Example Project

I searched on CodePlex for a good example of an application that has a Layered Architecture. I found ExpenseSample-V3.5, which is very well adapted. This project is written in .NET 3.5 and is based on the following architecture as described in the CodePlex documentation.

LayerSample-App-Arch-Diagram-Jan-2009

Since this is a .NET 3.5 project it should be converted to .NET 4.0, which is done by using the built in wizard. The project converts very well and there are no errors.

Archi_Layer_Validation1Archi_Layer_Validation2

When opening the solution you will find 15 projects that are divided into different layers (Business, Data, Hosts, Services, UI).

Archi_Layer_Validation3

To assure that the application still works after the .NET 4.0 conversion, we need to build it. The build also works without any errors, which means that everything is in place for the next steps of the tutorial.

Archi_Layer_Validation4


Share/Save/Bookmark