Generate source code
In this tutorial, we'll use your project to generate source code from it so we can use the mapped elements in our own programs.
Configuring and generating source code
Please follow the following steps to generate sourcecode from the project we've created in the previous tutorial. This tutorial will be for C# and will generate .NET 4.5.2 code. If you're more into VB.NET, you should select VB.NET instead of C#.
- Start the LLBLGen Pro designer. Either use the Programs start menu, Windows search or double click the LLBLGenPro.exe in the installation folder to start the LLBLGen Pro designer.
- To open our tutorial project, select it from the Recent Projects list on the Home Tab, or select File -> Open Project... from the main menu, or select the project from the File -> Recent Projects... menu.
-
To generate code, either press
F7
, or select the menu option Project -> Generate Source-code... The Generate Code dialog opens. - As we have just one model in the project, it will show one task, for the Entity Model. As this is the first time we're generating code, it's not currently valid and we have to edit the task. Do this by double-clicking the task or by clicking the Edit Selected Task Specifics button. The Code Generation Task Configuration dialog opens.
- By default the view is the 'Simple' view, with just 1 tab. On this tab, General Settings, select either C# of VB.NET for Target language and select the .NET version of your choice in the Target Platform drop-down.
- For Root namespace, specify Northwind.Tutorial
- There's one template group for NHibernate v4 so leave it at 'General'.
- There are four presets for NHibernate v4, select the default one, SD.NHibernate (Fluent NHibernate and classes).
- For Destination root folder under Code file parameters you should specify a folder into which you want to generate the code. For this tutorial we'll use c:\temp\NorthwindTutorial\DAL
- Click OK to close the configuration dialog.
- We're now done setting everything up. LLBLGen Pro will store your settings in the project file after code generation so you don't have to set things up the next time you're generating code. Click Perform Tasks... to generate the sourcecode into the Destination root folder.
Compiling the generated cs/vbproj projects
You now have two cs/vbproj projects generated for you, one with the entity classes and one with the SessionManager class and mapping related files, be it either a set of .hbm xml files (if you chose the XML mappings and classes preset) or a set of mapping classes (if you chose the FluentNHibernate and classes preset). To compile and use them in your code, please follow the following steps. If you have used the LLBLGen Pro designer from within Visual Studio, the generated projects have been added to your solution automatically.
- Start your IDE, be it Visual Studio or Jetbrains Rider.
- In your IDE, load the two generated csproj files, the Root namespace.Model.csproj and the Root namespace.Persistence.csproj file, by opening them using File -> Open -> Project / Solution.
- Save the solution created for you by your IDE.
-
You now have to add the NHibernate reference. Depending on what preset you chose, do the following.
- If you've chosen to use .hbm xml files, you have to add a nuget reference to the NHibernate package in the Root namespace.Persistence project.
- If you've chosen to use FluentNHibernate, you have to add a nuget reference to the FluentNHibernate package in the Root namespace.Persistence project.
- Add the package reference by right-clicking the References node under the Root namespace.Persistence project and select Manage Nuget Packages, then click the Browse tab in the Nuget GUI, and type the package name you need to add, be it either NHibernate or FluentNHibernate. Click Install.
- Compile the solution.
Using the generated code
Now that you have compiling source code, the next step is to use it. We'll setup a small console app which will retrieve the number of customers from the Customers table. This tutorial will use FluentNHibernate.
- In Visual Studio, with your solution with the two generated Visual Studio projects, right-click the solution in Solution Explorer and select Add -> New Project.
- Select Visual C# -> Console Application and give it a name. Specify a location and click OK.
- In the Solution Explorer, right-click the project you just created and select Set as Startup project
- Right-click the References node below the new project and select Add Reference
- In the Add Reference dialog, on the left click Projects and check the checkboxes of both projects. Click OK
- Drag the app.config file from the Root namespace.Persistence project to your new console application project to copy it. This file contains the connection string.
- To the new console application, add a reference to the NHibernate v4 package from nuget with the same steps as in the previous section.
- Compile the solution. This should succeed.
- Open the Program.cs class in the console application. We're going to add some code to retrieve the number of customers from the Customers table. Alter the Program class so it looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Northwind.Tutorial;
using Northwind.Tutorial.EntityClasses;
namespace Tester
{
class Program
{
static void Main(string[] args)
{
var session = SessionManager.OpenSession();
var count = session.QueryOver<Customer>().RowCount();
Console.WriteLine("Number of customers: {0}", count);
}
}
}
The Root namespace you've specified when generating code can differ from the code above, in which case you have to correct the last using
statement.
Running the code above should give:
Number of customers: 91 Press any key to continue . . .