A brief introduction to Codesmith
This paper describes how to use this software by automatically generating nhibernate mapping files and examples of map classes .
Codesmith is a template-based code generation tool that uses an ASP. NET-like syntax to generate arbitrary types of code and files. With Codesmith, you can generate anything that includes a simple strongly typed collection and a full application. (Weakly typed-no obvious type, automatic transformation type with different environment; Strong type-Specifies its data type when declaring, guarantees class security, although the system also has a certain default conversion, but no weak type so casually)
When you build an application, you often need to repeat certain tasks, such as writing data access code or building a custom collection. Codesmith is especially useful at these times because you can write templates to automate these tasks, not only to improve your productivity, but also to automate the most tedious tasks. Codesmith comes with a number of templates, including templates that correspond to all. NET collection types and templates for building stored procedures, but the tool's real power is the ability to create custom templates.
Two software downloads
1. Download Codesmith Code Helper Generator
This article uses the CodeSmith6.5.0 perfect cracked version and installs.
2. Download a component of the NHibernate template
Because I'm going to build a nhibernate mapping file and class, but the software doesn't bring it, so you need to download a component of Nhibernate_template, as shown in:
(iii) Operation process
0, the use of SQLServer2008 to establish nhibernate database, table person, structure as follows:
1, using Codesmith to generate NHibernate mapping files and mapping classes
Click the nhibernate.cst file:
Outputdireatory: File Output path
SourceDatabase: Database files that need to be read
Forceid:true forces all tables in the database NHibernate to have a primary key.
Namespace: Namespaces
Removetableprefix: Default
Select the appropriate database-add
After you configure the database connection information, test the testconnection:
Because we are working with the SQLServer2008 database, we choose Sqlschemaprover here. If you are using a different database, you can choose your own.
You can see the automatically generated link string
Finally click the Generate button to automatically generate the mapping file and the mapping class:
3, Analysis Person.hbm.xml
By looking at the auto-generated mapping file Person.hbm.xml, take a look at: How objects are mapped to the table:
?
12345678910111213141516 |
<?xml version=
"1.0" encoding=
"utf-8" ?>
"urn:nhibernate-mapping-2.0"
>
<!
--对象Person和表t_Person建立映射关系-->
<class
name
=
"Test.Model.Person, Test.Model" table
=
"t_Person"
>
<!
--对象Person的Id属性和表t_Person中字段t_Id建立映射关系-->
<id
name
=
"Id" type=
"Int32" unsaved-value=
"null"
>
<
column name
=
"t_Id" length=
"4" sql-type=
"int" not
-
null
=
"true" unique
=
"true"
/>
<generator class=
"native" />
</id>
<!
--对象Person的Name属性和表t_Person中字段t_Name建立映射关系-->
<property
name
=
"Name" type=
"String"
>
<
column name
=
"t_Name" length=
"50" sql-type=
"varchar" not
-
null
=
"false"
/>
</property>
</class>
"font-family:FangSong_GB2312;font-size:18px;"
>
</span>
|
This is the map file generated by the table automatically nhibernate and mapping class (that is, we have handwritten entity) the approximate use of the method, is not very simple it?! It saves a lot of manual work.
The next article will use the generated person mapping files and mapping classes in the demo of the NHibernate tutorial, so stay tuned!
Hibernate mapping File Quick build: Use Codesmith to quickly generate mapping files and mapping classes