我有一个现有的服务器框架(WCF 服务),我需要升级到“oData”格式。我从“oData”站点下载了一些 oData 示例: http ://www.odata.org/ecosystem
我想避免使用“实体框架”,因为它需要重写我的大量代码,所以我选择了“自定义提供程序”解决方案。在我看到的所有示例中,有些东西让我感到奇怪:
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;