我有一個exising服務器框架(WCF服務),我需要升級到「oData」格式。 我下載了一些ODATA樣品從「OData的」網站: http://www.odata.org/ecosystemo數據與自定義提供商
我想避免使用「實體框架」,因爲它需要重寫我的代碼很多,所以我決定爲「自定義提供」解決方案。在我看到的所有示例中,有些東西讓我覺得奇怪:
WCF方法的調用調用「CreateDataSource()」方法(附加代碼),該方法從頭開始爲每個調用初始化整個數據模型。在這個演示版本中,重新加載商業實體的6個實例並進行過濾並不是什麼大事,然後過濾它們,但在現實生活中:我會擁有表格,每個表格包含1000-1000000行。
我應該如何避免每次加載我的整個數據庫到內存?我可能錯過了一些東西,否則這項技術不會有用。
protected override DSPContext CreateDataSource()
{
DSPContext context = new DSPContext();
ResourceSet productsSet, categoriesSet;
this.Metadata.TryResolveResourceSet("Products", out productsSet);
this.Metadata.TryResolveResourceSet("Categories", out categoriesSet);
IList<DSPResource> products = context.GetResourceSetEntities(productsSet.Name);
IList<DSPResource> categories = context.GetResourceSetEntities(categoriesSet.Name);
var categoryFood = new DSPResource(categoriesSet.ResourceType);
categoryFood.SetValue("ID", 0);
categoryFood.SetValue("Name", "Food");
categoryFood.SetValue("Products", new List<DSPResource>());
categories.Add(categoryFood);
var categoryBeverages = new DSPResource(categoriesSet.ResourceType);
categoryBeverages.SetValue("ID", 1);
categoryBeverages.SetValue("Name", "Beverages");
categoryBeverages.SetValue("Products", new List<DSPResource>());
categories.Add(categoryBeverages);
var categoryElectronics = new DSPResource(categoriesSet.ResourceType);
categoryElectronics.SetValue("ID", 2);
categoryElectronics.SetValue("Name", "Electronics");
categoryElectronics.SetValue("Products", new List<DSPResource>());
categories.Add(categoryElectronics);
var productBread = new DSPResource(productsSet.ResourceType);
productBread.SetValue("ID", 0);
productBread.SetValue("Name", "Bread");
productBread.SetValue("Description", "Whole grain bread");
productBread.SetValue("ReleaseDate", new DateTime(1992, 1, 1));
productBread.SetValue("DiscontinueDate", null);
productBread.SetValue("Rating", 4);
productBread.SetValue("Category", categoryFood);
productBread.SetValue("BackupCategoryID", 2);
productBread.SetValue("RelatedProductID", 1);
products.Add(productBread);
var productMilk = new DSPResource(productsSet.ResourceType);
productMilk.SetValue("ID", 1);
productMilk.SetValue("Name", "Milk");
productMilk.SetValue("Description", "Low fat milk");
productMilk.SetValue("ReleaseDate", new DateTime(1995, 10, 21));
productMilk.SetValue("DiscontinueDate", null);
productMilk.SetValue("Rating", 3);
productMilk.SetValue("Category", categoryBeverages);
productMilk.SetValue("BackupCategoryID", 2);
productMilk.SetValue("RelatedProductID", 2);
products.Add(productMilk);
var productWine = new DSPResource(productsSet.ResourceType);
productWine.SetValue("ID", 2);
productWine.SetValue("Name", "Wine");
productWine.SetValue("Description", "Red wine, year 2003");
productWine.SetValue("ReleaseDate", new DateTime(2003, 11, 24));
productWine.SetValue("DiscontinueDate", new DateTime(2008, 3, 1));
productWine.SetValue("Rating", 5);
productWine.SetValue("Category", categoryBeverages);
productWine.SetValue("BackupCategoryID", 4);
productWine.SetValue("RelatedProductID", 3);
products.Add(productWine);
((List<DSPResource>)categoryFood.GetValue("Products")).Add(productBread);
((List<DSPResource>)categoryBeverages.GetValue("Products")).Add(productMilk);
((List<DSPResource>)categoryBeverages.GetValue("Products")).Add(productWine);
return context;
您是否知道自定義提供程序的良好示例代碼? – user355289 2013-03-19 14:50:59
http://www.odata.org/ecosystem#samplecode - 查找OData提供者工具包,它包含自定義提供者的樣本。但請注意,它們都不包含示例IQueryable,它們都使用LINQ to Objects。 – 2013-03-20 09:13:12