Click here to Skip to main content
11,645,889 members (67,771 online)
Click here to Skip to main content

NHibernate Templates for Smart Code Generator

, 11 Dec 2007 CPOL 141.9K 1.6K 128
Rate this:
Please Sign up or sign in to vote.
Describes how to generate NHibernate objects and ASPX pages using Smart Code
Screenshot - img1.png

Introduction

To learn more about Smart Code please read the next article, by clicking here.

The sample code and templates, are based in the next article NHibernate Best Practices with ASP.NET . In this excellent article, Billy McCafferty, describes best practices for leveraging the benefits of NHibernate 1.2, ASP.NET.

For this article we'll try to be original.....and we'll use the Northwind database as sample, but these examples will work with any databases

Bug Fix on ASPX templates, loading page by ID method

Code Generation, a brief overview

The Smart Code tool makes possible the automatic generation of programs and components that interact with database systems. It works by reading a database schema, allowing the user to set certain extended properties for tables and columns, and applying templates to each table in order to generate code. The templates are themselves programs that access the Smart Code Object Model to produce tailored programs and components. Although Smart Code comes with a set of basic templates, users may edit these or create their own to suit their particular project needs. Moreover, templates may be written in C# or VB.NET (or theoretically in any .NET language that supports the creation of dynamic-link libraries).

Quick Start Guide

Smart Code is a very powerful tool, and perhaps the best way to learn how to use it is to see a sample application running and then go back and examine which features of Smart Code were responsible for the application's functionality.

  • On the web.config and modify the connection.connection_string value
  • Play with the application, open up different pages and entering some data.
  • Open the Newin.scp project with the last release of Smart Code.
  • Compile the NHibernateTemplates and WebTemplates and Load the dlls into Smart Code.
  • Change some properties in Newin.scp project, this will show you the effect of different attribute settings and template assignments.

The NHibernate Template Libraries

The Sample package comes with one set of template libraries for generating 3- tier, web-form-based solutions that use NHibernate to access and modify data. This set of libraries is for generating code in C#; (future releases will support generating code in VB.NET).

In addition, the Sample package includes sample Northwind project in Visual Studio.NET to work with these sets of libraries, so that the generated code may simply be dragged-and-dropped into them.

The Domain Layer

For a description about the domain layer, click here

The Domain layer describing the data access objects and the classes of objects used to transport data between the tiers

Generating the Separated Interface, IDAOFactory

The IDAOFactory Interface provide methods to get objects as DAOs. This class serves as the abstract data. The template to generate the IDAOFactory for all entities in the Smart Code project is the IDaoFactory class in the NHibernateTemplates project.

IDAOFactory Interface

Notice, if you check the Item it Template run only one time, because it is an project level template.

The Code Generated for the IDAOFactory look like this:

 
using System;
using System.Collections.Generic;

namespace Northwind.Core.DataInterfaces
{
    public interface IDaoFactory
    {
        ICategoryDao GetCategoryDao();
        ICustomerCustomerDemoDao GetCustomerCustomerDemoDao();
        ICustomerDemographicDao GetCustomerDemographicDao();
        ICustomerDao GetCustomerDao();
        IEmployeeDao GetEmployeeDao();
        IEmployeeTerritoryDao GetEmployeeTerritoryDao();
        IOrderDetailDao GetOrderDetailDao();
        IOrderDao GetOrderDao();
        IProductDao GetProductDao();
        IRegionDao GetRegionDao();
        IShipperDao GetShipperDao();
        ISupplierDao GetSupplierDao();
        ITerritoryDao GetTerritoryDao();
    }
}
 

The Data Interfaces

The template DataInterfaces generate code for supporting each one concrete DAO factory. Each factory is an is a database-independent implementation, where the implementations will be database specific.

The Template used to generate the DataInterfaces is shwon in the next figure:

The Data Interfaces

And the code generated, for the Product Table look like this

using System;
using System.Collections.Generic;
using Northwind.Core.Domain;

namespace Northwind.Core.DataInterfaces
{
    /// Since this extends the IDao{TypeOfListItem, IdT} behavior, it's a good idea to 
    /// place it in its own file for manageability.  In this way, it can grow further without
    /// cluttering up IDaoFactory.
    public interface IProductDao : IDao<Product, System.Int32>
    {

    }
}

Mapping the Domain to the Database, HBM-XML files

The NHibernateTemplates project provide a class NHibernateHbm, to generate mapping files for NHibernate

Mapping the Domain to the Database, HBM-XML files

This Template generate the HBM files, for tables with single and composite Key and linking tables which will be recognised and the appropriate many-to-one or many-to-many mapping created

Hbm for Table with single key:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Northwind.Core" namespace="Northwind.Core.Domain">
  <class name="Product" table="Products" >
    <id name="ID" type="System.Int32" column="ProductID">
      <generator class="identity"/>
    </id>
    <property name="ProductName" column="ProductName" type="System.String" not-null="true" length="40"/>
    .............
    <many-to-one name="CategoryIDCategories" column="CategoryID" class="Category"  update="0"  insert="0" />
    <many-to-one name="SupplierIDSuppliers" column="SupplierID" class="Supplier"  update="0"  insert="0" />
    <bag name="OrderDetailses" table="Order Details" inverse="true" lazy="true" cascade="delete">
      <key column="ProductID" />
      <one-to-many class="OrderDetail"/>
    </bag>
  </class>
</hibernate-mapping>

Hbm for Table with composite key:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Northwind.Core" namespace="Northwind.Core.Domain">
    <class name="EmployeeTerritory" table="EmployeeTerritories" >
    <composite-id name="ID" class="Northwind.Core.Domain.EmployeeTerritory+DomainObjectID">
        <key-property type="System.Int32" name="EmployeeID" column="EmployeeID" />
        <key-property type="System.String" name="TerritoryID" column="TerritoryID" />
    </composite-id>
    <many-to-one name="EmployeeIDEmployees" column="EmployeeID" class="Employee"  update="0"  insert="0" />
    <many-to-one name="TerritoryIDTerritories" column="TerritoryID" class="Territory"  update="0"  insert="0" />
 </class>
</hibernate-mapping>

The many-to-one and bag are created from the Foreing Key relationship between the tables (InReferences and OutReferences) these references are created when you build the Domain, the next figure shown this concept:

InReferences and OutReferences

Both tables Products and Territories are related by Out References (OutReferences), with the EmployeeTerritories (InReferences).

Generating the C# class

The NHibernateTemplates project provide the class NHibernateClass, to generate the C# class for NHibernate

Generating the C# class

As the mapping, the NHibernateClass support single and composite key, about the IDs please refeer to Generic IDs and Object Comparisons section in the NHibernate Best Practices with ASP.NET Article.

The next code shows a sample how the code for composite keys are generated:

using System;
using System.Collections.Generic;

namespace Northwind.Core.Domain
{
    /// <span class="code-SummaryComment"><summary>
</span>

Where EmployeeTerritory+DomainObjectID overrides the Equals and GetHashCode Methods.

Web Forms for Listing Records

SmartCode IDE

These templates generate Web Forms for listing records. All of them display records in an ASPGrid, 20 records at a time (by default), with controls for browsing the grid's contents; also, column headers may be clicked to sort rows and paging.

SmartCode IDE

Each row in the grid has hyperlinks to edit and view the corresponding record, and the pages include a "Add" button to create a new record. The grid is automatically populate when the page is loaded

Web Forms for Editing Records

SmartCode IDE

These templates generate Web Forms for creating and editing records.

SmartCode IDE

Moreover, these templates recognize attributes like the Control property has been set to ComboBox or Check Box.

SmartCode 
       IDE

The ComboBox control combinated with LOV property, produce custom DataTextField properties in the dropDown Web Control.

SmartCode 
       IDE

Summary

The possibilities for Smart Code templates are unlimited. Smart Code is an exceptionally powerful developer productivity tool. Coupled with the NHibernate templates you can write the base code for your application in a matter of minutes.

Web Application Project

The Sample Code uses the Web Application Project type of website for building web applications. This kind of web site is very similar to the .Net Framework 1.1 model.

You can get the installer for Web Application Projects at http://download.microsoft.com/download/9/0/6/906064ce-0bd1-4328-af40-49dca1aef87c/WebApplicationProjectSetup.msi. After downloading it you can just run it.

Changelog

  • Minor bug fix in composite-key definition

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Danilo Mendez
Web Developer
United States United States
Danilo is the creator of SmartRules, a Business Rules Engine. He is an industry consultant working primarily with companies interested in implementing dynamic rules programming concepts to add flexibility to their architectures on web, CE, and desktop platforms. He operates his own website, Kontac, where you will find more information.

To contact Danilo, email him at danilo.mendez@gmail.com.

You may also be interested in...

Comments and Discussions

 
GeneralUnable to download any thing Pin
justintimberlake29-Aug-09 17:06
memberjustintimberlake29-Aug-09 17:06 
QuestionSmartCode version problem Pin
Francesca Nardi28-Apr-09 5:23
memberFrancesca Nardi28-Apr-09 5:23 
QuestionSmartCode version problem Pin
Francesca Nardi28-Apr-09 5:31
memberFrancesca Nardi28-Apr-09 5:31 
AnswerRe: SmartCode version problem Pin
Danilo Mendez Gamonal30-Apr-09 9:33
memberDanilo Mendez Gamonal30-Apr-09 9:33 
GeneralCannot compile mapping document Pin
brenor6-Apr-09 6:28
memberbrenor6-Apr-09 6:28 
GeneralStart using SmartCode Pin
MarcoFIerro4-Feb-09 11:17
memberMarcoFIerro4-Feb-09 11:17 
GeneralRe: Start using SmartCode Pin
LeonelCorso3-Apr-09 9:54
memberLeonelCorso3-Apr-09 9:54 
GeneralCombine it with a visual designer Pin
ElGuroProgramador30-Jul-08 7:17
memberElGuroProgramador30-Jul-08 7:17 
GeneralSQL Create statements Pin
ElGuroProgramador30-Jul-08 7:08
memberElGuroProgramador30-Jul-08 7:08 
GeneralI can not download the studio Pin
sonnywong29-Jun-08 23:19
membersonnywong29-Jun-08 23:19 
GeneralI don't get code for Stored Procedures Pin
Gerardo Palazuelos G.16-Jan-08 12:00
memberGerardo Palazuelos G.16-Jan-08 12:00 
GeneralRe: I don't get code for Stored Procedures Pin
Danilo Mendez17-Jan-08 10:24
memberDanilo Mendez17-Jan-08 10:24 
Questionhow nhibernate-mapping.xsd was created Pin
Thanks for all the fish12-Dec-07 8:12
memberThanks for all the fish12-Dec-07 8:12 
GeneralRe: how nhibernate-mapping.xsd was created Pin
Danilo Mendez12-Dec-07 10:31
memberDanilo Mendez12-Dec-07 10:31 
GeneralDomain Model VS Domain Tables Pin
omarvr7212-Dec-07 4:20
memberomarvr7212-Dec-07 4:20 
GeneralRe: Domain Model VS Domain Tables Pin
Danilo Mendez12-Dec-07 10:31
memberDanilo Mendez12-Dec-07 10:31 
QuestionHow to debug templates Pin
giammin20-Sep-07 5:35
membergiammin20-Sep-07 5:35 
AnswerRe: How to debug templates Pin
Danilo Mendez20-Sep-07 5:55
memberDanilo Mendez20-Sep-07 5:55 
GeneralRe: How to debug templates Pin
giammin20-Sep-07 6:11
membergiammin20-Sep-07 6:11 
GeneralProject template checkbox Pin
giammin19-Sep-07 8:09
membergiammin19-Sep-07 8:09 
GeneralRe: Project template checkbox Pin
giammin19-Sep-07 9:59
membergiammin19-Sep-07 9:59 
GeneralRe: Project template checkbox Pin
Danilo Mendez20-Sep-07 5:47
memberDanilo Mendez20-Sep-07 5:47 
NewsVisual Studio Web Applications Projects is now part of VS 2005 SP1 [modified] Pin
Abel Braaksma11-Sep-07 12:45
memberAbel Braaksma11-Sep-07 12:45 
Generalcannot assign views to template Pin
ukungx15-Aug-07 4:18
memberukungx15-Aug-07 4:18 
views don't appear in object list so i cannot assign view to template, i have added views before clicked generate icon, i also checked the checkbox under objects list pane but the views still doesnt appear so i cannot assign view to the template

i use nhibernate/web template from example including in this article

thank you for your smart code, its make life happier Smile | :)
QuestionRe: cannot assign views to template Pin
ukungx19-Aug-07 21:38
memberukungx19-Aug-07 21:38 
AnswerRe: cannot assign views to template Pin
Danilo Mendez5-Sep-07 1:17
memberDanilo Mendez5-Sep-07 1:17 
GeneralFor those who prefer Mapping.Attributes.... Pin
DamonCarr14-Aug-07 3:09
memberDamonCarr14-Aug-07 3:09 
GeneralRe: For those who prefer Mapping.Attributes.... Pin
Danilo Mendez14-Aug-07 4:57
memberDanilo Mendez14-Aug-07 4:57 
GeneralRe: For those who prefer Mapping.Attributes.... Pin
DamonCarr20-Aug-07 6:09
memberDamonCarr20-Aug-07 6:09 
GeneralSome Changes to Make it work for me Pin
Andi Baso11-Aug-07 19:09
memberAndi Baso11-Aug-07 19:09 
GeneralRe: Some Changes to Make it work for me Pin
Danilo Mendez13-Aug-07 12:22
memberDanilo Mendez13-Aug-07 12:22 
GeneralRe: Some Changes to Make it work for me Pin
Andi Baso13-Aug-07 16:45
memberAndi Baso13-Aug-07 16:45 
GeneralOracle connection problem Pin
VladimirCh16-Jul-07 22:10
memberVladimirCh16-Jul-07 22:10 
GeneralRe: Oracle connection problem Pin
Danilo Mendez17-Jul-07 1:32
memberDanilo Mendez17-Jul-07 1:32 
GeneralRe: Oracle connection problem Pin
ghollins20-Jul-07 5:31
memberghollins20-Jul-07 5:31 
GeneralRe: Oracle connection problem Pin
VladimirCh20-Jul-07 5:40
memberVladimirCh20-Jul-07 5:40 
GeneralRe: Oracle connection problem [modified] Pin
Danilo Mendez20-Jul-07 6:13
memberDanilo Mendez20-Jul-07 6:13 
GeneralRe: Oracle connection problem Pin
ghollins20-Jul-07 6:40
memberghollins20-Jul-07 6:40 
GeneralRe: Oracle connection problem Pin
ghollins20-Jul-07 6:47
memberghollins20-Jul-07 6:47 
GeneralRe: Oracle connection problem Pin
Danilo Mendez21-Jul-07 7:40
memberDanilo Mendez21-Jul-07 7:40 
GeneralI can't open your site:http://www.kontac.net/WelcomeSC.aspx Pin
paulyuan7-Jul-07 21:53
memberpaulyuan7-Jul-07 21:53 
GeneralRe: I can't open your site:http://www.kontac.net/WelcomeSC.aspx Pin
Danilo Mendez Gamonal8-Jul-07 0:54
memberDanilo Mendez Gamonal8-Jul-07 0:54 
GeneralRe: I can't open your site:http://www.kontac.net/WelcomeSC.aspx Pin
paulyuan9-Jul-07 21:52
memberpaulyuan9-Jul-07 21:52 
GeneralFew Items Pin
StevieGen4-Jul-07 16:04
memberStevieGen4-Jul-07 16:04 
GeneralRe: Few Items Pin
Danilo Mendez Gamonal4-Jul-07 16:39
memberDanilo Mendez Gamonal4-Jul-07 16:39 
QuestionJava/Hibernate Pin
The Wizard of Doze4-Jul-07 7:56
memberThe Wizard of Doze4-Jul-07 7:56 
AnswerRe: Java/Hibernate Pin
Danilo Mendez Gamonal4-Jul-07 8:39
memberDanilo Mendez Gamonal4-Jul-07 8:39 
GeneralRe: Java/Hibernate Pin
The Wizard of Doze4-Jul-07 10:18
memberThe Wizard of Doze4-Jul-07 10:18 
GeneralConnection String Pin
StevieGen3-Jul-07 14:41
memberStevieGen3-Jul-07 14:41 
GeneralRe: Connection String Pin
Danilo Mendez Gamonal3-Jul-07 14:53
memberDanilo Mendez Gamonal3-Jul-07 14:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 11 Dec 2007
Article Copyright 2007 by Danilo Mendez
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid