views:

223

answers:

3

I'm a C# programmer constrained to write VB.NET code.

While exploring NHibernate further for my current client, I encountered FluentNHibernate, which I find real attractive.

But now, I wonder how to "translate" this C# code for component mapping into VB.NET code:

Component(x => x.Address, m =>
{
    m.Map(x => x.Number);
    m.Map(x => x.Street);
    m.Map(x => x.PostCode);
});

I know from here:

Component(Of Client)(Function(c) c.Address, ...)

what I miss is how to continue with the brackets in VB.NET, since there's no Begin End keywords or so.

EDIT 1: Following Mr. JaredPar instructions, I figured that his solution might work. If we take the time to read his answer, we may notice that we both don't know what the MType is within his solution. I might have found out that the MType is:

FluentNHibernate.Mapping.ComponentPart(Of TComponent)

Thus, TComponent is, from my understanding, an anonymous type that I shall parameter to use. From this point of view, since I wish to map the properties of my Address object, replacing TComponent in my help method signature seems not to work.

Private Sub MapAdresseHelper(Of Adresse)(ByVal a As FluentNHibernate.Mapping.ComponentPart(Of Adresse))
    a.Map(Function(m) m.Number)
    a.Map(Function(m) m.Street).Length(50)
    a.Map(Function(m) m.PostCode).Length(10)
End Sub

The error I get is that my Address class doesn't have a property member named Street, for instance. It sees my Address type, it recognizes it, but it seems buggy somehow. I guess VBNET is poorly designed for lambda expressions and is less evolved than C# (Sorry, a bit of frustration due to the constraint of working with it and not being capable of doing things VERY easily done in C#.)

Thanks!

+1  A: 

You are not able to do multiline lambda expressions in VB.Net. VB.Net 2010 will fix this I believe. Can you not just create a dll in C# and then call it from VB.Net?

Wade73
I can't tell. Up to now, my client didn't even know that we could "push" VBNET that far in comparison to C#, though politically speaking he's stuck with VBNET. I think this is something that will be unacceptable for the division where my services are required. I guess I only will have to figure another way out. Otherwise, do you think this would work if I multiply the root Component call by the amount of properties I have within my component?
Will Marcouiller
+2  A: 

In Visual Basic 2010 you can write the following

Component(Function(x) x.Address, Sub(m)
  m.Map(Function(x) x.Number)
  m.Map(Function(x) x.Street)
  m.Map(Function(x) x.PostCode)
  End Sub)

EDIT

Here is a VS2008 style solution. I'm not terrible familiar wit FluentNHibernate so I don't know what the type of M is but you should be able to replace MType with it's type and have the code work just fine.

Private Sub Helper(ByVal m As MType)
  m.Map(Function(x) x.Number)
  m.Map(Function(x) x.Street)
  m.Map(Function(x) x.PostCode)
End Sub

...  
Component(Function(x) x.Address, AddressOf Helper)
JaredPar
Thanks for the information! I will then be glad to work with it if someday my client migrates to VBNET2010.
Will Marcouiller
I'm unfortunately using VS2008 as for now. Do you think there's another workaround? Will multiply the root Component() line by the number of properties of my Address class work for a viable workaround solution?
Will Marcouiller
@Will, updated my answer to include a 2008 work around
JaredPar
@JaredPar: Thanks for the update! I shall look at it as I'm writing!
Will Marcouiller
@JaredPar: I have tried to find out what MType is. I found, but I can't get to understand how I can parameter the type to accept either my method signature or my class object properties. I shall edit my question to show a sample of what I mean. Thanks!
Will Marcouiller
A: 

Will, I share your frustration with the VB.NET implementation of lambdas as of the compiler for .NET 3.5/VS 2008. It really isn't so much that the language is poorly designed for lambdas, it is just that the implementation was incomplete with .NET 3.5. Lambda support involves a lot of compiler trickery that couldn't be completed in the timeframe required for the 2008 release. I want to point out that you can continue to target your current .NET framework version using VS 2010 and get the completed support for lambdas provided by the VS 2008 compiler. This means you can use multiline lambdas and anonymous Sub(to complete the previous existing anonymous Function), allowing Action <T> to work properly. You also can avoid the use of the underscore character for muitiline code statements. This is extremely convenient when working with lambdas directly, fluent APIs, or LINQ. Hopefully, this can help you make a strong argument to your client to upgrade your compiler version without introducing the risk of significant change. Aside from the very significant lack of support for iterator blocks, the VB.NET implementation in VS 2010 is pretty sweet as is the WPF-based Visual Studio itself! John Wigger

John Wigger