Index
This is going to be another lengthy post. But you can skip all the code bits and go to the end. Or wait a bit and read the conclusion in the last post.
First, nHibernate needs some sort of configuration
```vbnet Imports NHibernate.Mapping.Attributes Imports NHibernate Imports NHibernate.Cfg Imports StructureMap
Namespace DAL.CRUD.Hibernate “’ <summary> “’ “’ </summary> “’ <remarks></remarks> <CLSCompliant(True)> _ <Pluggable(“Live”)> _ Public Class NHibernateConfiguration
#Region “ Private members “
''' <summary>
''' Holds the Configuration so that the subclasses can use it.
''' </summary>
''' <remarks>Of type Nhibernate.Cfg.Configuration.</remarks>
Private _Configuration As Configuration
''' <summary>
''' Holds the sessionfactory so that the sub classes can use it.
''' </summary>
''' <remarks>is an interface of type Nhibernate.ISessionFactory</remarks>
Private _SessionFactory As ISessionFactory
''' <summary>
''' Holds the Databaseserver so we can easily switch between Test and productionservers.
''' </summary>
''' <remarks>is of type string and will be used in the connectionstring
''' </remarks>
Private _ServerName As String
''' <summary>
''' Holds the Database so we can easily switch between Test and productiondatabases.
''' </summary>
''' <remarks></remarks>
Private _Database As String
''' <summary>
'''
''' </summary>
''' <remarks></remarks>
Private _SessionFactoryString As String
#End Region
#Region “ Constructors “
''' <summary>
'''
''' </summary>
''' <remarks></remarks>
Public Sub New()
MakeConfiguration()
End Sub
#End Region
#Region “ Public properties “
''' <summary>
'''
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property Configuration() As Configuration
Get
If _Configuration Is Nothing Then
MakeConfiguration()
End If
Return _Configuration
End Get
End Property
''' <summary>
'''
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property SessionFactory() As ISessionFactory
Get
If _Configuration Is Nothing Then
MakeConfiguration()
End If
If _SessionFactory Is Nothing Then
MakeSessionFactory()
End If
Return _SessionFactory
End Get
End Property
''' <summary>
'''
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property SessionFactoryString() As String
Get
Return _SessionFactoryString
End Get
End Property
''' <summary>
'''
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property Database() As String
Get
Return _Database
End Get
End Property
''' <summary>
'''
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property ServerName() As String
Get
Return _ServerName
End Get
End Property
#End Region
#Region “ Private methods “
''' <summary>
''' Makes the configuration and sets the property
''' </summary>
''' <remarks></remarks>
Private Sub MakeConfiguration()
Dim _DBObjectSettings As New DBObjectSettings()
_ServerName = "Server"
_Database = "Database"
_Configuration = New Configuration()
_Configuration.SetProperty(Environment.ConnectionProvider, _DBObjectSettings.ConnectionProvider)
_Configuration.SetProperty(Environment.Dialect, _DBObjectSettings.Dialect)
_Configuration.SetProperty(Environment.ConnectionDriver, _DBObjectSettings.ConnectionDriver)
_Configuration.SetProperty(Environment.ConnectionString, String.Format(_DBObjectSettings.ConnectionString, _ServerName, _Database))
End Sub
''' <summary>
''' Makes the sessionfactory
''' </summary>
''' <remarks></remarks>
Private Sub MakeSessionFactory()
Try
Dim _DBObjectSettings As New DBObjectSettings()
HbmSerializer.Default.HbmDefaultAccess = _DBObjectSettings.HbmDefaultAccess
HbmSerializer.Default.Validate = True
_Configuration.AddInputStream(HbmSerializer.Default.Serialize(System.Reflection.Assembly.GetExecutingAssembly()))
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Try
_SessionFactory = _Configuration.BuildSessionFactory()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
#End Region
End Class
End Namespace``` Then it needs some additional tests.
```vbnet <Test()> _ Public Sub Select_MSP_SQLClientnhibernate_1000000() Dim _crud As DAL.Interfaces.IMSPCoordinate = StructureMap.ObjectFactory.GetNamedInstance(Of DAL.Interfaces.IMSPCoordinate)(“nhibernate”) Assert.IsNotNull(_crud.Selectall) Assert.Greater(_crud.Selectall.Count, 0) Assert.AreEqual(1000000, _crud.Selectall.Count) End Sub
<Test()> _
Public Sub Select_MSP_SQLClientnhibernate_1000()
Dim _crud As DAL.Interfaces.IMSPCoordinate = StructureMap.ObjectFactory.GetNamedInstance(Of DAL.Interfaces.IMSPCoordinate)("nhibernate")
Assert.IsNotNull(_crud.Selectall(1000))
Assert.Greater(_crud.Selectall(1000).Count, 0)
Assert.AreEqual(1000, _crud.Selectall(1000).Count)
End Sub
<Test()> _
Public Sub Select_MSP_SQLClientnhibernate_1000_1000()
Dim _crud As DAL.Interfaces.IMSPCoordinate = StructureMap.ObjectFactory.GetNamedInstance(Of DAL.Interfaces.IMSPCoordinate)("nhibernate")
For i As Integer = 0 To 999
Assert.IsNotNull(_crud.Selectall(1000))
Assert.Greater(_crud.Selectall(1000).Count, 0)
Assert.AreEqual(1000, _crud.Selectall(1000).Count)
Next
End Sub```
Then it needs some implementation.
```vbnet Imports NHibernate Imports StructureMap
Namespace DAL.CRUD.Hibernate <Pluggable(“nhibernate”)> _ Public Class MSPCoordinate Implements Interfaces.IMSPCoordinate
''' <summary>
''' Holds the sessionfactory so that the sub classes can use it.
''' </summary>
''' <remarks>is an interface of type Nhibernate.ISessionFactory</remarks>
Protected SessionFactory As ISessionFactory
Public Sub New()
SessionFactory = New NHibernateConfiguration().SessionFactory
End Sub
Public Function Selectall() As System.Collections.Generic.IList(Of Model.MspCoordinate) Implements Interfaces.IMSPCoordinate.Selectall
Dim _Session As ISession = Nothing
Dim _ReturnList As IList(Of Model.MspCoordinate) = Nothing
_Session = SessionFactory.OpenSession
_ReturnList = _Session.CreateQuery("FROM MspCoordinate").SetMaxResults(1000000).List(Of Model.MspCoordinate)()
If _Session IsNot Nothing Then
_Session.Close()
_Session.Dispose()
End If
Return _ReturnList
End Function
Public Function Selectall1(ByVal top As Integer) As System.Collections.Generic.IList(Of Model.MspCoordinate) Implements Interfaces.IMSPCoordinate.Selectall
Dim _Session As ISession = Nothing
Dim _ReturnList As IList(Of Model.MspCoordinate) = Nothing
_Session = SessionFactory.OpenSession
Dim q As IQuery
q = _Session.CreateQuery("FROM MspCoordinate")
q.SetMaxResults(top)
_ReturnList = q.List(Of Model.MspCoordinate)()
If _Session IsNot Nothing Then
_Session.Close()
_Session.Dispose()
End If
Return _ReturnList
End Function
End Class
End Namespace``` And then the times.
Test | Time |
---|---|
Select_MSP_SQLClientnhibernate_1000000 | 2:08.34 m |
Select_MSP_SQLClientnhibernate_1000 | 0:00.09 m |
Select_MSP_SQLClientnhibernate_1000_1000 | 1:35.81 m |
I also tried this with a transaction, but as can be expected with a select of this type, it didn’t help much.
Test | Time |
---|---|
Select_MSP_SQLClientnhibernatetrans_1000000 | 2:48.07 m |
Select_MSP_SQLClientnhibernatetrans_1000 | 0:00.15 m |
Select_MSP_SQLClientnhibernatetrans_1000_1000 | 2:14.03 m |
And the last thing I tried was with createcriteria instead of createquery.
Test | Time |
---|---|
Select_MSP_SQLClientnhibernatecrit_1000000 | 2:08.21 m |
Select_MSP_SQLClientnhibernatecrit_1000 | 0:00.25 m |
Select_MSP_SQLClientnhibernatecrit_1000_1000 | 1:36.35 m |
Need help with VB.Net? Come and ask a question in our VB.Net Forum