Monday, September 03, 2007
In the beginning of this year (april) I started using nHibernate. Having some experience in Hibernate (Java) I knew what to expect. So without RTFM or not enough anyway I started implementing it. And I used the attribute framework that came with it (version 1.2 beta 3) at the time. The learning curve was a bit high and over the next few posts you will understand why.

First of all I will start with a simple example which works well. In the beginning I couldn't find any good example in VB.Net using the attributes all were in C#.

nHibernate can be downloaded here

So this is what a normal class would look like in the Chrissie1 universe. Nothing special a person with a firstname and lastname. And you will notice that I already added an Id In a perfect world I wouldn't need an Id since firstname and lastname togther would make for a perfect id, but since the perfect world only exists on TV I add it since the beginning. I can always add comparers/constraints later. In the next post I will change it to suit the needs of nHibernate. But for now enjoy it's beauty.
Namespace Model
''' <summary>
''' A normal person
''' </summary>
''' <remarks>For testing purposes</remarks>
Public Class Person
Implements IComparable

#Region " Private members "
''' <summary>
''' A local variable called _id of type String
''' </summary>
''' <remarks>Has Property Id</remarks>
Private _id As Guid
''' <summary>
''' A local variable called _firstName of type String
''' </summary>
''' <remarks>Has Property FirstName</remarks>
Private _firstName As String
''' <summary>
''' A local variable called _lastName of type String
''' </summary>
''' <remarks>Has Property LastName</remarks>
Private _lastName As String
#End Region

#Region " Constructors "
''' <summary>
''' Default empty constructor
''' </summary>
''' <remarks></remarks>
Public Sub New()
Me.New(Guid.NewGuid, "", "")
End Sub

''' <summary>
''' Constructor with all the fields
''' </summary>
''' <param name="Id">DataType = String</param>
''' <param name="FirstName">DataType = String</param>
''' <param name="LastName">DataType = String</param>
''' <remarks></remarks>
Public Sub New(ByVal Id As Guid, ByVal FirstName As String, ByVal LastName As String)
Me.Id = Id
Me.FirstName = FirstName
Me.LastName = LastName
End Sub
#End Region

#Region " Public properties "
''' <summary>
''' This is the Id property.
''' </summary>
''' <value>Guid</value>
''' <returns>Guid</returns>
''' <remarks></remarks>
Public Property Id() As Guid
Get
Return _id
End Get
Set(ByVal Value As Guid)
_id = Value
End Set
End Property

''' <summary>
''' This is the FirstName property.
''' </summary>
''' <value>String</value>
''' <returns>String</returns>
''' <remarks></remarks>
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal Value As String)
_firstName = Value
End Set
End Property

''' <summary>
''' This is the LastName property.
''' </summary>
''' <value>String</value>
''' <returns>String</returns>
''' <remarks></remarks>
Public Property LastName() As String
Get
Return _lastName
End Get
Set(ByVal Value As String)
_lastName = Value
End Set
End Property
#End Region

#Region " Overrides ToString "
''' <summary>
''' Overridden ToString method for this class
''' </summary>
''' <returns>String</returns>
''' <remarks>The String value of this class represented by the ToStrings of all it's private members</remarks>
Public Overrides Function ToString() As String
Dim ReturnString As New System.Text.StringBuilder()
ReturnString.Append("[Id: " & _id.ToString() & "]")
ReturnString.Append(" - [FirstName: " & _firstName.ToString() & "]")
ReturnString.Append(" - [LastName: " & _lastName.ToString() & "]")
Return ReturnString.ToString()
End Function
#End Region

#Region " Overrides Equals "
''' <summary>
''' Overridden Equals method for this class
''' </summary>
''' <returns>Boolean</returns>
''' <remarks>The Boolean value indictaing if this is equal to the other class</remarks>
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If obj.GetType Is Me.GetType Then
Return Me._id.Equals(CType(obj, Person)._id)
Else
Return False
End If
End Function
#End Region

#Region " Overrides CompareTo "
''' <summary>
''' Overridden CompareTo method for this class
''' </summary>
''' <returns>Integer</returns>
''' <remarks></remarks>
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
If obj.GetType Is Me.GetType Then
Return Me._id.CompareTo(CType(obj, Person)._id)
Else
Return -1
End If
End Function
#End Region

End Class
End Namespace

9/3/2007 12:38 PM Romance Daylight Time  #    Disclaimer  |  Comments [0]  |  Trackback
Comments are closed.