Analysis of Nhibernate generated code using the codesmith Template

Source: Internet
Author: User
Codesmith is a common code generation tool. It generates different code from different templates, which can greatly accelerate project development and reduce repetitive work. The nhib.pdf template is one of its common templates. You can download the latest template file from here. Now the latest version is NHibernate-v1.2.1.2125 that can generate code for nhibernate1.2, 2.1, 3.0. I downloaded a little earlier, is the NHibernate-v1.1.7.2056, the code generated up to 2.1, but the same, I downloaded the version for analysis.


The basic generation operation will not be mentioned. There are videos on the official website, and a large number of netizens have made special tutorials. After the code is generated, it can be divided into five main parts:

1. the base block, that is, the base block. Here we put the businessobjects block, managerobjects block, and the base class of the unittests block. It also includes a small isession module provided by the author to manage NH: nhibernatesessionmanage block.

2. businessobjects block, which is the entity/model block we often call. There are various entities that correspond to the database tables one by one.

3. hbmmaps block. NH is required.

4. The managerobjects block, which is also known as the BL layer, contains operation classes for each object. The naming method is similar to xxxmanage. In addition, there is a small factory that instantiates different management classes through different methods, which is relatively simple and does not contain tables.

5. unittests blocks and test case blocks are not the focus of our discussion today.


As you can see above, the Code generated by the NH template is a typical three-tier architecture code, and the architecture mode is activity recording. For a more detailed description of the concept, please google it yourself, or refer to my other two blog posts: business logic architecture mode (transaction script, table module, activity record, domain model ), let's talk about the business logic architecture model (transaction script, table module, activity record, and domain model ).

Because the base classes in the base block have a large relationship with the sub-classes in the following three blocks, I will explain them in the order of businessobjects blocks, managerobjects blocks, and nhibernatesessionmanage blocks, the base class is directly expressed together during the presentation process. For example, see the following figure. In this example, aim is an object class and aimmanage is its corresponding management class.

The first is the businessobjects block. In fact, our more common name is the entity layer. Aim is our own entity, and businworksheet <t> is its base class. The generic type T refers to the type of the primary key. If it is a joint primary key, it is not a basic type here, instead, a class is generated separately. The attributes of this class correspond to each sub-element of the joint primary key one by one. <T> implements the ibusinproductname <t> interface, which specifies an important attribute: ID, whose class type is specified by T, this indicates that the primary keys of all tables are called IDs in the code. Of course, it does not matter if the primary key in the actual table is not called ID. The object configuration file HBM. XML can solve this problem. This interface also has two methods: gethashcode and equals, which are relatively simple and not many tables.


Let's take a look at the managerobjects block. We also have a more common name for this: the BL layer or the business logic layer.

From the top down, it is initially an imanagerbase <t, tkey> interface, where T refers to the entity category managed by it, and tkey is the primary key type managed by it. This interface defines common object operation methods: add, delete, modify, and query in various situations. The managerbase <t, tkey> class is its specific implementation. Iaimmanage is an interface for a specific object management class, which defines the specific operation methods of a specific object. Note that the imanagerbase <t, tkey> interface is inherited here, which is very important,. The aimmanage class inherits the managerbase <t, tkey> class and the iaimmanage interface. Obtain the entity of a common operation from the former, and obtain the defined specific operation from the latter and implement it by yourself. Here we will explain why the iaimmanage interface inherits imanagerbase <t, tkey>. From the compilation perspective, the former can still be compiled without inheriting the latter, but from the perspective of our architecture code, there are two ways to operate the aimmanage class in encoding, one is to directly use this type of operation:

Aimmanage manage = new aimmanage ();

One is to operate through an interface:

Iaimmanage manage = new aimmanage ();

To reduce code coupling, interface operations are ideal. If the former does not inherit the latter, when the latter code appears, we cannot use manage to operate the generic operations that aimmanage classes inherit from the managerbase <t, tkey> class. In other words, because the managerbase <t, tkey> class inherits and implements the imanagerbase <t, tkey> interface, the aimmanage class inherits the managerbase <t, tkey> class, in fact, the aimmanage class indirectly inherits and implements the imanagerbase <t, tkey> interface. Now the aimmanage class inherits and implements the iaimmanage interface. From the perspective of Multi-inheritance, each interface can only operate on attributes and methods inherited from its subclass, such:


If the iaimmanage interface does not inherit the imanagerbase interface, when the code is written as follows:

Iaimmanage manage = new aimmanage ();

The Manage variable cannot execute the () method because the () method comes from the imanagerbase interface.


Finally, let's take a look at the nhibernatesessionmanage block. This is a session management module provided by the author, including two classes and two interfaces. This module has little interaction with other modules through interfaces, so there are not many tables. It mainly describes the use of two classes: The nhibernatesessionmanager class and the nhibernatesession class. The nhibernatesession class is a session class reencapsulated by the author. It encapsulates the original isession interface to replace the original isession interface of NH. There are two important members: isession and itransaction. Isession is the original isession interface of NH. Because he encapsulates the original isession interface here, it is not very convenient to operate the transaction through it, so the author uses the itransaction member to reference the itransaction in isession, and wrote a lot of methods to complete the transaction operations. Another method is getisession () to obtain the original isession.


The nhibernatesessionmanager class is a specific session management class. It implements the singleton mode through the static variable instance. It has an important attribute session and an important method createisession (). Obtain the nhibernatesession class of this operation through the session attribute. As you can see, the author puts the nhibernatesession of each operation into the cache (where webform and winform are placed differently), improving performance. The createisession () method actually obtains the original isession, And the getisession () method of the nhibernatesession class calls it.

Public isession getisession ()
If (isession = NULL)
Isession = nhibernatesessionmanager. instance. createisession ();
Return isession;

Return to the managerbase <t, tkey> class, in the two constructors:

Public managerbase ()
: This (nhibernatesessionmanager. instance. Session ){}
Public managerbase (inhibernatesession session)
This. Session = session;
This. session. incrementrefcount ();

By default, the session attribute of the nhibernatesessionmanager class Singleton instance is called or injected by customizing the inhibernatesession.


The above is the whole process of code analysis. We can see that the generated code is compact and easy to use. It is a good demonstration of the three-tier architecture model, activity recording model and production practice.

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: 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.