Fluent NHibernate Mapping Generator

Summary

I finally did it. I completed my new Fluent NHibernate mapping generator. It has several features that make it more powerful than the hacked together console application that I did earlier this year. Included in this package are objects that use the generated code to create unit tests around SQLLocalDB.

The Mapping Generator

The mapping generator has an interface allowing you to choose which SQL server to create mappings from. Once you select your server, a check list of databases will be shown allowing the selection to the database level. Once one or more databases are selected, click the “Generate” button and your database mappings will be generated in the project named “NHibernateDataLayer”. You can change the location of this directory to any directory or project file. If you choose a directory, your mappings will be created inside the directory and you will need to manually add your cs files to a project to use them. If you designate a project, the mapping cs files will be generated in the project directory and the project file xml will be updated so they show up in the Visual Studio project list.

The generator will create a directory with the name of the database. Inside that directory will be four directories for tables, views, stored procedures and constraints. The tables are used by Fluent NHibernate to become the mappings for the ORM. The views, stored procedures and constraints are definitions that will allow your unit tests to generate these objects in a SQLLocalDB before running a unit test. If you change your stored procedure in your database, you can re-run the generator to update the stored procedure that will be used in your unit tests.

The Unit Testing Package

This unit test package has the ability to insert data defined in an XML file or a JSON file. To use this feature you will need to create a file like the ones in the sample package, and make sure the file is set as embedded. Then you can use the UnitTestHelpers.ReadData() command to read all the xml data into the database. If you specify a primary key data point, then any identity columns will be overridden while inserting this data. If you don’t specify a key in your xml data and the column is setup as an identity column, then a new primary key will be generated. You can leave out optional fields (i.e. nullable fields) from your xml definition and they will be ignored by the insert command in the ReadData() method.

The UnitTestHelpers.CreateConstraint() method allows you to create a constraint that has been defined in your database between two specified tables. This allows you to create only the constraints you’ll need for the tables that you will be running your unit tests against. I did it this way to cut down on the number of constraints to create, otherwise you would have to pre-populate every table that had a constraint on it, instead of a sub-set of tables you wish to tests.

The UnitTestHelpers.ClearConstraints() method will clear all constraints. Make sure you call this at the end of your unit test, otherwise the TruncateData() method used in the cleanup method of your unit tests will fail.

What can you do with this Source Code?

Anything you want. Extract the code you wish to add to your own projects. Download the code and add the features you desire. I hope to some day make this into a real application, but for now, I just want to get this posted so other people can use it. No warranties are implied and you’ll have to use this code at your own risk. If you find any bugs, you can drop a comment here, email me or create an issue on GitHub (you’ll need to sign up for a free GitHub account to post issues).

Where to get the Source Code

I used GitHub to check in my source code. That will allow me to fix any bugs and add any features in the future. You can click here to find the source.

Leave a Reply