此 C# FluentNHibernate 组件映射的等效 VB.NET 代码是什么?

发布于 2024-08-20 03:06:10 字数 1245 浏览 1 评论 0原文

我是一名 C# 程序员,只能编写 VB.NET 代码。

在为我当前的客户进一步探索 NHibernate 时,我遇到了 FluentNHibernate,我发现它非常有吸引力。

但现在,我想知道如何将组件映射的 C# 代码“翻译”为 VB.NET 代码:

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

我从这里知道:

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

我想念的是如何继续使用 VB.NET 中的括号,因为没有 Begin End 关键字等。

编辑 1:按照 JaredPar 先生的指示,我认为他的解决方案可能有效。如果我们花时间阅读他的答案,我们可能会注意到我们都不知道他的解决方案中的 MType 是什么。我可能发现 MType 是:

FluentNHibernate.Mapping.ComponentPart(Of TComponent)

因此,根据我的理解,TComponent 是一个匿名类型,我将使用它的参数。从这个角度来看,由于我希望映射我的 Address 对象的属性,因此在我的帮助方法签名中替换 TComponent 似乎不起作用。

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

例如,我得到的错误是我的 Address 类没有名为 Street 的属性成员。它看到我的地址类型,它识别它,但不知何故它似乎有问题。我猜想 VBNET 对于 lambda 表达式的设计很差,并且比 C# 的发展程度要低(抱歉,由于使用它的限制并且无法完成在 C# 中很容易完成的事情,所以有点沮丧。)

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#.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你又不是我 2024-08-27 03:06:10

在 Visual Basic 2010 中,您可以编写以下

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)

编辑

这里是 VS2008 风格的解决方案。我对 FluentNHibernate 不太熟悉,所以我不知道 M 的类型是什么,但您应该能够用它的类型替换 MType 并且让代码正常工作。

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)

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)
荒人说梦 2024-08-27 03:06:10

您无法在 VB.Net 中执行多行 lambda 表达式。我相信 VB.Net 2010 将解决这个问题。不能直接用 C# 创建一个 dll,然后从 VB.Net 调用它吗?

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?

等风来 2024-08-27 03:06:10

将要,
我与您一样对 .NET 3.5/VS 2008 编译器中 VB.NET 的 lambda 实现感到沮丧。实际上,并不是该语言针对 lambda 的设计很糟糕,而是 .NET 的实现不完整。 NET 3.5。 Lambda 支持涉及许多编译器技巧,无法在 2008 年版本所需的时间范围内完成。我想指出的是,您可以继续使用 VS 2010 以当前的 .NET 框架版本为目标,并获得 VS 2008 编译器提供的对 lambda 的完整支持。这意味着您可以使用多行 lambda 表达式和匿名 Sub(以完成之前现有的匿名函数),从而使 Action 正常工作。您还可以避免在多行代码语句中使用下划线字符。当直接使用 lambda、Fluent API 或 LINQ 时,这非常方便。希望这可以帮助您向客户提出强有力的论据,以升级编译器版本,而不会带来重大更改的风险。除了严重缺乏对迭代器块的支持之外,VS 2010 中的 VB.NET 实现与基于 WPF 的 Visual Studio 本身一样非常出色!
约翰·维格

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文