Before entering the topic, let's talk about the EA tool: ea -- Enterprise impact ect, which is a modeling tool. For details, click here.
Here
With the improvement of capabilities, the current projects require class diagrams first and then coding. However, after each drawing, the first frame of the Code will correspond to the diagram. Later, I found that there is something inappropriate in the figure. Then I changed the figure and typed the Code. However, if there are too many errors in the figure, I am too lazy to change the diagram and directly modify the code. If this takes a long time, the graph will be discarded. After the development is complete, a person with a better attitude will use code to generate a delivery gap, and some will simply ignore it. As a result, the work of other colleagues in the same project team will be greatly affected. Of course, I have mentioned many such cases in small companies. Of course, we will also encounter this situation when we are self-taught.
How can this problem be solved?
Fundamentally speaking, of course, the analysis is in place, and the drawing process is well considered. programmers must strictly encode the image and document. You cannot change the code without authorization. If you need to change the settings, submit the suggestions to the project manager for review. The project manager will modify the suggestions. Of course, many companies or individuals do not follow these steps. After all, changing it will waste a lot of time. So we can solve this problem by synchronizing the graph and code.
Let's start with the idea: I use EA and vs2010 to draw class diagrams with EA and then generate a code framework. Add the generated code in the vs project and modify the code in the vs project. You can use reverse code engineering in the EA to update the original class diagram.
A simple demonstration:
First, create a project. Here I create a console application:
Then draw a class chart in EA
Right-click and select generate code: (because I am a single class, select generate code directly on the class. If you have many classes in the class diagram, you can select the generated code on the icon similar to the folder above)
Select the project folder and the corresponding language. Here I use C #. Click Generate.
In this way, a human. CS file is added to the project folder.
Now this graph has a relationship with the Code. Because EA has integrated the Code Editor, we can right-click the class and choose View Source Code:
You can open the source code:
Then, in the vs project, add the newly generated CS,
In this way, a human class is added to the vs project:
Open and take a look at the Code directly generated by the graph:
//////////////////////////////////////// /// // Human. CS // Implementation of the class human // generated by enterprise effecect // created on: 23-May 23-2012 17:18:28 // Original Author: admin /////////////////////////////////////// /// // namespace human {// <summary> /// <B> Student </B> /// <B> includes three fields: name, age, sex </B> /// <B> Add the following two methods: name, age, sex </B> /// <B>: </B> // <B> constructor and print </B> /// </Summary> public class human {// <summary> /// age // </Summary> private int age; /// <summary> /// name /// </Summary> private string name; /// <summary> /// gender /// </Summary> private string sex; Public Human (){}~ Human () {} Public Virtual void dispose () {}/// <summary> /// constructor /// </Summary> /// <Param name = "name"> name </param> // <param name = "Age"> age </param> // <Param name = "sex"> gender </param> Public Human (string name, int age, string sex) {}/// <summary> // age // </Summary> Public int age {get {return age ;} set {age = value ;}/// <summary> /// name // </Summary> Public string name {get {return name ;} set {name = value ;}/// <summary> // output student information /// </Summary> Public void print () {}/// <summary> /// gender /// </Summary> Public String sex {get {return sex;} set {sex = value ;}}} // end human} // end namespace human
Now the code and graph can be completely synchronized. After modifying the code in Vs, In EA, right-click the corresponding class and choose | "Code Synchronization" to synchronize the modified part of the code to the class diagram.
For example, we add an address field and attribute in the code, and add content in the build function and print method:
Address field and attribute:
/// <Summary> /// address /// </Summary> private string address; /// <summary >/// address /// </Summary> Public String address {get {return address;} set {name = value ;}}
BUILD functions and print methods:
/// <Summary> /// constructor /// </Summary> /// <Param name = "name"> name </param> /// <Param name = "Age"> age </param> // <Param name = "sex"> gender </param> Public Human (string name, int age, string sex) {This. name = Name; this. age = age; this. sex = sex;} // <summary> // output student information // </Summary> Public void print () {system. console. writeline ("My name is" + name + "," + sex + ", "+ age );}
In EA, right-click human, select "Code Synchronization", and click "yes ":
Let's look at the effect of the class:
The update is successful.
After modifying the class diagram, select the modified class and generate the code again to update the original code.
Now we add a telphone and its corresponding attributes to the human class:
Right-click the source code after the code is generated:
//////////////////////////////////////// /// // Human. CS // Implementation of the class human // generated by enterprise effecect // created on: 23-May 23-2012 17:23:58 // Original Author: admin /////////////////////////////////////// /// // namespace human {// <summary> /// <B> Student </B> /// <B> includes five fields: name, age, sex, address, telphone </B> // <B> Add three attributes: name, age, sex, address, telphone </B>/ // <B> two methods: </B> /// <B> constructor and print </B> /// </Summary> public class human {// <summary> // age /// </Summary> private int age; /// <summary> /// name /// </Summary> private string name; /// <summary> /// gender /// </Summary> private string sex; /// <summary> /// address /// </Summary> private string address; /// <summary> /// call /// </Summary> private int telphone; /// <summary> /// address // </Summary> Public String address {Get {return address;} set {name = value;} public human (){}~ Human () {} Public Virtual void dispose () {}/// <summary> /// constructor /// </Summary> /// <Param name = "name"> name </param> // <param name = "Age"> age </param> // <Param name = "sex"> gender </param> Public Human (string name, int age, string sex) {This. name = Name; this. age = age; this. sex = sex;} // <summary> // age // </Summary> Public int age {get {return age;} set {age = value ;}} /// <summary >/// name /// </Summary> Public string name {get {return name ;}set {name = value ;}} /// <summary> /// output the student information /// </Summary> Public void print () {system. console. writeline ("My name is" + name + "," + sex + ", "+ age );} /// <summary >/// gender /// </Summary> Public String sex {get {return sex;} set {sex = value ;}} /// <summary >/// call // </Summary> Public int telphone {get {return telphone ;}set {telphone = value ;}}} // end human} // end namespace human
We found that the source code section of the modified constructor and print method was not deleted. This indicates that the image only updates the Code, rather than overwrites the code.
This is the demonstration.
Through the demo just now, we can clearly see that the diagram and Code Synchronization are no longer so far away. We no longer need to change the diagram to change the code. It can save a lot of time.
========================================================== ========================================================== ========
I used a file for demonstration, but in actual projects, it must be multiple files. Some bloggers asked me what to do, so I will try again.
Experiment with one of my small projects: package diagram, class diagram:
When generating code, if you generate the source code for the first time, right-click the entire class view and choose "Code project"> "generate source code ". In this way, all graphs are generated. If you modify the class in a package and update the code using diagrams, you can right-click the package and choose "Code project"> "generate source code". The effect is the same. The following figure generates code on the Class View.
In the title of the dialog box, you can see "generate source code based on package ". The namespace is also included. In synchronization conflicts, select "Synchronization Model and code ". Select the automatic file generation check box and select your project path (same as that of a single file ). If you have sub-packages in the package diagram, you need to check "tables including all sub-packages ". Generate.
This is the result of generation:
If you want to synchronize all class diagrams when updating class diagrams with code, right-click the Class View and choose "Code project"> "import source file directory ". If you only want to update a package, right-click the package and perform the same operation. I chose the idal package here (this project is now attached using VB. NET ).
In the "Import Source code" dialog box, select the root directory of the Code (this must be clear when updating a package, otherwise, the classes of other packages will be imported into this package ). The file type is correct. Select "generate package for each directory" for the package structure. Otherwise, you may not want to see the effect.SynchronizationSelect "Synchronize existing classes" as the type ". If you select override, the relationships between other classes and updated classes will be deleted, and the objects of updated classes in the sequence chart will become blank. Therefore, you must select synchronization.DeleteDepends on the actual situation. If some classes are deleted from the code, update the class diagram.
According to your selection.
All right, there are so many operations on multiple files in the project. The paper may end with a glimpse of it, and we know that it is necessary to practice it.
========================================================== ========================================================== ========
Of course, this solution is only a remedy for code and disconnections. The most important thing is to do the best possible design in the early stage. When coding, the code is strictly encoded according to the graph and the document, so that the code and the diagram won't be out of touch.
Looking back, we found that we always follow the "simple principle" intentionally or unintentionally. It is precisely because of the "simple principle" that our efficiency will continue to improve, we will continue to improve.
Reprinted please explain the source http://blog.csdn.net/xiaoxian8023/article/details/7901206