NHibernate Templates - C#
Table of Contents
Overview
These are a collection of templates that generate code that use NHiberate Framework.
These templates are designed to produce 5 major things...
- NHibernate XML mapping files.
- Business objects to match the NHibernate maps.
- A Manager object for each business object that provide table specific access methods.
- A NHibernateSessionManager that produces NHibernateSession objects to help abstract NHibernate ISession logic.
- Very basic unit tests for each business object/manager.
Quick Start
- Create a new VisualStudio project.
- Add a new CodeSmith Project (CSP) to your project.
- Right click on the project.
- Select "Add new item..."
- In Common Items there will be a CodeSmith Project.
- Add NHibernateMaster.cst to your CSP's output.
- Configure NHibernateMaster as desired.
- Don't forget to update your Assembly name!
- Create/Update your App.Config to support NHibernate (example below).
- If using the sample below, update the connection string and...
- ...update the mapping assembly.
- Right click on your CSP and Generate Outputs.
- Compile, and enjoy!
Sample App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string"></property>
<mapping assembly="NHibernate.Generated"/>
</session-factory>
</hibernate-configuration>
</configuration>
Dependancies
These templates will only build with CodeSmith 5.0.
These templates include NHibernate version 2.0.0.Alpha1; but they can also support NHibernate 1.2.1
NHibernate 1.2.1 Differences:
- Supports legacy Namespaces.
- UnitTests validate deleted rows differently.
These templates linclude NUnit 2.4.7 for .Net 2.0
These templates support both Visual Studio 2008 and 2005.
Visual Studio 2005 Differences:
- Does not include partial methods in business ojects.
Known Issues
- UnitTests require that each table in the database have at least valid row.
Downloads
Download the latest release from GoogleCode.
History
- Release 1.0.1
- Bug Fix: We now support creating unit test data for varchar(max).
- New Feature: Any table or column name can be overriden by added "cs_alias" to it's extended properties.
- Bug Fix: We now support one table having two foreign keys tied to the same table.
- Release 1.0.0
*Breaking change
Links
NHibernate for .NET
NUnit
Special Thanks
Billy McCafferty
Ayende @ Rahien
NHibernate in Action