开发者

What would be the equivalent VB.NET code for this C# FluentNHibernate component mapping?

开发者 https://www.devze.com 2022-12-19 09:07 出处:网络
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.

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 ev开发者_Python百科olved 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#.)


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)


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消