Bugs in VB version of EntityMapperTranslator and EntityTranslatorService

Topics: CAB & Smart Client Software Factory
Jun 3, 2007 at 9:17 AM
Edited Jun 3, 2007 at 9:22 AM
Out of the box, the recipe generated code does not find a valid translator due to a bug. As an example, the code uses the test
TypeOf targetType Is TBusinessEntity
The proper test is
targetType Is GetType(TBusinessEntity)

The full correction is as follows:
    Public MustInherit Class EntityMapperTranslator(Of TBusinessEntity, TServiceEntity)
        Inherits BaseTranslator
 
 
        Public Overloads Overrides Function CanTranslate(ByVal targetType As System.Type, ByVal sourceType As System.Type) As Boolean
			Return (targetType Is GetType(TBusinessEntity) AndAlso sourceType Is GetType(TServiceEntity)) OrElse _
			(targetType Is GetType(TServiceEntity) AndAlso sourceType Is GetType(TBusinessEntity))
		End Function
 
        Public Overloads Overrides Function Translate(ByVal service As [Interface].Services.IEntityTranslatorService, ByVal targetType As System.Type, ByVal source As Object) As Object
			If targetType Is GetType(TBusinessEntity) Then
				Return ServiceToBusiness(service, CType(source, TServiceEntity))
			End If
 
			If targetType Is GetType(TServiceEntity) Then
				Return BusinessToService(service, CType(source, TBusinessEntity))
			End If
 
            Throw New EntityTranslatorException()
        End Function
 
        Protected MustOverride Function BusinessToService(ByVal service As IEntityTranslatorService, ByVal value As TBusinessEntity) As TServiceEntity
 
        Protected MustOverride Function ServiceToBusiness(ByVal service As IEntityTranslatorService, ByVal value As TServiceEntity) As TBusinessEntity
 
    End Class
The second bug is in the testing of arrays. It is a simple case of missing a "Not" in the following method in the EntityTranslatorService
        Public Function CanTranslate(ByVal targetType As System.Type, ByVal sourceType As System.Type) As Boolean Implements [Interface].Services.IEntityTranslatorService.CanTranslate
            If targetType Is Nothing Then
                Throw New ArgumentNullException("targetType")
            End If
            If sourceType Is Nothing Then
                Throw New ArgumentNullException("sourceType")
            End If
 
			Return IsArrayConversionPossible(targetType, sourceType) OrElse FindTranslator(targetType, sourceType) IsNot Nothing
        End Function