views:

525

answers:

1

I think I might have found a bug, but I'm not really sure. It could be a syntax error on my part, but the compiler isn't catching. Anyway, here is what I'm trying to do. Basically I've written my own repository class that essentially just wraps the Fluent Repository class. So here is the relevant code:

Public Class GenericRepository(Of T As IHasIntId) 
    Private _fluentRepos As FluentNHibernate.Framework.IRepository 
    Public Sub New(ByVal FluentRepository As 
FluentNHibernate.Framework.IRepository) 
        _fluentRepos = FluentRepository 
    End Sub 
    Private Sub New() 
    End Sub 
    Public Function GetById(ByVal Id As Integer) As T 
        Return Query(Function(x As T) (x.Id = Id)).FirstOrDefault 
    End Function 
    Public Function Query(ByVal w As Expression(Of System.Func(Of T, 
Boolean))) As IList(Of T) 
        Return _fluentRepos.Query(Of T)(w).ToList() 
    End Function 
End Class

Then I wrote two unit tests, one that would pass in an InMemoryRepository and one that would use an actual NHibernate session to hit the real database. here they are:

 <TestMethod()> Public Sub InMemoryTest() 
        Dim inmemRepos As New InMemoryRepository() 
        Dim p As New Product() 
        Dim id As Integer = 5 
        p.Id = id 
        p.Title = "my product" 
        inmemRepos.Save(p) 
        Dim genRepos As New GenericRepository(Of Product)(inmemRepos) 
        Dim foundP = genRepos.GetById(id) 
        Assert.AreEqual(p.Title, foundP.Title) 
    End Sub 

   <TestMethod()> Public Sub DatabaseTest() 
        Dim session = NHibernateSessionManager.Instance.GetSession() 
        Dim flRepos As New Repository(session) 
        Dim genRepos As New GenericRepository(Of Product)(flRepos) 
        Dim id As Integer = 1 
        Dim p = genRepos.GetById(id) 
        Assert.IsNotNull(p) 
        Assert.AreEqual(id, p.Id) 
    End Sub

The InMemoryTest passed, and the DatabaseTest failed. The exception from the DatabaseTest was a type conversion, from int to product (or maybe the other way around.) I was able to "fix" it though. In the Fluent NHibernate code I changed the Query method on the Repository class from:

return _session.Linq<T>().Where(where).ToArray();

to

return _session.Linq<T>().Where(where.Compile()).ToArray();

Now both tests pass. All of the unit tests in the Fluent NHibernate project pass either way.

+2  A: 

The answer that you received on the Fluent NHibernate mailing list is most likely the correct one. That is, that it's a bug in Linq to NHibernate rather than Fluent NHibernate, which is caused by the VB compiler producing different expression-trees to C#.

James Gregory

related questions