Introduction
NHibernate is a popular open-source .Net ORM. It is based on the popular Java ORM framework and built on top of ADO.NET. Its current version is 4.0.3 and can be installed using the Nuget manager within Visual Studio. It can be used with many databases like SQL Server, Oracle, DB2, Firebird, Informix, Ingres, MySQL, PostgreSQL, SQLite, SQL Server CE and many other.
Let me provide a summary of this tutorial. It contains:
- Creating the simple WebAPI project.
- Installing of the NHibernate using the Package Manager console.
- Defining a simple business object.
- Creating a NHibernate Mapper class to load and save the business object.
- Configure the NHibernate to link with the database.
- Writing simple CURD operations.
Create WebAPI Project
Create a simple web API project named NHibernate Example.
Create a Database
Create a database named NHibernateExample and add a table with the name “User”.
Create a Business Object
Create a class user as in the following:
- public class User
- {
- public virtual int Id { get; set; }
- public virtual String FirstName { get; set; }
- public virtual String LastName { get; set; }
-
- }
Create a NHibernate Mapper class to load and save the business object as in the following:
- public class UserMap: ClassMapping < User > {
-
- public UserMap() {
- Schema("[dbo]");
- Table("[User]");
- Id(i = > i.Id, map = > map.Generator(Generators.GuidComb));
- Property(x = > x.FirstName, m = > {
- m.NotNullable(true);
- });
- Property(x = > x.LastName, m = > {
- m.NotNullable(true);
- });
-
- }
-
- }
Configure the NHibernate to link with Database
Create a class with the name ConfigurationManager.
- public static class ConfigurationManager {
-
- public static Configuration SetConfiguration() {
- var mapper = new ModelMapper();
- var config = new Configuration();
- mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
- config.DataBaseIntegration(x = > {
- x.ConnectionString = "Data Source=U6031005-TPL-A;Initial Catalog=NHibernateExample;Integrated Security=True";
- x.Driver < SqlClientDriver > ();
- x.Dialect < MsSql2008Dialect > ();
- });
- config.AddAssembly(Assembly.GetExecutingAssembly());
- config.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
- return config;
- }
- }
Writing simple CURD operations
Create a DataAccess class DALUser.
- public class DalUser: IDALUser {
- private ISessionFactory sessionFactory;
-
- public DalUser() {
- var config = ConfigurationManager.SetConfiguration();
- sessionFactory = config.BuildSessionFactory();
- }
-
- public List < User > UserList() {
- var userList = new List < User > ();
- using(var session = sessionFactory.OpenSession()) {
- using(var tx = session.BeginTransaction()) {
- userList = session.Query < User > ().ToList();
- tx.Commit();
- }
- }
-
- return userList;
- }
-
- }
Create a Business class
- public class BLUser: IBLUser {
- public List < User > UserList() {
- IDALUser dalUser = new DalUser();
- return dalUser.UserList();
- }
- }
Create a Controller class
- [RoutePrefix("User")]
- public class UserController: ApiController {
- [Route("UserList")]
- public IHttpActionResult getUserList() {
- IBLUser blUser = new BLUser();
- var userlist = blUser.UserList();
- return Ok(userlist);
- }
- }
Output
- http:
-
-
- <ArrayOfUser
- xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://schemas.datacontract.org/2004/07/TestNHibernate.Models">
- <User>
- <FirstName>Rakesh Kumar</FirstName>
- <Id>1</Id>
- <LastName>Kotha</LastName>
- </User>
- </ArrayOfUser>