Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

All Blogs

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    Tags: fluentnhibernate

    comments

    I thought it was time to use fluent nhibernate just to keep up. Up untill now I still use the old atributes and nhibernate 1.2.1 because I see no reason to upgrade. It all just works and does what it is supposed to do.

    But I don't want to stand in the way of progress and I want to help the VB.Net community ahead. So that Jeremy can feel a bit better about the VB.Net community ;-).

    First thing I did was checkout the trunk versions of nHibernate and fluentnhibernate

    Then I made a huge database with one table in it. I used SQL server 2005 64 bits (just in case Ted is reading this) for this.

    this is the one table magic.

    1. USE [fluentnhibernate]
    2. GO
    3. /****** Object:  Table [dbo].[Person]    Script Date: 01/12/2009 10:54:14 ******/
    4. SET ANSI_NULLS ON
    5. GO
    6. SET QUOTED_IDENTIFIER ON
    7. GO
    8. CREATE TABLE [dbo].[Person](
    9.     [Id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_Person_Id]  DEFAULT (newid()),
    10.     [LastName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    11.     [FirstName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
    12. ) ON [PRIMARY]

    And I filled up the table with this.

    1. INSERT INTO [fluentnhibernate].[dbo].[Person]
    2.            ([LastName]
    3.            ,[FirstName])
    4.      VALUES
    5.            ('Baes'
    6.            ,'Christiaan')
    7.  
    8. INSERT INTO [fluentnhibernate].[dbo].[Person]
    9.            ([LastName]
    10.            ,[FirstName])
    11.      VALUES
    12.            ('Gobo'
    13.            ,'Denis')

    So the corresponding model class should be simple enough too.

    1. Namespace Model
    2.     Public Class Person
    3.         Private _id As Guid
    4.         Private _lastName As String
    5.         Private _firstName As String
    6.  
    7.         Public Sub New()
    8.             _id = Guid.NewGuid
    9.             _lastName = ""
    10.             _firstName = ""
    11.         End Sub
    12.  
    13.         Public Overridable Property Id() As Guid
    14.             Get
    15.                 Return _id
    16.             End Get
    17.             Set(ByVal value As Guid)
    18.                 _id = value
    19.             End Set
    20.         End Property
    21.  
    22.         Public Overridable Property LastName() As String
    23.             Get
    24.                 Return _lastName
    25.             End Get
    26.             Set(ByVal value As String)
    27.                 _lastName = value
    28.             End Set
    29.         End Property
    30.  
    31.         Public Overridable Property FirstName() As String
    32.             Get
    33.                 Return _firstName
    34.             End Get
    35.             Set(ByVal value As String)
    36.                 _firstName = value
    37.             End Set
    38.         End Property
    39.  
    40.     End Class
    41. End Namespace

    First of all I added the fluentnhibernate project to my new solution, quickly noticing that it was not wise to call my testproject fluentnhibernate and even quicker changing that to fluentnhibernatetest.

    I also added nhibernate to the solution.

    And I added them as a reference to the testproject.

    And then I did this.

    1. Option Infer On
    2.  
    3. Imports FluentNHibernate.AutoMap
    4. Imports fluentnhibernatetest.Model
    5. Imports FluentNHibernate
    6.  
    7. Namespace Dal
    8.     Public Class nHibernateConfiguration
    9.         Public Shared SessionFactory As NHibernate.ISessionFactory
    10.         Public Shared ConnectionString As String = "Server=servername;Database=fluentnhibernate;Trusted_Connection=True;"
    11.  
    12.         Public Shared Function confignhibernate() As NHibernate.ISessionFactory
    13.             Dim properties = New Dictionary(Of String, String)
    14.             properties.Add("connection.connection_string", ConnectionString)
    15.             properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider")
    16.             properties.Add("dialect", "NHibernate.Dialect.MsSql2005Dialect")
    17.             properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver")
    18.             properties.Add("show_sql", "true")
    19.             properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu")
    20.  
    21.             Dim autoMappings = AutoPersistenceModel _
    22.                                     .MapEntitiesFromAssemblyOf(Of Person) _
    23.                                     .Where(Function(t) t.Namespace = "fluentnhibernatetest.Model")
    24.  
    25.             SessionFactory = New NHibernate.Cfg.Configuration() _
    26.                                             .AddProperties(properties) _
    27.                                             .AddAutoMappings(autoMappings) _
    28.                                             .BuildSessionFactory()
    29.             Return SessionFactory
    30.         End Function
    31.     End Class
    32. End Namespace

    which meant I also had to import and reference the NHibernate.ByteCode.Linfu project/assembly. Cant't remember ever needing that.

    And of course I also neede something to run this and see if it worked. Of course I could have created a testproject but what's the fun in that.

    1. Option Infer On
    2.  
    3. Imports fluentnhibernatetest.Model
    4. Imports NHibernate
    5.  
    6. Module Module1
    7.  
    8.     Sub Main()
    9.         Try
    10.             Dim sessionfactory = Dal.nHibernateConfiguration.confignhibernate
    11.             Dim session As ISession = sessionfactory.OpenSession()
    12.             Dim _ReturnList = session.CreateCriteria(GetType(Model.Person)).List(Of Model.Person)()
    13.             For Each Person In _ReturnList
    14.                 Console.WriteLine(Person.Id.ToString & " " & Person.LastName & " " & Person.FirstName)
    15.             Next
    16.         Catch ex As Exception
    17.             Console.WriteLine(ex.Message)
    18.             Console.WriteLine(ex.StackTrace)
    19.         End Try
    20.         Console.ReadLine()
    21.     End Sub
    22.  
    23. End Module

    With this as the result of all our hard labor.

    Not that difficult ;-).

    I will do a more elaborate test later on.

    Once again thanks to the guys that sacrificied their time and energy in making this happen.

    you can also find more examples in C# here by James Gregory.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN