In the previous article, "ORM Weapon: NHibernate (a) Introduction", we have a brief introduction to NHibernate, the next series of articles will be in the form of demo and you share the use of NHibernate preliminary use.
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 file that needs to be read
Forceid:true forces all tables in the database NHibernate to have a primary key.
namespace: namespace
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:
<?xml version= "1.0" encoding= "Utf-8"? >
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!