2014-01-17 69 views
1

我們有一個WCF應用服務層,它充當我們在數據庫中定義的域模型的DataService。我們正在編寫新的REST Web服務,該服務將在現有的應用程序服務之上運行。計劃使用MVC Web API。MVC - 實現無EF/DB但應用服務作爲數據源的OData

我們希望將我們的RESTFul API公開爲OData端點。但看起來OData與EnityFramework Data Model和Context緊密結合。

問題是我們無法使用EF dbcontext,因爲我們需要通過從後端應用程序服務請求它們然後映射到數據模型來創建對象。

有沒有一種方法來實現沒有數據庫但應用服務作爲數據源的OData。

感謝, 中號

+0

MVC和WebAPI是兩個獨立的東西 – LostInComputer

回答

0

展望雖然MSDN article introducing OData,似乎同時在Visual Studio中的腳手架是針對使用OData的與EF,你還可以直接從EntitySetController派生創建控制器:

public class PeopleController : EntitySetController<Person, int> 
{ 
    public override IQueryable<Person> Get() 
    { 
     // return your own non-EF data source 
    } 
} 

只要你可以得到一個IQueryable,你應該能夠打開一個OData控制器。

0

你可以!您唯一需要注意的問題是get

有兩種方式:

  1. 使用ODataQueryOptions。它包含諸如跳躍,排序依據等

樣品

public class TestController : ODataController 
{ 
    public IEnumerable<TestModel> Get(ODataQueryOptions<TestModel> options) 
    { 
     var entities = new List<TestModel>() 
     { 
      new TestModel { Id = 1 }, 
      new TestModel { Id = 2 }, 
      new TestModel { Id = 3 }, 
      new TestModel { Id = 4 }, 
      new TestModel { Id = 5 }, 
      new TestModel { Id = 6 } 
     }; 

     //In this example, we use ApplyTo but you can build an adapter to your application service from the options parameter 
     return (IEnumerable<TestModel>)options.ApplyTo(entities.AsQueryable()); 
    } 
} 
  1. Implement your own IQueryable Provider that will query the application service的參數。我沒有嘗試這個,但我沒有看到任何理由不起作用。
+0

這個答案適用於Web API 2 – LostInComputer

+0

這是有幫助的。它可以處理多個對象之間的關係查詢嗎? http:// localhost/odata/Product(1)/供應商/供應商在這裏,我們想要獲得供應商的產品1供應商。 – MarvinDev

+0

這是另一個/無關的問題。 'ODataConventionModelBuilder'自動支持'/ odata/Product(1)/ Supplier'。對於'/ odata/Product(1)/ Supplier/Vendor',我不知道該怎麼做。解決方法是使用'$ expand'(odata/Product(0)/ Supplier?$ expand = Vendor),在結果中包含'Vendor' – LostInComputer