2013-05-23 1 views
4

Visual Studio 2012를 사용하여 OData wcf 데이터 서비스를 만드는 데 관심이 있습니다. 그러나 엔티티 모델 프레임 워크를 사용하고 싶지는 않지만 더 적은 nosql 데이터 세트를 사용하여 데이터. Microsoft의 엔티티 프레임 워크와 같은 특정 클래스 구조에 밀리지 않고 odata 서비스를 제어 할 수있는 방법이 있습니까?엔티티 프레임 워크없이 OData를 사용하는 방법

답변

4

Entity Framework없이 Microsoft OData 구현을 사용할 수 있습니다. 필요한 것은 IQueryable의 구현입니다. 다음은 객체 배열을 쿼리하는 예제 OData 서비스입니다.

using System.Web.Http; 
using System.Web.Http.OData; 
using System.Web.Http.OData.Builder; 
using System.Web.Http.OData.Query; 

// GET api/values 
[ActionName("FromList")] 
public IList<Poco> GetFromList(ODataQueryOptions<Poco> queryOptions) 
{ 
    IQueryable<Poco> data = (
     new Poco[] { 
      new Poco() { id = 1, name = "one", type = "a" }, 
      new Poco() { id = 2, name = "two", type = "b" }, 
      new Poco() { id = 3, name = "three", type = "c" } 
     }) 
     .AsQueryable(); 

    var t = new ODataValidationSettings() { MaxTop = 25 }; 
    queryOptions.Validate(t); 

    var s = new ODataQuerySettings() { PageSize = 25 }; 
    IEnumerable<Poco> results = 
     (IEnumerable<Poco>)queryOptions.ApplyTo(data, s); 

    return results.ToList(); 
} 

public class Poco 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
} 
+1

이 방법을 사용하면 데이터베이스에서 필터하는 기능을 잃지 않고 대신 메모리에서 수행 할 수 있습니다. 어쨌든 우아한 둥근, IQueryable 인터페이스를 다시 구현하지 않고? – gdp

+0

@gdp [Simple.OData] (http://www.codeproject.com/Articles/686240/reasons-to-consume-OData-feeds-using-Simple-ODa)와 같은 것을 찾고 계십니까? – qujck

+1

내가 보일 것이다,하지만 이상적으로 내가 OData 쿼리를 구문 분석하고 내가 SQL을 동적으로 구축 할 수있는 일종의 모델로 변환하려고합니다. 임 메신저 프로젝트의 데이터 계층에는 IQueryable을 반환 할 수있는 기능이 없으며 여전히 페이징/필터링이 DB에서 수행되기를 원합니다. – gdp