Back to blog

Why I Built an NHibernate Code Generator

This was the first product I ever shipped. I built it in two days, and it solved a problem I'd been living with for years.

Here's the problem.

The same three files, every time

Every NHibernate entity needs a C# class with virtual properties, a fluent mapping class, and a repository with basic CRUD. I've written these by hand more times than I can count, across multiple enterprise projects. The structure never changes. Only the names do.

You copy the last entity you made. Rename everything. Adjust properties. Fix the mapping columns. Update the repository generics. Then you do it again for the next entity. It's not hard. It's just tedious enough that you make mistakes — a missing virtual keyword that NHibernate silently ignores until runtime, a typo'd column name that returns empty results, a NotNullable() call you forgot.

At some point you realize: the input is always the same (entity name, properties, types, nullability). The output is always the same (three files). This is a solved pattern pretending to be work.

JSON in, three files out

The tool takes a simple JSON definition:

{
  "entityName": "Customer",
  "tableName": "Customers",
  "namespace": "MyProject.Entities",
  "id": { "name": "Id", "type": "int", "generator": "identity" },
  "properties": [
    { "name": "FirstName", "type": "string", "length": 50, "nullable": false },
    { "name": "Email", "type": "string", "length": 255, "nullable": true }
  ]
}

Run it. Get Customer.cs, CustomerMap.cs, CustomerRepository.cs. All with correct virtual properties, proper fluent mapping syntax, clean repository pattern. Drop them into your project. Done.

Let me be honest about something

You can paste a JSON description into ChatGPT and get NHibernate code for free. I know that. Any code generator that does format-A-to-format-B conversion is competing with AI now.

So why does this tool exist? Two reasons.

First: it runs on your machine, offline, as a self-contained executable. No API calls, no pasting proprietary schemas into a chat window, no internet required. On locked-down enterprise machines where installing runtimes requires IT tickets, "runs without .NET installed" is a real feature.

Second: consistency. When every entity in your project comes from the same tool with the same config format, the code looks the same everywhere. AI gives you a different style every time you ask. This gives you the same style every time you run it.

But honestly? The main reason this tool exists is that it was the first thing I shipped. And shipping it changed everything.

What shipping felt like

I built it in two days. Shipped it 23 days ahead of my own deadline. The code wasn't the hard part — I'd been solving this problem mentally for years. The hard part was deciding that v0.1 doesn't need relationships, XML mappings, or database schema reading. Just JSON in, three files out. Ship it. See what happens.

I was embarrassed by how simple it was. Then I remembered: done is better than perfect. The perfectionism that says "add one more feature before you ship" is the same perfectionism that keeps most side projects from ever launching.

I shipped it anyway. Five more products followed.

Available on Gumroad for EUR 10.