The new version of Visual Studio (Ultimate) brings a lot of new architecture features with it.
It allows you to create UML sequence diagrams from extisting code. Or it visualizes the most important components in your application.
This visualizations are very helpful when you have to modificate/extend an existing project written by other developers.
Here I want to show you the “Model Validation” Feature.
You can add a Model Validation project into your solution. Inside the designer you have the ability to define some “rules”.
I.e. class Calculator uses class Math and not otherwise.
But this doesn’t generate source code! It is only used for validation.
Here is a short example.
I created two classes. Calculator and Math. The Calculator uses the class Math for executing some calculations.
And in my Architecture, it’s not allowed to call the Calculator from the Math class.
public class Math { public int Add(int s1, int s2) { return s1 + s2; } } public class Calculator { public Math Math { get; set; } public int Calc(int s1, int s2) { this.Math = new Math(); return this.Math.Add(s1, s2); } }
This code works well. Now I define my architecture:
– Add a new Modeling Project to your solution. |
– In the Layer References add your Libraries. |
– Add a Layer Diagram. |
– Drag and drop the classes into the diagram. |
– Create a Dependency by using the “Dependency” Tool from the Toolbox. |
A picture says more than thousand words…. 😉
Now right-click on the diagram and click “Validate Architecture”.
Inside the Output Window you should see this message:
16.11.2010 19:32:58: Architecture validation is starting.
16.11.2010 19:33:00: Architecture validation succeeded (0 suppressed).
Now you can finish your Calculator Application and at the end you can check the Architecture again.
I this example, I’ll extend the Math class with one Method. This Method creates and object of the Calculator class. (Don’t forget, this is still not allowed in my Architecture).
The Project will still compile successful. But when you validate the Architecture you will receive an error.
Example:
public class Math { public int Add(int s1, int s2) { return s1 + s2; } public void Nonsense() { Calculator c = new Calculator(); } }
After the validation following error occures:
AV0001 : Invalid Dependency : ClassLibrary1.Math.Nonsense(Method) –> ClassLibrary1.Calculator(Type)
Cool! 😉
Now you can generate the Dependencies (right-click – Generate Dependencies).
The result:
The Designer analyzes your source code and shows you the actual Architecture.
I don’t like the “generate Dependencies” function. It redesigns your Architecture. After generating your application will be valid. This doesn’t really make sense for me.