Visual NHibernate can be customized in many ways. Today we’ll look at the built-in template engine. You can easily create the exact output you need by tweaking the built-in templates or creating new custom templates.
Visual NHibernate currently has two built-in templates:
The template editor enables you to specify how the generated files and folders should be laid out in the destination folder, WYSIWYG style. Script can be written to generate dynamic files, while static files such as images and DLLs can also be included in the output.
The code editor has IntelliSense and syntax-highlighting, as well as live feedback about errors. Compile errors are underlined in red squiggly lines. They also appear in the errors grid and cause the title-bar to change color from green to red.
Find and replace can be accessed via the standard keyboard shortcuts (Ctrl+F, Ctrl+Shift+F, Ctrl+H, Ctrl+Shift+H).
Add the first folder or file by right-clicking the ‘Output Folder’ node in the treeview.
Folders and files can have iterators. You can generate either a single file, or generate one file or folder per Entity, Table, Column, Component or View. If you select an iterator then you will have access to the iterator in script via a lowercase name, such as entity.Name or table.Name.
File-names and folder-names can be dynamic and can contain script:
Add a folder by right-clicking on the parent folder.
A dynamic file is one in which the body is created dynamically via script. Add a file by right-clicking on the parent folder.
A static file is one that is distributed as-is. It can be an image file, a readme file, a DLL etc.
Static files need to be added to the Resources collection. To do this, select the Resources tab on the left then click the Add button to select the file.
Dynamic files output text. Create dynamic content by using either ASP-style delimiters <%...%> or T4-style delimiters <#...#>.
Select the delimiter style towards the top right corner of the screen.
Dynamic text can be inserted into the document in a number of ways:
A dynamic file can be skipped via script by setting SkipThisFile = true and returning the reason. For example:
SkipThisFile = true;
return “Project.Settings.UseFluentNHibernate is false”;
Visual NHibernate has a smooth edit-test cycle, giving immediate feedback on your template and scripts. It also enables you to test individual entities, tables, views etc.
The API available for scripting exposes all details about the current project. You have full access to all properties or entities, tables, relationships etc.
The editor has full IntelliSense and the API is easily discoverable:
Here’s a small portion of the API:
Visual NHibernate can reverse-engineer your existing NHibernate Visual Studio projects. Your existing code becomes the model. This enables you to create new code based on your existing code. Create a new template, import your existing code, generate new code.
Custom templates give you a tremendous amount of freedom and power. The best way to learn about the capabilities of templates is to inspect the built-in templates. Download Visual NHibernate, have a play, and tell us what you think.