Hya all NHibernator users,
(What is NHibernator anyway?)
NHibernator is now available in SourceForge
http://sourceforge.net/projects/nhibernator
You can get anonymous access to the cvs, and see the code
http://nhibernator.cvs.sourceforge.net/nhibernator/
Showing posts with label NHibernate. Show all posts
Showing posts with label NHibernate. Show all posts
Wednesday, May 02, 2007
Sunday, April 15, 2007
NHibernator - The simple but yet powerful NHibernate helper
Overview
Working with NHibernate requires the use of a framework to configure NHibernate and to manage the session. The NHibernate wiki contains documentation about the recommended way to do it (see http://www.hibernate.org/363.html) especially in an ASP.NET environment.
Frameworks like Spring.Net and Castle provide a full-featured solution for integrating NHibernate in an application, but for some applications it's considered as overkill.
That's why most applications use some kind of homemade NHibernate Helper, based on code examples from the Internet. Some of them can be found at the bottom of http://www.hibernate.org/363.html.
So I was searching for a simple NHibernate Helper that is simple to integrate, but still provides the feature I need, like working with multiple databases, support for ASP.NET and Winform environment, and more...
NHibernator
I created the NHibernator as a solution for a simple NHibernate Helper, I will upload it to SourceForge as an open source so people can contribute. Meanwhile you can download it here.
Main features:
1. Easy configuration at the bootstrap, with a specific configuration file.
2. Support for multiple databases.
3. OpenSessionInViewModule for ASP.NET applications.
4. Thread bounded session management for Winform application.
5. NHibernatorTransaction for easy transaction management, with automatic Rollback and easy snippet for easy coding.
NHibernator supports 3 session management mechanisms.
1. OpenSessionInViewModule: Just define a module in the web.config and the session will be opened when needed and released at the end of each request.
2. Session per Transaction: In a non-web applications session will be opened at the beginning the transaction and closed at the end of it.
3. Manual session management: Session will be opened and closed manually with NHibernator.OpenSession() and NHibernator.CloseSession() respectively.
How to use?
1. Basic Configuration: Just place "hibernate.cfg.xml" file in the executable directory, as an alternative you can use the "NHibernatorConfigFileLocation" key in the appSettings section of the web.config. For example:
<appSettings>
<add key="NHibernatorConfigFileLocation" value="~Config\hibernate.cfg.xml"/>
</appSettings>
This can either be full path or relative path using ~. For multiple databases use semicolon, for example:
<appSettings>
<add key="NHibernatorConfigFileLocation" value="~Config\hibernate1.cfg.xml;~Config\hibernate2.cfg.xml"/>
</appSettings>
2. ASP.NET: For a web application you will probably want to use the OpenSessionInViewModule for handling the session at the http-request level.
This can be done by adding this to your web.config file.
<system.web>
<httpModules>
<add name ="NHibernator" type="NHibernatorFramework.OpenSessionInViewModule, NHibernator"/>
</httpModules>
</system.web>
3. Using NHibernator
public class BaseDAO
{
protected ISession session = null;
public BaseDAO()
{
session = NHibernator.GetSession();
}
}
When using multiple databases, you will have to specify the name of the sessionFactory to use, like this:
NHibernator.GetSession("SomeDB")
respectively with the name defined in the xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="SomeDB">
<property name="hibernate.connection.connection_string">...</property>
</session-factory>
4. NHibernatorTransaction: Using NHibernatorTransaction will provide Auto Rollback on exceptions, and Automatic session-per-transaction management for a non-web application.
For example:
using (NHibernatorTransaction transaction = new NHibernatorTransaction())
{
// some dal code here
transaction.Commit();
}
This can also be done using the "NHibernator Transaction" Snippet, like this:


That's it, Please comment if you have remarks or suggestions.
Working with NHibernate requires the use of a framework to configure NHibernate and to manage the session. The NHibernate wiki contains documentation about the recommended way to do it (see http://www.hibernate.org/363.html) especially in an ASP.NET environment.
Frameworks like Spring.Net and Castle provide a full-featured solution for integrating NHibernate in an application, but for some applications it's considered as overkill.
That's why most applications use some kind of homemade NHibernate Helper, based on code examples from the Internet. Some of them can be found at the bottom of http://www.hibernate.org/363.html.
So I was searching for a simple NHibernate Helper that is simple to integrate, but still provides the feature I need, like working with multiple databases, support for ASP.NET and Winform environment, and more...
NHibernator
I created the NHibernator as a solution for a simple NHibernate Helper, I will upload it to SourceForge as an open source so people can contribute. Meanwhile you can download it here.
Main features:
1. Easy configuration at the bootstrap, with a specific configuration file.
2. Support for multiple databases.
3. OpenSessionInViewModule for ASP.NET applications.
4. Thread bounded session management for Winform application.
5. NHibernatorTransaction for easy transaction management, with automatic Rollback and easy snippet for easy coding.
NHibernator supports 3 session management mechanisms.
1. OpenSessionInViewModule: Just define a module in the web.config and the session will be opened when needed and released at the end of each request.
2. Session per Transaction: In a non-web applications session will be opened at the beginning the transaction and closed at the end of it.
3. Manual session management: Session will be opened and closed manually with NHibernator.OpenSession() and NHibernator.CloseSession() respectively.
How to use?
1. Basic Configuration: Just place "hibernate.cfg.xml" file in the executable directory, as an alternative you can use the "NHibernatorConfigFileLocation" key in the appSettings section of the web.config. For example:
<appSettings>
<add key="NHibernatorConfigFileLocation" value="~Config\hibernate.cfg.xml"/>
</appSettings>
This can either be full path or relative path using ~. For multiple databases use semicolon, for example:
<appSettings>
<add key="NHibernatorConfigFileLocation" value="~Config\hibernate1.cfg.xml;~Config\hibernate2.cfg.xml"/>
</appSettings>
2. ASP.NET: For a web application you will probably want to use the OpenSessionInViewModule for handling the session at the http-request level.
This can be done by adding this to your web.config file.
<system.web>
<httpModules>
<add name ="NHibernator" type="NHibernatorFramework.OpenSessionInViewModule, NHibernator"/>
</httpModules>
</system.web>
3. Using NHibernator
- Add a reference to the NHibernator assembly.
- Add a using to NHibernatorFramework.
- Get the session with NHibernator.GetSession(). When using a Data Access Layer in your application, it's recommended to use a BaseDal like this:
public class BaseDAO
{
protected ISession session = null;
public BaseDAO()
{
session = NHibernator.GetSession();
}
}
When using multiple databases, you will have to specify the name of the sessionFactory to use, like this:
NHibernator.GetSession("SomeDB")
respectively with the name defined in the xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="SomeDB">
<property name="hibernate.connection.connection_string">...</property>
</session-factory>
4. NHibernatorTransaction: Using NHibernatorTransaction will provide Auto Rollback on exceptions, and Automatic session-per-transaction management for a non-web application.
For example:
using (NHibernatorTransaction transaction = new NHibernatorTransaction())
{
// some dal code here
transaction.Commit();
}
This can also be done using the "NHibernator Transaction" Snippet, like this:
That's it, Please comment if you have remarks or suggestions.
Tuesday, January 16, 2007
As long as ODP.NET is under the hood - NHibernate
A lot of developers are embracing ODP.NET as their Oracle ADO.NET provider (this as opposed to the built in Microsoft OracleClient).
ODP.NET is a high performance provider that takes advantage of Oracle database functionality. More than that, ODP.NET is fully documented, and updates regularly.
When working with NHinbernate, we can still make sure that it's using ODP.NET under the hood. This will allow us to utilize ODP.NET features like the Connection Pool, Tracing etc. Plus, since this product is at Oracle's hands, it will ensure us compatibility and optimization with the Oracle database.
In the NHibernate config file, use this driver_class:
Can it be easier than that?
ODP.NET is a high performance provider that takes advantage of Oracle database functionality. More than that, ODP.NET is fully documented, and updates regularly.
When working with NHinbernate, we can still make sure that it's using ODP.NET under the hood. This will allow us to utilize ODP.NET features like the Connection Pool, Tracing etc. Plus, since this product is at Oracle's hands, it will ensure us compatibility and optimization with the Oracle database.
In the NHibernate config file, use this driver_class:
<property name="connection.driver_class">
NHibernate.Driver.OracleDataClientDriver
</property>
Can it be easier than that?
Friday, January 05, 2007
Welcome Oracle & .Net developers
For quite some time I've been planning on creating a Blog to share my thoughts and knowledge with the people. So today I’m posting my first post (I hope the first of many…).
As you can see in the “About Me” section, I’m a .Net consultant working in Oracle (I’m sure this fact puzzles some of you :-) . As you probably know, there are more than a few .Net project that chose to use Oracle as their database. That’s the reason Oracle is committed to provide the best connectivity and functionality between these two platforms, and that is why I’m here.
In this Blog you will find posts on best practices with Oracle .Net related products like “Oracle Data Provider for .Net” (ODP.NET), "Oracle Developer Tools for Visual Studio .Net" (ODT.NET), "Oracle Database Extensions for .Net". I will also submit information and code, on database connectivity from other products like the ASP.NET 2.0 Providers, log4net.
In addition, I will try to demonstrate open source solutions that help developers build a good enterprise application, like NHibernate, Spring.Net, etc.
So… that’s it for now. I will appreciate any feedbacks on stuff related to .Net and Oracle that you would like to see in this Blog.
Stay tuned.
As you can see in the “About Me” section, I’m a .Net consultant working in Oracle (I’m sure this fact puzzles some of you :-) . As you probably know, there are more than a few .Net project that chose to use Oracle as their database. That’s the reason Oracle is committed to provide the best connectivity and functionality between these two platforms, and that is why I’m here.
In this Blog you will find posts on best practices with Oracle .Net related products like “Oracle Data Provider for .Net” (ODP.NET), "Oracle Developer Tools for Visual Studio .Net" (ODT.NET), "Oracle Database Extensions for .Net". I will also submit information and code, on database connectivity from other products like the ASP.NET 2.0 Providers, log4net.
In addition, I will try to demonstrate open source solutions that help developers build a good enterprise application, like NHibernate, Spring.Net, etc.
So… that’s it for now. I will appreciate any feedbacks on stuff related to .Net and Oracle that you would like to see in this Blog.
Stay tuned.
Labels:
.Net,
NHibernate,
ODP.NET,
ODT.NET,
Oracle,
Spring.NET
Subscribe to:
Posts (Atom)

