Use the code generation mechanism to implement strong-type programming-codesimth

Source: Internet
Author: User

I have always wanted to write a code generation series, but I wrote it to codesimth. I found that the blogs of terrylee and the hardworking cubs are very detailed, so I just want to write some examples, however, I did not think of anything to write. Recently, artech has written two articles, from data to code-implementing strong programming through the code generation mechanism-the first and the next articles, where Daniel writes codedom, today, I want to write a codesimth version using the example of Daniel. I hope artech will not blame me. My code generation technology has already written codedom. See the codedom series directory. Thank you for your advice.

Directly to the topic. First, the data entity messageentry (I added the description attribute as the code field description on the basis of the old ):

Code

1 namespace Wolf
2 {
3 Public class messageentry
4 {
5 Public String ID {Get; private set ;}
6 Public String Value {Get; private set ;}
7 Public String description {Get; private set ;}
8 Public messageentry (string ID, string value)
9 {
10 This. ID = ID;
11 This. value = value;
12}
13
14 public messageentry (string ID, string value, string description)
15 {
16 this. ID = ID;
17 this. value = value;
18 this. Description = description;
19}
20
21 public string format (Params object [] ARGs)
22 {
23 return string. Format (this. Value, argS );
24}
25}
26}
27
28

Codesimth on my machine is version 2. 0, so I cannot use the LINQ namespace. I want to use this space more quickly, so I first converted it to 3.0

Dictionary <string, list <messageentry> import the object to the template:

Code:

Code

1 using system;
2 using system. Collections. Generic;
3 using system. LINQ;
4 using system. text;
5 using system. xml;
6 using system. xml. LINQ;
7
8 namespace Wolf
9 {
10 public class messagecodegenerator
11 {
12 public dictionary <string, list <messageentry> generatorcode (string path)
13 {
14 return generatorcode (xelement. Load (PATH ));
15}
16 public dictionary <string, list <messageentry> generatorcode (xelement root)
17 {
18
19 var elemts = root. Elements ("message"). groupby (E => (xattribute) E. Attribute ("category"). value );
20 dictionary <string, list <messageentry> dict = new dictionary <string, list <messageentry> ();
21 foreach (VAR item in elemts)
22 {
23 list <messageentry> List = new list <messageentry> ();
24 foreach (VAR g in item)
25 {
26 if (G. Attribute ("Description ")! = NULL)
27 {
28 list. add (New messageentry (xattribute) g. attribute ("ID ")). value, (xattribute) g. attribute ("value ")). value, (xattribute) g. attribute ("Description ")). value ));
29}
30 else
31 {
32 list. add (New messageentry (xattribute) g. attribute ("ID ")). value, (xattribute) g. attribute ("value ")). value ));
33}
34
35}
36 dict. Add (item. Key. tostring (), list );
37}
38 return dict;
39
40}
41}
42}
43
44

The template can be written in the following code:

Code

1 <% @ codetemplate Language = "C #" targetlanguage = "text" src = "" inherits = "" DEBUG = "false" Description = "template description here." %>
2
3 <% @ import namespace = "system" %>
4 <% @ import namespace = "system. xml" %>
5 <% @ import namespace = "system. Text" %>
6 <% @ import namespace = "system. Collections. Generic" %>
7 <% @ import namespace = "wolf" %>
8 <% @ Assembly name = "wolf" %>
9
10 <SCRIPT runat = "template">
11
12 private dictionary <string, list <messageentry> dict;
13 public dictionary <string, list <messageentry> Generator
14 {
15 get
16 {
17 return dict;
18}
19 set
20 {
21 dict = value;
22}
23}
24
25 Public String generatorcode ()
26 {
27 string STR = "using wolf; \ r \ nusing system. collections. generic; \ r \ nnamespace wolf. message \ r \ n {\ r \ n public class messages \ r \ n {\ r \ n ";
28 foreach (string catage in generator. Keys)
29 {
30 STR + = "public class" + catage + "\ r \ n {\ r \ n ";
31 foreach (Wolf. messageentry entry in generator [catage])
32 {
33 STR + = "\ r \ N // <summary>" +
34 "\ r \ N //" + entry. Description +
35 "\ r \ N // </Summary>" +
36 "\ r \ n public static wolf. messageentry "+ entry. ID + "= new messageentry (\" "+ entry. ID + "\", \ "" + entry. value + "\", \ "" + entry. value + "\"); \ r \ n ";
37}
38 STR + = "\ r \ n} \ r \ n ";
39
40}
41 STR + = "\ r \ n }";
42 return STR;
43}
44
45 </SCRIPT>
46 // copyright (c) Wolf. All rights reserved.
47 <% = generatorcode () %>
48
49

Leave a message if you have any questions. The namespace can be passed in as an attribute.

XML objects use old:

Code

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <messages>
3 <message id = "mandatoryfield" value = "The {0} is mandatory." Category = "validation" Description = "Description"/>
4 <message id = "greaterthan" value = "The {0} must be greater than {1}." Category = "validation" Description = "Description"/>
5 <message id = "reallydelete" value = "Do you really want to delete the {0}." Category = "Confirmation" Description = "Description"/>
6 </messages>
7
8

I want to leave the codesimth tool, so I created a console program and referenced the codesmith. Engine. dll assembly.

Code:

Code

1 static void main (string [] ARGs)
2 {
3 // wolf. Message. Messages. Confirmation. reallydelete. Value
4 // test ();
5 codetemplate template = compiletemplate (@ "E: \ MyApp \ linqtest \ consoleapplication1 \ messagecodegenerator. CST", S => console. writeline (s ));
6 if (template! = NULL)
7 {
8 template. setproperty ("_ messagefilepath ","");
9 wolf. messagecodegenerator Gen = new messagecodegenerator ();
10 dictionary <string, list <messageentry> dict = gen. generatorcode (@ "E: \ MyApp \ linqtest \ leleapplication1 \ sample. xml ");
11 template. setproperty ("generator", dict );
12 template. rendertofile ("gen. cs", true );
13 // system. Diagnostics. process. Start ("gen. cs ");
14}
15 // console. Read ();
16
17}
18
19 public static codetemplate compiletemplate (string templatename, Action <string> errorwriter)
20 {
21 codetemplatecompiler compiler = new codetemplatecompiler (templatename );
22 compiler. Compile ();
23
24 if (compiler. errors. Count = 0)
25 {
26 return compiler. createinstance ();
27}
28 else
29 {
30 For (INT I = 0; I <compiler. errors. Count; I ++)
31 {
32 errorwriter (compiler. errors [I]. tostring ());
33}
34 return NULL;
35}
36
37}
38
39

Generated code:

Code

1 // copyright (c) Wolf. All rights reserved.
2 using wolf;
3 using system;
4 using system. Collections. Generic;
5 namespace wolf. Message
6 {
7. Public class messages
8 {
9 public class Validation
10 {
11
12 /// <summary>
13 // description
14 /// </Summary>
15 public static wolf. messageentry mandatoryfield = new messageentry ("mandatoryfield ",
16
17 "The {0} is mandatory.", "The {0} is mandatory .");
18
19 /// <summary>
20 // description
21 /// </Summary>
22 public static wolf. messageentry greaterthan = new messageentry ("greaterthan ",
23
24 "The {0} must be greater than {1}.", "The {0} must be greater than {1 }.");
25
26}
27
28 public class confirmation
29 {
30
31 /// <summary>
32 // description
33 /// </Summary>
34 public static wolf. messageentry reallydelete = new messageentry ("reallydelete ",
35
36 "Do you really want to delete the {0}.", "Do you really want to delete the {0 }.");
37
38}
39
40}
41}
42
43

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.