Many code generators have chosen to generate a domain model from the table structure, the premise of such a scheme is that the domain model and database table structure are isomorphic, that is, the class in the domain model and the record structure in the database are very consistent, so that the database table structure can be directly mapped to the domain model. However, in the case of complex business logic, object scenarios and relational scenarios often do not match, and usually there is a data mapper between the two to isolate the two, at which point the two are mutually invisible and evolve independently of each other. In this way, the class of the code generation domain model based on the table structure does not work, and the object model should be used. And this kind of code generation almost does not need to write the code generator itself, you can use PowerDesigner9. PowerDesigner9 already has the code generation function, you just build a object_oriented Model (language select C #, Class diagram), complete class design, Use the "Generate C # Code" Under the Language menu. After generating a look at the code file, all the properties do not have get and set methods, in order to generate these two methods, you have to modify the PowerDesigner code generation template, you can choose the Language menu under the "Edit current Object Language , modify the code template in the pop-up window: You can see the code-generated script in the Value section, and if you've written a template using code generation tools like Codesmith, this script is easy to understand. As long as you can modify it, for example, I put the third line of which: [%visibility%][%flags%]%datatype% _%code%[=%initialvalue%]; The sentence changed into: private%datatype% _%code%[=%initialvalue%]; Public%datatype%%code% { get { return _%code%; } set { value = _%code%; } The other two if branches also make the corresponding modifications, and then generate a look, get and set functions have.
The code template for C # is the resource Files/object under the PowerDesigner installation directory The languages directory of the Csharp.xol file, open to see is actually an XML file, so you can define your own code generated template. above is the use of PowerDesigner to generate C # code directly, for the sake of universality, the preferred XML, you can select the XML Schema for the object model language, the object model to generate an XSD file, Then write a code generator of your own, I made a very simple example, interested friends can see. PowerDesigner generated XSD file contents: <?xml Version = "1.0"?> <xsd:schema name= "Objectorientedmodel_1.xsd" Http://www.w3.org/2001/XMLSchema "> <xsd:element name=" Customers "> <xsd: Element name= "ID" type= "int"/> <xsd:element name= "name" type= "string"/> <xsd: Element name= "Address" type= "string"/> </xsd:element> <xsd:element name= "Orders" > & nbsp; <xsd:element name= "ID" type= "int"/> <xsd:element name= "CustomerID" type= "int"/> & nbsp; <xsd:element name= "ordernumber" type= "int"/> </xsd:element> </xsd:schema> I also used an XML file asGenerator configuration config.xml file contents: <?xml version= "1.0"?> <template> <xsdfile Name= ' z:/study/cs/objectorientedmodel_1.xsd '/> <outputdirectory name= ' Z:/csfile '/> <namespace name = ' Dahuzizyd '/> </template>
My machine development environment and Vs.net conflict, had to write one in Python, but only less than 50 lines, should be more easily read: from xml.dom import minidom
Import OS
# get Config
Configdoc = Minidom.parse (' config.xml ')
Xsdfilename = Configdoc.getelementsbytagname (' xsdfile ') [0].attributes[' name '].value
namespace = Configdoc.getelementsbytagname (' namespace ') [0].attributes[' name '].value
OutputDirectory = Configdoc.getelementsbytagname (' outputdirectory ') [0].attributes[' name '].value
# load XSD file
Xsddoc = Minidom.parse (xsdfilename)
Basenode = Xsddoc.childnodes[0]
# Create. cs file
For node in Basenode.childnodes:
If Node.nodetype = node. Element_node:
filename = node.attributes["name"].value
f = open (outputdirectory + filename + '. cs ', ' W ')
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.