跨 WCF 数据服务的动态 JOIN

发布于 2024-08-20 15:40:37 字数 810 浏览 4 评论 0原文

我正在通过 WCF 数据服务构建通用数据浏览器的原型。

用户可以从 TreeView 中选择实体,因此我无法对查询结果类型进行硬编码,而必须动态编码查询(URI 或 LINQ)。

为了提供跨不同数据服务的联接,我将每个数据服务的结果加载到客户端,并尝试动态联接它们:

Dim q1 As IQueryable = ctx.Execute(Of Object)(New Uri("Service1.svc/Customers")).ToList.AsQueryable

Dim q2 As IQueryable = ctx.Execute(Of Object)(New Uri("Service2.svc/Orders")).ToList.AsQueryable

Dim j = q1.JoinDynamic("q1", q2, "q2", "q1.CustomerID", "q2.CustomerID", "New (q1.CustomerID as q1id, q1.CompanyName as CompanyName)")

我在使用动态联接时遇到了问题。请参阅:链接文本

  1. 当类型直到运行时才知道时,ctx.Execute 是查询结果的正确方法吗?

  2. 有人对如何通过数据服务实现动态连接有更好的想法吗?

I am prototyping a generic data browser over WCF Data Services.

The user can select the entities from a TreeView, thus I cannot hardcode the query result types and have to code the queries (URI or LINQ) dynamically.

To offer joins across different Data Services I am loading the results from each Data Service to the client an try to dynamically join them:

Dim q1 As IQueryable = ctx.Execute(Of Object)(New Uri("Service1.svc/Customers")).ToList.AsQueryable

Dim q2 As IQueryable = ctx.Execute(Of Object)(New Uri("Service2.svc/Orders")).ToList.AsQueryable

Dim j = q1.JoinDynamic("q1", q2, "q2", "q1.CustomerID", "q2.CustomerID", "New (q1.CustomerID as q1id, q1.CompanyName as CompanyName)")

I'm am stuck with a problem using the dynamich Join. See: link text

  1. Is ctx.Execute the right way to query results when the types not known until runtime?

  2. Does someone have a better idea on how to implement dynamic joins over Data Services?

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

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

发布评论

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

评论(1

风筝有风,海豚有海 2024-08-27 15:40:37

作为解决方法,我通过内存中程序集动态创建连接代码。
这似乎工作正常。
我可以在调试器中看到连接结果。
我只是不知道如何将结果绑定到 DataGrid。

        Dim codeDomProvider = New VBCodeProvider
    Dim cp As New Compiler.CompilerParameters
    cp.GenerateExecutable = False
    cp.GenerateInMemory = True
    cp.CompilerOptions = "/optionexplicit- /optionstrict-"
    cp.ReferencedAssemblies.Add(IO.Path.ChangeExtension(My.Application.Info.AssemblyName, "exe"))
    cp.ReferencedAssemblies.Add("System.dll")
    cp.ReferencedAssemblies.Add("System.Core.dll")
    cp.ReferencedAssemblies.Add("System.Data.Services.Client.dll")

    Dim sb = New Text.StringBuilder()
    sb.Append("Imports System" & vbCrLf)
    sb.Append("Imports System.Linq" & vbCrLf)
    sb.Append("Imports " & My.Application.Info.AssemblyName & vbCrLf)
    sb.Append("Public Class JoinHelper" & vbCrLf)
    sb.Append("Public Shared Function GetData() As Object" & vbCrLf)
    sb.Append("  Dim ctx1 As New NorthwindDataService.NorthwindEntities(New Uri(""http://localhost:3631/NorthwindDataService.svc/""))" & vbCrLf)
    sb.Append("  Dim ctx2 As New SalesDataService.SalesEntities(New Uri(""http://localhost:4354/SalesDataService.svc""))" & vbCrLf)
    sb.Append("  Dim q1 as System.Data.Services.Client.QueryOperationResponse (of NorthwindDataService.Customers) = ctx1.Execute(Of NorthwindDataService.Customers)(New Uri(""Customers"", UriKind.Relative))" & vbCrLf)
    sb.Append("  Dim q2 as System.Data.Services.Client.QueryOperationResponse (of SalesDataService.CustomerSize) = ctx2.Execute(Of SalesDataService.CustomerSize)(New Uri(""CustomerSize"", UriKind.Relative))" & vbCrLf)
    sb.Append("  Dim j = From c In q1 Join s In q2 On c.CustomerID Equals s.CustomerID Select New With {c.CompanyName, s.Size}" & vbCrLf)
    'sb.Append("  return j.tostring" & vbCrLf)
    sb.Append("  return j" & vbCrLf)
    'sb.Append("  r = j.ToList" & vbCrLf)
    'sb.Append("  return r" & vbCrLf)
    sb.Append("End Function" & vbCrLf)
    sb.Append("End Class" & vbCrLf)
    sb.Append(vbCrLf)
    Dim code = sb.ToString
    Dim compilerResults = codeDomProvider.CompileAssemblyFromSource(cp, code)
    If compilerResults.Errors.HasErrors Then
        Throw New ApplicationException(compilerResults.Errors.Item(0).ToString)
    End If
    Dim o = compilerResults.CompiledAssembly.CreateInstance("JoinHelper")
    Dim t = o.GetType
    Dim j = t.InvokeMember("GetData", Reflection.BindingFlags.InvokeMethod, Nothing, o, Nothing)
    DataGrid1.ItemsSource = j

As a workaround I dynamically created the join code via an in-memory assembly.
This seems to work fine.
I can see the join result in the debugger.
I only do not know hot to bind the result to a DataGrid.

        Dim codeDomProvider = New VBCodeProvider
    Dim cp As New Compiler.CompilerParameters
    cp.GenerateExecutable = False
    cp.GenerateInMemory = True
    cp.CompilerOptions = "/optionexplicit- /optionstrict-"
    cp.ReferencedAssemblies.Add(IO.Path.ChangeExtension(My.Application.Info.AssemblyName, "exe"))
    cp.ReferencedAssemblies.Add("System.dll")
    cp.ReferencedAssemblies.Add("System.Core.dll")
    cp.ReferencedAssemblies.Add("System.Data.Services.Client.dll")

    Dim sb = New Text.StringBuilder()
    sb.Append("Imports System" & vbCrLf)
    sb.Append("Imports System.Linq" & vbCrLf)
    sb.Append("Imports " & My.Application.Info.AssemblyName & vbCrLf)
    sb.Append("Public Class JoinHelper" & vbCrLf)
    sb.Append("Public Shared Function GetData() As Object" & vbCrLf)
    sb.Append("  Dim ctx1 As New NorthwindDataService.NorthwindEntities(New Uri(""http://localhost:3631/NorthwindDataService.svc/""))" & vbCrLf)
    sb.Append("  Dim ctx2 As New SalesDataService.SalesEntities(New Uri(""http://localhost:4354/SalesDataService.svc""))" & vbCrLf)
    sb.Append("  Dim q1 as System.Data.Services.Client.QueryOperationResponse (of NorthwindDataService.Customers) = ctx1.Execute(Of NorthwindDataService.Customers)(New Uri(""Customers"", UriKind.Relative))" & vbCrLf)
    sb.Append("  Dim q2 as System.Data.Services.Client.QueryOperationResponse (of SalesDataService.CustomerSize) = ctx2.Execute(Of SalesDataService.CustomerSize)(New Uri(""CustomerSize"", UriKind.Relative))" & vbCrLf)
    sb.Append("  Dim j = From c In q1 Join s In q2 On c.CustomerID Equals s.CustomerID Select New With {c.CompanyName, s.Size}" & vbCrLf)
    'sb.Append("  return j.tostring" & vbCrLf)
    sb.Append("  return j" & vbCrLf)
    'sb.Append("  r = j.ToList" & vbCrLf)
    'sb.Append("  return r" & vbCrLf)
    sb.Append("End Function" & vbCrLf)
    sb.Append("End Class" & vbCrLf)
    sb.Append(vbCrLf)
    Dim code = sb.ToString
    Dim compilerResults = codeDomProvider.CompileAssemblyFromSource(cp, code)
    If compilerResults.Errors.HasErrors Then
        Throw New ApplicationException(compilerResults.Errors.Item(0).ToString)
    End If
    Dim o = compilerResults.CompiledAssembly.CreateInstance("JoinHelper")
    Dim t = o.GetType
    Dim j = t.InvokeMember("GetData", Reflection.BindingFlags.InvokeMethod, Nothing, o, Nothing)
    DataGrid1.ItemsSource = j
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文