Must read and references

NHibernate Fluent Criteria

– Updated on the 2. January 2010

I have been using NHibernate for some time now, and I am very satisfied with the functionality it provides. However, I do really not like “magic strings” as these makes the code resistent to refactoring.

NHibernate makes use of “magic strings” when doing a query using HQL or the Criteria API. I tend to use the Criteria API as this, opposed to HQL, limits the logic encompassed in “magic strings”.

As I mostly use the Criteria API, I thought, why not do something about it :)  So I did. Very quickly a Fluent Criteria interface was implemented. Here is how it is used:

var email = "email@email.com";
var customerType = CustomerType.Default;

var result = nhSession.Criteria<Customer>()
             .Add.Equals(x => x.Email, email)
             .Add.Equals(x => x.Type, customerType)
             .Order.Desc(x => x.Created)
             .NHCriteria
                 .SetMaxResults(1)
                 .UniqueResult<Customer>();

In this line we call the extension method Criteria with Customer as a generic argument:

var result = nhSession.Criteria<Customer>()

By doing so we of the alternate Fluent Criteria API I have implemented. Currently it is only some of the Restrictions and Order classes that have been implemented, but you are free to use these classes and extend them as you wish.

By calling the NHCriteria property, you get the normal NHibernate criteria API, so here you can set the cache options, max results, etc.

Download the source code here http://ovesen.codeplex.com/wikipage?title=NHibernateFluentCriteria&referringTitle=Home

Happy New Year btw :)

Using Multiple Configuration files for NHibernate (HybridSessionBuilder)

 

I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions.

This post is also written because of a simple question: Does the HybridSessionBuilder support multiple configurations?

Why this question? Well, I have a project where some repositories uses one database and some other uses another database.

Original implementation

My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:

http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/

But as you can see:image

The original implementation used a static session factory and session, so this is does not support multiple configurations.

CodeCampServer/Tarantino

I then found out that the CodeCampServer also was using the HSB. Hoping to find an implementation that supported my request, I then started to read the source code.

But by viewing the source code for CodeCampServer I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa :)

Furthermore I found out that the HSB now is a part of a collection of libraries called Tarantino.

I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.

Test web application

image Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.

nhibernate.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true

nhibernate2.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true

My windsor.config.xml looks like this:

<component id="firstRepository" type="HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate.cfg.xml</ConfigurationFile>
  </parameters>
</component>
 
<component id="secondRepository" type="HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate2.cfg.xml</ConfigurationFile>
  </parameters>
</component>

It basically just configures the two repositories with each own configuration.

But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:

image

Only the default NHibernate configuration file was used.

Bug or feature?

That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.

I was expecting an object structure like this:

image

But I got something like this:

image  I identified the following method to be the central, when the session was instantiated:

   1: private ISession getExistingOrNewSession(ISessionFactory factory, string configurationFile)
   2:         {
   3:             if (HttpContext.Current != null)
   4:             {
   5:                 var session = GetExistingWebSession();
   6:  
   7:                 if (session == null || !session.IsOpen)
   8:                 {
   9:                     session = openSessionAndAddToContext(factory, configurationFile);
  10:                 }
  11:  
  12:                 return session;
  13:             }
  14:  
  15:             var currentSession = _currentSessions.ContainsKey(configurationFile)
  16:                                     ? _currentSessions[configurationFile]
  17:                                     : null;
  18:             if (currentSession == null || !currentSession.IsOpen)
  19:             {
  20:                 _currentSessions[configurationFile] = OpenSession(factory);
  21:             }
  22:  
  23:             return _currentSessions[configurationFile];
  24:         }

Patch/solution

The problem I identified is line #5. It returns the existing session based on the default configuration, no matter what you specify in the configurationFile parameter.

I changed it to:

   1: var session = GetExistingWebSession(configurationFile);

 

And tadaaa:

image

The second repository now uses the second configuration file :)

Conclusion

You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.

First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.

Second of all, as NHibernate and Castle Windsor is still a bit new to me, one of the best ways to learn its capabilities and how to use it, is to dig in deep. And that is exactly what I have done.

And BTW – I have not just learned about HSB, but also CodeCampServer, Tarantino, S#arp and how it handles multiple configurations, and how it all works together.

NHibernateSessionModule

Please note. Before my change is used, you need to also update the NHibernateSessionModule. It does currently not dispose all the sessions that are opened.

Posted in Development. Tags: , , . No Comments »

Multiple Configurations for NHibernate using HybridSessionBuilder

 

I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions.

This post is also written because of a simple question: Does the HybridSessionBuilder support multiple configurations?

Why this question? Well, I have a project where some repositories uses one database and some other uses another database.

Original implementation

My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:

http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/

But as you can see:image

The original implementation used a static session factory and session, so this is does not support multiple configurations.

CodeCampServer/Tarantino

I then found out that the CodeCampServer also was using the HSB. Hoping to find an implementation that supported my request, I then started to read the source code.

But by viewing the source code for CodeCampServer I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa :)

Furthermore I found out that the HSB now is a part of a collection of libraries called Tarantino.

I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.

Test web application

image Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.

nhibernate.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true

nhibernate2.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true

My windsor.config.xml looks like this:

<component id="firstRepository" type="HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate.cfg.xml</ConfigurationFile>
  </parameters>
</component>
 
<component id="secondRepository" type="HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate2.cfg.xml</ConfigurationFile>
  </parameters>
</component>

 

It basically just configures the two repositories with each own configuration.

But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:

image

Only the default NHibernate configuration file was used.

Bug or feature?

That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.

I was expecting an object structure like this:

image

But I got something like this:

image  I identified the following method to be the central, when the session was instantiated:

   1: private ISession getExistingOrNewSession(ISessionFactory factory, string configurationFile)
   2:         {
   3:             if (HttpContext.Current != null)
   4:             {
   5:                 var session = GetExistingWebSession();
   6: 
   7:                 if (session == null || !session.IsOpen)
   8:                 {
   9:                     session = openSessionAndAddToContext(factory, configurationFile);
  10:                 }
  11: 
  12:                 return session;
  13:             }
  14: 
  15:             var currentSession = _currentSessions.ContainsKey(configurationFile)
  16:                                     ? _currentSessions[configurationFile]
  17:                                     : null;
  18:             if (currentSession == null || !currentSession.IsOpen)
  19:             {
  20:                 _currentSessions[configurationFile] = OpenSession(factory);
  21:             }
  22: 
  23:             return _currentSessions[configurationFile];
  24:         }

Patch/solution

The problem I identified is line #5. It returns the existing session based on the default configuration, no matter what you specify in the configurationFile parameter.

I changed it to:

   1: var session = GetExistingWebSession(configurationFile);

 

And tadaaa:

image

The second repository now uses the second configuration file :)

Conclusion

You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.

First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.

Second of all, as NHibernate and Castle Windsor is still a bit new to me, one of the best ways to learn its capabilities and how to use it, is to dig in deep. And that is exactly what I have done.

And BTW – I have not just learned about HSB, but also CodeCampServer, Tarantino, S#arp and how it handles multiple configurations, and how it all works together.

Posted in Development. Tags: , . No Comments »

Multiple Configurations for NHibernate (HybridSessionBuilder)

 

I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions.

This post is also written because of a simple question: Does the HybridSessionBuilder support multiple configurations?

Why this question? Well, I have a project where some repositories uses one database and some other uses another database.

Original implementation

My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:

http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/

But as you can see:image

The original implementation used a static session factory and session, so this is does not support multiple configurations.

CodeCampServer/Tarantino

I then found out that the CodeCampServer also was using the HSB. Hoping to find an implementation that supported my request, I then started to read the source code.

But by viewing the source code for CodeCampServer I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa :)

Furthermore I found out that the HSB now is a part of a collection of libraries called Tarantino.

I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.

Test web application

image Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.

nhibernate.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true

nhibernate2.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true

My windsor.config.xml looks like this:

<component id="firstRepository" type="HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate.cfg.xml</ConfigurationFile>
  </parameters>
</component>
 
<component id="secondRepository" type="HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate2.cfg.xml</ConfigurationFile>
  </parameters>
</component>

It basically just configures the two repositories with each own configuration.

But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:

image

Only the default NHibernate configuration file was used.

Bug or feature?

That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.

I was expecting an object structure like this:

image

But I got something like this:

image  I identified the following method to be the central, when the session was instantiated:

   1: private ISession getExistingOrNewSession(ISessionFactory factory, string configurationFile)
   2:         {
   3:             if (HttpContext.Current != null)
   4:             {
   5:                 var session = GetExistingWebSession();
   6:  
   7:                 if (session == null || !session.IsOpen)
   8:                 {
   9:                     session = openSessionAndAddToContext(factory, configurationFile);
  10:                 }
  11:  
  12:                 return session;
  13:             }
  14:  
  15:             var currentSession = _currentSessions.ContainsKey(configurationFile)
  16:                                     ? _currentSessions[configurationFile]
  17:                                     : null;
  18:             if (currentSession == null || !currentSession.IsOpen)
  19:             {
  20:                 _currentSessions[configurationFile] = OpenSession(factory);
  21:             }
  22:  
  23:             return _currentSessions[configurationFile];
  24:         }

Patch/solution

The problem I identified is line #5. It returns the existing session based on the default configuration, no matter what you specify in the configurationFile parameter.

I changed it to:

   1: var session = GetExistingWebSession(configurationFile);

 

And tadaaa:

image

The second repository now uses the second configuration file :)

Conclusion

You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.

First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.

Second of all, as NHibernate and Castle Windsor is still a bit new to me, one of the best ways to learn its capabilities and how to use it, is to dig in deep. And that is exactly what I have done.

And BTW – I have not just learned about HSB, but also CodeCampServer, Tarantino, S#arp and how it handles multiple configurations, and how it all works together.

Posted in Development. Tags: , , , . No Comments »

NHibernate – many-to-many relationship with data attached

I just found a good blog post about the may-to-many relationship in NHibernate.

The configuration sis done in Fluent NHibernate.

http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx

Posted in Development. Tags: , , , . No Comments »

Ayende explains cascading with NHibernate

To continue my little "post with good links"… here is a good article by Ayende, explaining cascadin with NHibernate:

http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx

Here is the basics:

Here is what each cascade option means:

  • none – do not do any cascades, let the users handles them by themselves.
  • save-update – when the object is saved/updated, check the assoications and save/update any object that require it (including save/update the assoications in many-to-many scenario).
  • delete – when the object is deleted, delete all the objects in the assoication.
  • delete-orphans – when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.
  • all – when an object is save/update/delete, check the assoications and save/update/delete all the objects found.
  • all-delete-orhpans – when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In additional to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.

 

Posted in Software. Tags: , , , , . No Comments »

Learning NHibernate

NHibernate is a port of Hibernate Core for Java to the .NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading and storing the objects. Optionally, you can describe your mapping metadata with attributes in your source code.

Update 18-11-2008, well it turs out there infect is a NHibernate 2.0documentation here:

http://nhforge.org/doc/nh/en/index.html

 

A very good place to start is here:

http://www.hibernate.org/hib_docs/v3/reference/en-US/html/

Properties

Chapter 5 explains setting up properties in the mapping file, tags and attributes.

http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html

Collections

Chapter 6 is about collections and lists

http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections.html

Many-to-many relations

Handling many-to-many relations can be difficult, especially when it comes to cascading and data integrity. This blog post explains one way of handling it.

http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx

Posted in Development, Software. Tags: , , , . No Comments »