Search code examples
vb.netnhibernatefluent-nhibernateconvention

Ignoring properties in Fluent NHibernate with VB.Net (2.0)


Because there is so little information about VB.Net and (Fluent) NHibernate to be found, I decided to write this question to all other developers finding themselves looking for more information.

One of the things I had to struggle with was how to Ignore properties in NHibernate.

The reason I had to ignore properties was because we used a Webserivce which cannot serialize Interface classes (ILists). Which are used a lot with NHibernate.

So we had to ignore some properties from NHibernate and let those properties convert the IList objects to List objects which could be used in Webservice.

We couldn't find any good translation from this C# code to VB.Net:

.Override<Shelf>(map =>  
{  
  map.IgnoreProperty(x => x.YourProperty);
});

OR

.OverrideAll(map =>  
{  
  map.IgnoreProperty("YourProperty");
});

And found a different solution to solve the problem (see self-made answer)


Solution

  • Make a new class which implements DefaultAutomappingConfiguration

    Imports FluentNHibernate.Automapping
    
    Public Class AutomapperConvention
        Inherits DefaultAutomappingConfiguration
    
        Public Overrides Function ShouldMap(ByVal member As FluentNHibernate.Member) As Boolean
            'When the the mapper finds a List object it ignores the mapping you can make your own choices here'
            If (member.PropertyType.IsGenericType AndAlso Not (member.PropertyType.IsInterface)) Then
                Return False
            Else
                Return MyBase.ShouldMap(member)
            End If
        End Function
    
    End Class
    

    Add the AutomapperConvention here:

    'Custom automapping to make the automapper find the correct id's (entityname + "Id")
    Dim mappingConfig As New AutomapperConvention()
    Dim model As New FluentNHibernate.Automapping.AutoPersistenceModel(mappingConfig)