Hi Fabio,

Thanks for the Tip.  I've tried to impliment a usertype but I am getting the
*"property mapping has wrong number of columns:
VolunteerImpact.Domain.UserInfo.GenderID type: GenderIdType"*
error that I was having with my other attempt at a user type (that i ended
up using a listener for(Thanks to your advice, it works great!).


It occurs on the config.buildsession

Here is the relevant mapping line:
<property name="GenderId" column="GenderID" access="property"
type="VolunteerImpact.Data.GenderIdType, VolunteerImpact.Data" />

Here is the User Type Code:
Imports NHibernate.UserTypes
Imports NHibernate
Imports VolunteerImpact.Common



Public Class GenderIdType
    Implements IUserType

    Public ReadOnly Property IsMutable() As Boolean Implements
NHibernate.UserTypes.IUserType.IsMutable
        Get
            Return False
        End Get
    End Property

    Public ReadOnly Property ReturnedType() As System.Type Implements
IUserType.ReturnedType
        Get
            Return GetType(GenderIdType)
        End Get
    End Property

    Public ReadOnly Property SqlTypes() As NHibernate.SqlTypes.SqlType()
Implements IUserType.SqlTypes
        Get
            Dim types(1) As NHibernate.SqlTypes.SqlType
            types(0) = New SqlTypes.SqlType(DbType.Int16)
            Return types
            ' Return New () {NHibernateUtil.[String].SqlType}
        End Get
    End Property

    Public Function NullSafeGet(ByVal rs As IDataReader, ByVal names As
String(), ByVal owner As Object) As Object Implements IUserType.NullSafeGet
        Dim obj As Object = NHibernateUtil.[String].NullSafeGet(rs,
names(0))

        If obj Is Nothing Then
            Return Nothing
        End If

        Dim gender As Integer? = DirectCast(obj, Integer?)

        If gender.HasValue = False AndAlso gender <> 1 AndAlso gender <> 2
Then
            Throw New Exception("Invalid GenderID")
        Else
            Select Case gender
                Case 1
                    Return GenderIdEnum.Male
                Case 2
                    Return GenderIdEnum.Female
                Case Else
                    Return GenderIdEnum.NotSpecified

            End Select

        End If
    End Function

    Public Sub NullSafeSet(ByVal cmd As IDbCommand, ByVal value As Object,
ByVal index As Integer) Implements IUserType.NullSafeSet
        Dim gender As GenderIdEnum
        gender = DirectCast(value, GenderIdEnum)

        Select Case gender
            Case GenderIdEnum.NotSpecified
                DirectCast(cmd.Parameters(index), IDataParameter).Value =
DBNull.Value
            Case GenderIdEnum.Male
                DirectCast(cmd.Parameters(index), IDataParameter).Value = 1
            Case GenderIdEnum.Female
                DirectCast(cmd.Parameters(index), IDataParameter).Value = 2
        End Select

    End Sub

    Public Function DeepCopy(ByVal value As Object) As Object Implements
IUserType.DeepCopy
        Return value
    End Function

    Public Function Replace(ByVal original As Object, ByVal target As
Object, ByVal owner As Object) As Object Implements IUserType.Replace
        Return original
    End Function

    Public Function Assemble(ByVal cached As Object, ByVal owner As Object)
As Object Implements IUserType.Assemble
        Return cached
    End Function

    Public Function Disassemble(ByVal value As Object) As Object Implements
IUserType.Disassemble
        Return value
    End Function

    Public Shadows Function Equals(ByVal x As Object, ByVal y As Object) As
Boolean Implements IUserType.Equals
        If ReferenceEquals(x, y) Then
            Return True
        End If

        If x Is Nothing OrElse y Is Nothing Then
            Return False
        End If

        Return x.Equals(y)
    End Function

    Public Shadows Function GetHashCode(ByVal x As Object) As Integer
Implements IUserType.GetHashCode
        Return If(x Is Nothing, GetType(Boolean).GetHashCode() + 473,
x.GetHashCode())
    End Function


End Class



Here is the property in the class:

Private _GenderId As GenderIdEnum
 Public Overridable Property GenderId() As GenderIdEnum
        Get
            Return _GenderId
        End Get
        Set(ByVal value As GenderIdEnum)
            _GenderId = value
        End Set
    End Property


and here is the enum
Public Enum GenderIdEnum
    NotSpecified
    Male = 1
    Female = 2
End Enum


any help( from anyone) in pin pointing where the error is occurring would be
greatly appreciated!

Thanks very Much,
Patricia

On Mon, May 25, 2009 at 4:35 PM, Fabio Maulo <[email protected]> wrote:

> custom implementation of IUserType
>
> 2009/5/25 Patricia Vandermeer <[email protected]>
>
> Hi Everyone,
>>
>> here's the situation.   We've got a bunch of fields that are enums,  that
>> can be null.  For example:
>>
>> a user has a gender that is stored in a nullable column (it's a FK to a
>> Gender Table) and is represented in the application as an enum like so:
>>
>> GenderEnum
>> NotSpecified
>> Male 1
>> Female 2
>>
>> What we would like to do is have it put NULL in the database if it is Not
>> Specified.  and If it's NULL in the database have it set the field to
>> NotSpecified.
>>
>> I tried making the enum nullable and setting notSpecified = nothing
>>
>> but then when i try to use the enum  it doesn't work properly.
>>
>> we are working with a pre-existing database and code base that we are
>> upgrading, and the less code that we have to mangle the better.
>>
>> any suggestions on the best way to implement this scenario?
>>
>> Thanks in Advanced
>>
>> Patricia
>>
>>
>>
>>
>
>
> --
> Fabio Maulo
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to