Pages

Sunday, July 14, 2013

Generate Sequcen Diagram (Visual Studio)

Following post is about feature provided by MS Visual Studio to generate sequence diagram from already written code.
Before getting into details how the Visual Studio does the task of generated sequence diagram, Following is describe what is sequence diagram (Following description is from Wikipedia).

Sequence Diagram
A sequence diagram is a kind of interaction diagram that shows how processes operate with one another and in what order. It is a construct of a Message Sequence Chart. A sequence diagram shows object interactions arranged in time sequence. It depicts the objects and classes involved in the scenario and the sequence of messages exchanged between the objects needed to carry out the functionality of the scenario. Sequence diagrams are typically associated with use case realizations in the Logical View of the system under development.
Read more on this : http://en.wikipedia.org/wiki/Sequence_diagram

Now in the following way I designed my code and than I am going to show the sequence of this code.View Layer
This layer of the code is View part that is going to display to the end user of the application i.e. the front end of the application.

Business Layer
This layer of the code provides business logic for the fort end part of application . This layer does all the complex activity and communicate with the datalyer.

Data Layer
This layer of the code communicate with the DataBase or DataStructure where the data is stored and bring that data out of it.

Now after doing all this For the new developer in team or to my manager I want to show the flow of code goes from front end of application and data get bring in and out of database or datastructure . Flow of the code need to be cover following thing

How the code flow from one layer to another layer

Which classes are involved

Which method of the classes are involved

So to achieve this Visual Studio provide feature to generate Sequence Diagram which gives answer to above questions.

Generate Sequence Diagram
To generate sequence diagram just right click on the method of the code and in context menu there is option Generate Sequence diagram as you see in image.

Generate Sequence Diagram Window

Once clicked on menu above dialog displayed to use which ask use to provide input for generating sequence diagram. Following are the some important parameter of it.

Maximum call depth - How much deep you want to go i.e. if there are more than one class involved in call and how much deep you want to go.

Include calls in - as the options says include class and methods of project only or full solution or full solution and external references.

Exclude calls to - exclude call made to property, system name space or any other namespaces.

Generated Sequence Diagram
Now once clicked on the dialog generate sequence diagram as shown in above. Above sequence diagram shows the call made to GetCustomer detail to display on front end i.e. to the user of the application.
If you see the code above in images or recall the above image code is divided in three layer and three classes involved which are respectively part of each layer.
Following steps are performed to get data from the database layer displayed by sequence diagram above.

View layer of application i.e. Font ask for getting customer data form business layer by calling Getcustomer method, Which is static method of business class so there is no call to create object of business layer class.

Business layer of application receives the call to method called GetCustomer so to fill the customer details it gives call to datalayer. But before making call it crate instance of datalayer Customer class.

Database layer of application receives call to the method call and in turns it make call to database and retrieve data.

Now database layer returns this data back to business layer customer class.

Business layer does the process of row data return form the datalayer and return the customer object back to view layer.

View layer of fetch value from the object return by business layer and display it to end user

So by the mean of generating sequence diagram I will actually able to find out flow of my code between layer, classes and methods. Also this answer the all the above question.

Note :
I specified 10 in Maximum call textbox of dialog but over here only 3 level is displayed, because as discussed my layer of coding only involve three classes of three layer.

Goto code from Sequence Diagram

With the help of generated sequence diagram by visual studio I can also jump to specify method of code directly by double clicking on the message or method call.
For example I clicked on new Customer create message or method call i.e. on the method call which marked red in above image.

Code on double click from sequence diagram
So after double clicking on the method call it directly redirect me to the code part form the generated sequence diagram. Advantage of the Generating Sequence diagram

Biggest advantage of this is its helps to understand code build by some one else i.e. if you are involved in development of the project and code build by some other developer who is not part of your team and you want to find the flow of the code from start point to end point.

It also help you to explain code build by you and you want to explain to the other developer joining your team.

It displays the all classes and method called in code.

Note :
This feature generates sequence diagram from the method you clicked till end, but it doesn't display the method called before the method you selected.For example
If I clicked on the method of business layer of my project to generate sequence diagram and it generates sequence diagram from that method i.e. from the business layer and database layer. It doesn't display those method which given call to the method on which I clicked, that means I am not able to get information from which Font end method this method is get called.

So when generated sequence diagram by clicking on the business layer method of the code discussed above, it generate sequence diagram display above. As discussed it doesn't display method of view layer which called business layer method.

Conclusion
So this cool feature of visual studio very helpful when you want to explain your own code or you want to understand code.

Thanks for reading and do post comment if you like it or something is missing or wrong.

Thanks for sharing such informative guide on .Net technology. This post gives me detailed information about the .net technology. I am working as trainer in leading IT training academy offering Dot Net Training in Chennai and i use your guide to educate my students.