NHibernate Advantages
- Powerful mapping facilities.
- Execute multiple queries in one go to the database instead of going to the DB for each query.
- Lazy Loading works for NHibernate and it means that you just fetch your necessary data in memory instead of the entire collection in memory and it reduces the memory overhead.
- Decoupling from the database means you can use various database types instead of just using SQL such as Oracle and so on.
- Writing code for NHibernate makes developers feel better in the aspect of readability and documentation.
- Caching and second level caching.
- Its session factory is a thread-safe object in NHibernate.
NHibernate Disadvantages
- Learning and mastering in NHibernate takes time and it seems cumbersome to be professional.
- NHibernate needs complicated XML configuration per entities.
- It takes a long time when you initiate it for the first time because of the preparation operation in the metadata is heavy.
How NHibernate works
NHibernate provides an ORM as a free XML configuration. It depends on a session factory that creates a session to bind to the database. You need a class to define your specific configuration that introduces a connection string for the specific database. Then whenever you call the session you are able to connect to your db. You can either write a code as traditional SQL query or LINQ query using NHibernate. LINQ as your namespace. The session totally encapsulates the unit of work pattern too. You need to produce XML files to work with NHibenate but I have used Fluent NHibernate to prevent any cumbersome something during implementation.
CodeDOM With Fluent
NHibernate you need to write two classes per entity of your table, one is simple and the other is for mapping and introduces a relation to the outer table. It is a bit difficult if you use many tables in your project to write two classes per entity. Therefore I decided to use the Code Document Object Model,
CodeDOM, as a generation tool for generating these classes and I get table names, field names and their data types for the parent table from the UI and for the children I get its parent table to build the foreign key.
As a simplest explanation about
CodeDOM: You need to generate many classes with the same structures but some parts have a different expression so to save your time and energy it is better to write one class instead of many classes and those classes are a good template for generating all your classes.
The following namespaces enable you to use CodeDOM:
- using System.IO;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using Microsoft.CSharp;
Using
CodeDOM you can either write source code for
ASP.NET, XML web services client proxies and so on or compile. Its structure is a tree graph with codecompileunit as its root. Then you should define your namespace and import them. Determine your class name and fields and properties and add them as members to the class.
CodeDOM Advantages
- CodeDOM lets you create, compile and execute your source code for the application at run time, without writing many lines or when your parameter should be determined at run time.
- CodeDOM uses a single model to generate source code, so any languages that support CodeDOM can work with it.
- It is possible to translate VB to C# or inverse.
CodeDOM Disadvantages
- There are limitations to defining expressions using CodeDOM such that you need to use Snippet classes.
- Snippet classes cannot satisfy any expression so you should not use a heavy function on that.
Using the code step-by-step
You should create a database as "GenerateDB" that is a fundamental requirement for a connection string. I have used SQL Server 2008 but you can use any database because NHibernate is independent of database type.
I have defined the table "DataType" with the two columns, ID and Name, for the data type of entities. You can assign your datat type name to the "Name" field such as int, string and and so on. Be careful that CodeDOM pays attention to your application language data type. For instance I used C# so I used int and string. That is a bit different than the VB language. In VB we use "integer" instead of "int".
- Create MVC Project with Fluent NHibernate + Microsoft.Build for CodeDOM + AngularJS for Frontend
I have used Microsoft Visual Studio 2013 and MVC ASP.Net for this scenario. I have applied Fluent NHibernate instead of NHibernate because its source code generation is easy by CodeDOM. I imported Microsoft.Build library for CodeDOM.
Eventually I used AngularJS for the frontend and UI sections, you can get more information about Angularjs implementation at
http://www.codeproject.com/Articles/869433/AngularJS-MVC-Repository-Dispose and about MVC ASP.NET implementation at
http://technical.cosmicverse.info/article/index/3.
File -> New Project -> ASP.NET MVC 4 Project then select "Internet Application".
Select "Empty".
Install Fluent NHibernate
Go to the project in the Solution Exlorer then right-click on "References" then select "Manage Nuget Packages".
Search "FluentNHibernate" then click on "Install" to add its references.
Look at the References for the project. There are the following three new references:
- FluentNHibernate
- Iesi.Collections
- NHibernate
In order to use CodeDOM you should add "Microsoft.Build" and "Microsoft.Build.Framework".
Install AngularJS
Go to the project in the Solution Explorer and right-click on "References" then select "Manage Nuget Packages".
Search: "AngularJS" then click on "Install".
- Review on Scenario and Testing
In this scenario I have assigned two text boxes just for two fields, you can create more than one depending on your requirements. I will improve this section by creating text boxes at run time in the newer version of this article so please bookmark this article for a newer version.
I create a parent table and child table that have a one-to-many relation to each other. In the following picture the parent table has no parent so the last combo box should be empty.
But the child table has a relationship with the parent by calling its parent table and finally the combo box.
Look at the solution and you will see that new classes have been generated by CodeDOM inside "Entities" and "Mapping". For each entitiy there are two classes, one in the "Entities" folder such as "Parent.cs" and another is in the "Mapping" folder as "ChildMap.cs".
After generation look at your database too. There is no difference, whether there your database is SQL, Oracle or whatever. Your tables have been generated here too by FluentNHibernate.
In a nutshell
You determine your table spesification on the UI and then Angular sends info to the controller and there by using "GenerateTable.cs" and "GenerateMap.cs" with a GenerateDBController/Generate Action all the classes will be created on the "Entities" and "Mapping" folders. These classes, such as "Parent.cs" and "ParentMap.cs" by the FluentNHibernate utilities "NHibernate.cs" will create tables inside the database.
Configuration Fluent NHibernate
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using FluentNHibernate.Cfg;
- using FluentNHibernate.Cfg.Db;
- using NHibernate;
- using NHibernate.Tool.hbm2ddl;
- using MVCNHibernate.Entities;
- using MVCNHibernate.Controllers;
-
- namespace MVCNHibernate
- {
- public class NHibernate
- {
- private static ISessionFactory _sessionFactory;
- private static ISessionFactory SessionFactory
- {
- get {
- if (_sessionFactory == null) InitializeSessionFactory();
- return _sessionFactory;
- }
- }
-
- private static void InitializeSessionFactory()
- {
- _sessionFactory = Fluently.Configure()
- .Database(MsSqlConfiguration.MsSql2008.ConnectionString(@
- "Server=.;initial catalog=GenerateDB;integrated security=True")
- .ShowSql())
- .Mappings(m = > m.FluentMappings.AddFromAssemblyOf())
- .ExposeConfiguration(cfg = > new SchemaUpdate(cfg)
- .Execute(false, true))
- .BuildSessionFactory();
- }
-
- public static ISession OpenSession()
- {
- return SessionFactory.OpenSession();
- }
- }
- }
GenerateTable.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- using System.IO;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using Microsoft.CSharp;
- using System.Reflection;
- using System.Web.Mvc;
- using System.Web;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
-
-
- namespace MVCNHibernate
- {
- public class GenerateTable
- {
- CodeCompileUnit targetUnit;
-
- CodeTypeDeclaration targetClass;
-
- public GenerateTable(string tableName)
- {
- targetUnit = new CodeCompileUnit();
-
-
- CodeNamespace samples = new CodeNamespace("MVCNHibernate.Entities");
-
-
- samples.Imports.Add(new CodeNamespaceImport("System"));
- samples.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
- samples.Imports.Add(new CodeNamespaceImport("System.Linq"));
- samples.Imports.Add(new CodeNamespaceImport("System.Text"));
- samples.Imports.Add(new CodeNamespaceImport("MVCNHibernate.Entities"));
-
- targetClass = new CodeTypeDeclaration(tableName);
- targetClass.IsClass = true;
- targetClass.TypeAttributes = TypeAttributes.Public;
- samples.Types.Add(targetClass);
- targetUnit.Namespaces.Add(samples);
- }
-
- public void AddFields(string fld1, string dt1, string fld2, string dt2)
- {
- CodeMemberField field1 = new CodeMemberField();
- field1.Attributes = MemberAttributes.Private;
- if (dt1 == "int")
- {
- field1.Type = new CodeTypeReference(typeof(System.Int32));
- } else if (dt1 == "string")
- {
- field1.Type = new CodeTypeReference(typeof(System.String));
- }
-
- field1.Name = "_" + fld1;
- targetClass.Members.Add(field1);
-
- CodeMemberProperty property1 = new CodeMemberProperty();
- property1.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld1)));
- property1.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld1), new CodePropertySetValueReferenceExpression()));
- property1.Attributes = MemberAttributes.Public;
- property1.Name = fld1;
- if (dt1 == "int")
- {
- property1.Type = new CodeTypeReference(typeof(System.Int32));
- } else if (dt1 == "string")
- {
- property1.Type = new CodeTypeReference(typeof(System.String));
- }
-
- targetClass.Members.Add(property1);
-
- CodeMemberField field2 = new CodeMemberField();
- field2.Attributes = MemberAttributes.Private;
- if (dt2 == "int")
- {
- field2.Type = new CodeTypeReference(typeof(System.Int32));
- } else if (dt2 == "string")
- {
- field2.Type = new CodeTypeReference(typeof(System.String));
- }
- field2.Name = "_" + fld2;
- targetClass.Members.Add(field2);
-
- CodeMemberProperty property2 = new CodeMemberProperty();
- property2.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld2)));
- property2.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld2), new CodePropertySetValueReferenceExpression()));
- property2.Attributes = MemberAttributes.Public;
- property2.Name = fld2;
- if (dt2 == "int")
- {
- property2.Type = new CodeTypeReference(typeof(System.Int32));
- } else if (dt2 == "string")
- {
- property2.Type = new CodeTypeReference(typeof(System.String));
- }
-
- targetClass.Members.Add(property2);
- }
-
- public void RelationalAddFields(string tableName, string fld1, string dt1, string fld2, string dt2, string parent)
- {
- CodeMemberField field1 = new CodeMemberField();
- field1.Attributes = MemberAttributes.Private;
- if (dt1 == "int")
- {
- field1.Type = new CodeTypeReference(typeof(System.Int32));
- } else if (dt1 == "string")
- {
- field1.Type = new CodeTypeReference(typeof(System.String));
- }
-
- field1.Name = "_" + fld1;
- targetClass.Members.Add(field1);
-
- CodeMemberProperty property1 = new CodeMemberProperty();
- property1.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld1)));
- property1.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld1), new CodePropertySetValueReferenceExpression()));
- property1.Attributes = MemberAttributes.Public;
- property1.Name = fld1;
- if (dt1 == "int")
- {
- property1.Type = new CodeTypeReference(typeof(System.Int32));
- } else if (dt1 == "string")
- {
- property1.Type = new CodeTypeReference(typeof(System.String));
- }
-
- targetClass.Members.Add(property1);
-
- CodeMemberField field2 = new CodeMemberField();
- field2.Attributes = MemberAttributes.Private;
- if (dt2 == "int")
- {
- field2.Type = new CodeTypeReference(typeof(System.Int32));
- } else if (dt2 == "string")
- {
- field2.Type = new CodeTypeReference(typeof(System.String));
- }
- field2.Name = "_" + fld2;
- targetClass.Members.Add(field2);
-
- CodeMemberProperty property2 = new CodeMemberProperty();
- property2.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld2)));
- property2.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + fld2), new CodePropertySetValueReferenceExpression()));
- property2.Attributes = MemberAttributes.Public;
- property2.Name = fld2;
- if (dt2 == "int")
- {
- property2.Type = new CodeTypeReference(typeof(System.Int32));
- } else if (dt2 == "string")
- {
- property2.Type = new CodeTypeReference(typeof(System.String));
- }
-
- targetClass.Members.Add(property2);
-
- CodeMemberField field3 = new CodeMemberField();
- field3.Attributes = MemberAttributes.Private;
-
-
- Type myType = Type.GetType("MVCNHibernate.Entities." + parent);
-
-
- field3.Type = new CodeTypeReference(myType);
-
- field3.Name = "_" + parent + tableName;
- targetClass.Members.Add(field3);
-
- CodeMemberProperty property3 = new CodeMemberProperty();
- property3.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + parent + tableName)));
- property3.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_" + parent + tableName), new CodePropertySetValueReferenceExpression()));
- property3.Attributes = MemberAttributes.Public;
- property3.Name = parent + tableName;
- Type myType2 = Type.GetType("MVCNHibernate.Entities." + parent);
-
- property3.Type = new CodeTypeReference(myType2);
-
- targetClass.Members.Add(property3);
- }
-
- CodeDomProvider provider;
- public void GenerateCSharpCode(string fileName)
- {
-
- provider = CodeDomProvider.CreateProvider("CSharp");
- CodeGeneratorOptions options = new CodeGeneratorOptions();
- options.BracingStyle = "C";
-
- using(StreamWriter sourceWriter = new StreamWriter(fileName))
- {
- provider.GenerateCodeFromCompileUnit(
- targetUnit, sourceWriter, options);
- }
- }
- }
- }
GenerateMap.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- using System.IO;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using Microsoft.CSharp;
- using System.Linq.Expressions;
-
- namespace MVCNHibernate
- {
- public class GenerateMap
- {
- CodeCompileUnit targetUnit;
-
- CodeTypeDeclaration targetClass;
-
- public GenerateMap(string tableName)
- {
- targetUnit = new CodeCompileUnit();
-
-
- CodeNamespace samples = new CodeNamespace("MVCNHibernate.Mapping");
-
-
- samples.Imports.Add(new CodeNamespaceImport("System"));
- samples.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
- samples.Imports.Add(new CodeNamespaceImport("System.Linq"));
- samples.Imports.Add(new CodeNamespaceImport("System.Web"));
- samples.Imports.Add(new CodeNamespaceImport("FluentNHibernate.Mapping"));
- samples.Imports.Add(new CodeNamespaceImport("MVCNHibernate.Entities"));
-
- targetClass = new CodeTypeDeclaration(tableName + "Map");
- targetClass.BaseTypes.Add(new CodeTypeReference
- {
- BaseType = "ClassMap`1[" + tableName + "]", Options = CodeTypeReferenceOptions.GenericTypeParameter
- });
- targetClass.IsClass = true;
- targetClass.TypeAttributes = TypeAttributes.Public;
- samples.Types.Add(targetClass);
- targetUnit.Namespaces.Add(samples);
-
- }
-
- public void AddConstructor(string fld1, string fld2, string tbl)
- {
-
- CodeConstructor constructor = new CodeConstructor();
- constructor.Attributes = MemberAttributes.Public | MemberAttributes.Final;
-
- CodeExpression newType = new CodeExpression();
- CodeSnippetExpression snippet = new CodeSnippetExpression();
-
- string hh = string.Format("Table(\"{0}\"", tbl);
- string lambda = @
- "Id(x => x." + fld1 + "); Map(x => x." + fld2 + ");" + hh + ")";
-
- var lambdaExpression = new CodeSnippetExpression(lambda);
-
- constructor.Statements.Add(lambdaExpression);
-
- targetClass.Members.Add(constructor);
- }
-
-
- public void RelationalAddConstructor(string fld1, string fld2, string tbl, string parent)
- {
- CodeConstructor constructor = new CodeConstructor();
- constructor.Attributes = MemberAttributes.Public | MemberAttributes.Final;
-
- CodeExpression newType = new CodeExpression();
- CodeSnippetExpression snippet = new CodeSnippetExpression();
-
- string parenttbl = parent + tbl;
- string fk = parent + "id";
- string cc = string.Format("\"{0}\"", fk);
- string hh = string.Format("Table(\"{0}\"", tbl);
- string lambda = @
- "Id(x => x." + fld1 + "); Map(x => x." + fld2 + "); References(x => x." + parenttbl + ").Column(" + cc + "); " + hh + ")";
-
- var lambdaExpression = new CodeSnippetExpression(lambda);
-
- constructor.Statements.Add(lambdaExpression);
-
- targetClass.Members.Add(constructor);
- }
-
- public void GenerateCSharpCode(string fileName)
- {
- CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
- CodeGeneratorOptions options = new CodeGeneratorOptions();
- options.BracingStyle = "C";
-
- using(StreamWriter sourceWriter = new StreamWriter(fileName))
- {
- provider.GenerateCodeFromCompileUnit(
- targetUnit, sourceWriter, options);
- }
- }
- }
- }
GenerateDBController.cs