Friday, September 28, 2007
So know that we got our class up and running (unit test and all) we want to connect it to a database. At first this class will look like a DTO but it could be more. So don't let yourself be fooled.

First of all I needed to change the properties to make them virtual (C#) or better overridable (VB.Net). Why? Because I didn't know better and I did what it told me to do. But now I understand. It needs this for its lazy loading. That way it can make a sub class of your class and it just needs to overload the properties. At the mean while it also overloads the compareto method which was un unwanted side effect for me. It overloads it so that it starts sorting on the Id field.

Now for the mapping.
NHibernate like Hibernate uses mapping files to map the properties to columns in the database. But some nice guy also provided us with attributes to make it eassier to make the mapping files or make no mapping files at all.

So the attributes you see are from that.

I used nHibernate 1.2.1 for this

Enjoy

Imports NHibernate
Imports NHibernate.Mapping.Attributes

Namespace Model
''' <summary>
'''
''' </summary>
''' <remarks></remarks>
<CLSCompliant(True)> _
<NHibernate.Mapping.Attributes.JoinedSubclass(0, table:="tbl_Person", lazy:=False)> _
Public Class Person2
Implements IComparable

#Region " Private members "
''' <summary>
''' A local variable called _id of type Guid
''' </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()
'TODO : Implement default constructor
End Sub

''' <summary>
''' Constructor with all the fields
''' </summary>
''' <param name="Id">DataType = Guid</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>
<NHibernate.Mapping.Attributes.Id(0, typetype:=GetType(Guid), Name:="Id") _
, NHibernate.Mapping.Attributes.Generator(1, Class:="guid.comb")> _
<NHibernate.Mapping.Attributes.Key()> _
Public Overridable 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>
<NHibernate.Mapping.Attributes.Property()> _
Public Overridable 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>
<NHibernate.Mapping.Attributes.Property()> _
Public Overridable 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, Person2)._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, Person2)._id)
Else
Return -1
End If
End Function
#End Region
End Class
End Namespace

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