使用循环依赖和可加载列表实现WebAPI [英] Implementing WebAPI with circular dependencies and loadable lists
问题描述
public class Book
{
Guid id {get;组; }
string标题{get;组; }
Store Store {get;组; }
}
public class Store
{
Guid id {get;组; }
string标题{get;组; }
string所有者{get;组; }
IEnumerable< Book>书籍{get;组; $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ p
我有后端与SQL服务器和EntityFramework适配器在代码。只有在我需要它之后,我应该怎么做才能返回书籍清单?
如你所见,我有循环依赖,这使得我的JSON序列化程序显示错误。我解决了:
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
但序列化所有数据,我的回复是巨大的,这对我的Windows Phone应用程序不利。在某些情况下,我只需要Store对象,但在其他情况下,我需要Store与所有引用的书籍。
在控制器中,我使用[Queryable]属性来使用OData查询。
P.S.我不能创建更多的Web方法,如GetBooks,GetStore等。应该是一种方法。
如何解决我的问题?
解决方案
我如何解决我的问题?
不要从您的API操作方法返回Entities。
相反,返回模型。
所以,而不是这样:
public IEnumerable< Store> GetStores()
{
using(var db = new MyDbContext())
{
return db.Stores;
}
}
做这样的事情:
public IEnumerable&StoreModel> GetStores()
{
using(var db = new MyDbContext())
{
var entities = db.Stores;
return entities.Select(x => new StoreModel
{
Id = x.Id,
Title = x.Title,
Owner = x.Owner,
Books = x.Books.Select(y => new BookModel
{
Id = y.Id,
Title = y.Title,
StoreId = x .Id,
}),
});
}
}
通过这样做,您可以消除循环依赖图。 不再引用商店
,只有 StoreId
。
How it is possible to implement such "smart" GET in WebAPI? I have 2 classes that i want to get from my service. Let's say its book store which has relation to book objects.
public class Book
{
Guid Id { get; set; }
string Title { get; set; }
Store Store { get; set; }
}
public class Store
{
Guid Id { get; set; }
string Title { get; set; }
string Owner { get; set; }
IEnumerable<Book> Books { get; set; }
}
I have backend with SQL server and EntityFramework adapter in code. How should I make to return the list of books only after I need it (call for it), like with OData?
As you can see I have circular dependancies, which makes my JSON serializer to show errors. I resolve it with:
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
but serialized all data and my response is HUGE, which is not good for my Windows Phone app. In some cases I need only Store object, but in other cases I need Store with all referenced books.
In controllers I user [Queryable] attribute to use OData queries.
P.S. I can't create more web-methods like GetBooks, GetStore and so on. It should be one method.
How could I resolve my problem?
解决方案
How could I resolve my problem?
Do not return Entities from your API action methods.
Instead, return models.
So instead of this:
public IEnumerable<Store> GetStores()
{
using (var db = new MyDbContext())
{
return db.Stores;
}
}
Do something like this:
public IEnumerable<StoreModel> GetStores()
{
using (var db = new MyDbContext())
{
var entities = db.Stores;
return entities.Select(x => new StoreModel
{
Id = x.Id,
Title = x.Title,
Owner = x.Owner,
Books = x.Books.Select(y => new BookModel
{
Id = y.Id,
Title = y.Title,
StoreId = x.Id,
}),
});
}
}
By doing it this way you eliminate the circular dependency graph. Book
no longer references Store
, only StoreId
.
这篇关于使用循环依赖和可加载列表实现WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!