2010-02-06 34 views
1

我正在通過WCF數據服務原型化通用數據瀏覽器。跨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)") 

我使用dynamich加入我堅持的一個問題。請參閱:link text

  1. 是ctx.Execute查詢結果時,該類型不知道,直到運行時的正確方法?

  2. 是否有人對如何實現動態連接在數據服務更好的主意嗎?

回答

0

作爲解決方法,我通過內存中的程序集動態創建了聯接代碼。 這似乎工作正常。 我可以在調試器中看到連接結果。 我只是不知道熱將結果綁定到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