The role of codesmith is self-evident. People who have used it will think it is very powerful. based on custom templates, codesmith can be quickly generated.CodeOnly when we use it, we need to use the template in the codesmith studio environment provided by it, and then input the corresponding parameters to generate the net class file.
If we can integrate the codesmith function into the net code through encoding, it will be more flexible and convenient for us to control. We can implement it according to the following steps.
First, let's talk about the environment. The codesmith version I installed is 5.1, of course, a cracked version (You know ). codesmith registration fees are expensive. we need the following DLL files, which can be found in the installation path.
Codesmith. Engine. dll
Codesmith. Feedback. Client
Schemaexploer. dll
Schemaexploer. sqlschemaprovider. dll
After these DLL files are introduced into the project file, you need to create a template. You can also directly use the template that comes with codesmith. open codesmith studio and choose template exploer on the right. Right-click a template and choose Open folder in window exploer from the shortcut menu. After confirming the template path, you can start encoding.
Before writing code, let's take a look at the composition of the template, as shown in-1.
Figure-1
In code 2nd, the property keyword indicates the attributes of this template. It is also a parameter that needs to be passed in. The type is schemaexploer. databaseschema. That is to say, we only need to pass in the parameter value of this type.
Code-2
Figure 2
The code is very simple. Load the template before compiling, input the attribute value, and then output it. The template compilation part is the compiletemplate method, as shown in-3.
Figure-3
The code for passing in the property value is codetemplate. setproperty;
The code of the output class file is rendertofile;
OK. The whole process is over. Let's take a look at our results, as shown in-4.
Figure 4
This template is used to generate enumeration classes for all dB tables in the DB database.
Codesmith encoding method is very flexible. For example, if I want to generate the createsql statement for the DB table in the database, we may have spelled the character string before. Now we use the codesmith template, which is very simple. -5. You can refer to this code and try it yourself. Is it very simple?
Code File
Codesmith. Files
Figure 5