직렬화 할 POCO에서 계층 적 자식 컬렉션을 얻는 데 시간이 많이 걸립니다. 이것은 Azure Mobile Services 프로젝트입니다. 이 작업을 수행하기 위해 간단한 예를 생각해 보았습니다.
다음은 프로젝트의 유일한 DTO POCO입니다. 계층 구조를 함께 연결하는 데 사용되는 Parent
및 라는 탐색 속성 (가상으로 표시됨)을 확인 Children
합니다.
public class Node : EntityData
{
public Node()
{
Children = new List<Node>();
}
public string Text { get; set; }
public string ParentId { get; set; }
public virtual Node Parent { get; set; }
public virtual ICollection<Node> Children { get; set; }
}
컨트롤러 :
public class NodeController : TableController<Node>
{
/* BEGIN boilerplate (from Azure Mobile Services project template) */
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MyContext context = new MyContext();
DomainManager = new EntityDomainManager<Node>(context, Request, Services);
}
/* END boilerplate (from Azure Mobile Services project template) */
// GET tables/Node
/// <summary>
/// Gets all child nodes of a particular node, and their children.
/// To retrieve the top-level node, pass no id.
/// </summary>
/// <param name="id">The id of the node you wish to retrieve.</param>
/// <returns>The child nodes of a given id.</returns>
public IQueryable<Node> GetNodes(string id = null)
{
return Query().Where(x => x.ParentId == id).Include(x => x.Children);
}
}
컨텍스트 클래스 :
public class MyContext : DbContext
{
private const string connectionStringName = "Name=MS_TableConnectionString";
public MyContext() : base(connectionStringName)
{
}
public DbSet<Node> Nodes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Node>()
.HasMany(t => t.Children)
.WithOptional(t => t.Parent)
.HasForeignKey(t => t.ParentId);
/* BEGIN boilerplate (from Azure Mobile Services project template) */
string schema = ServiceSettingsDictionary.GetSchemaName();
if (!string.IsNullOrEmpty(schema))
{
modelBuilder.HasDefaultSchema(schema);
}
modelBuilder.Conventions.Add(
new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>(
"ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));
/* END boilerplate (from Azure Mobile Services project template) */
}
}
컨텍스트 이니셜 라이저 :
public class MyContextInitializer : ClearDatabaseSchemaAlways<MyContext>
{
protected override void Seed(MyContext context)
{
var node0_1 = new Node { Id = Guid.NewGuid().ToString(), Text = "Node 0-1" };
var node0_2 = new Node { Id = Guid.NewGuid().ToString(), Text = "Node 0-2" };
var node1_1 = new Node { Id = Guid.NewGuid().ToString(), Text = "Item 1-1", Parent = node0_1 };
var node1_2 = new Node { Id = Guid.NewGuid().ToString(), Text = "Item 1-2", Parent = node0_1 };
var node2_1 = new Node { Id = Guid.NewGuid().ToString(), Text = "Item 2-1", Parent = node0_2 };
var node2_2 = new Node { Id = Guid.NewGuid().ToString(), Text = "Item 2-2", Parent = node0_2 };
node0_1.Children.Add(node1_1);
node0_1.Children.Add(node1_2);
node0_2.Children.Add(node2_1);
node0_2.Children.Add(node2_2);
List<Node> nodes = new List<Node>
{
node0_1,
node0_2
};
context.Set<Node>().AddRange(nodes);
base.Seed(context);
}
}
왜 아이들은 결과에 나타나지 않습니까?
[
{
"$id": "1",
"id": "2c381538-b8e9-4b7c-b25d-7f6fd8cd373e",
"parentId": null,
"text": "Node 0-2"
},
{
"$id": "2",
"id": "695af179-aa27-45d3-9299-a96c5e719448",
"parentId": null,
"text": "Node 0-1"
}
]
... 생성 된 API 문서 샘플에서 하위가 있어야한다고 제안하지만 :
[
{
"$id": "1",
"text": "sample string 1",
"parentId": "sample string 2",
"parent": {
"$ref": "1"
},
"children": [
{
"$ref": "1"
},
{
"$ref": "1"
},
{
"$ref": "1"
}
],
"id": "sample string 3",
"__version": "QEBA",
"__createdAt": "2015-06-26T01:46:14.108Z",
"__updatedAt": "2015-06-26T01:46:14.108Z",
"__deleted": true
},
{
"$ref": "1"
},
{
"$ref": "1"
}
]
마침내 그것을 알아 냈습니다. 그냥 컨트롤러의 반환 형식 변경했습니다 GetNodes()
에서 방법 IQueryable<Node>
에 IEnmerable<Node>
. 나는 아직도 정확히 그 이유 를 모르지만 그것이 효과가 있다는 것은 기쁘다. 이제 실제 서비스를 수정 한 다음 Xamarin 앱에서 사용하는 중요한 작업으로 넘어갈 수 있습니다.
수정 된 메서드 서명 (좋은 측정을 위해 비동기로 던짐) :
public async Task<IEnumerable<Node>> GetNode(string id = null)
{
return await Query()
.Where(x => x.ParentId == id)
.Include(x => x.Children)
.ToListAsync();
}
결과 :
[
{
"$id": "1",
"children": [
{
"$id": "2",
"children": [],
"parent": {
"$ref": "1"
},
"text": "Item 1-2",
"parentId": "6a616abe-8328-4ca0-92e4-de0734101f2f",
"id": "398cf2e6-dbfb-4fe1-8555-13090885292f",
"__version": "AAAAAAABbzQ=",
"__createdAt": "2015-06-26T21:07:31.466Z",
"__updatedAt": "2015-06-26T21:07:31.466Z"
},
{
"$id": "3",
"children": [],
"parent": {
"$ref": "1"
},
"text": "Item 1-1",
"parentId": "6a616abe-8328-4ca0-92e4-de0734101f2f",
"id": "6560562b-0694-4436-bc50-08ead5af29e0",
"__version": "AAAAAAABbzY=",
"__createdAt": "2015-06-26T21:07:31.521Z",
"__updatedAt": "2015-06-26T21:07:31.521Z"
}
],
"text": "Node 0-1",
"id": "6a616abe-8328-4ca0-92e4-de0734101f2f",
"__version": "AAAAAAABbzI=",
"__createdAt": "2015-06-26T21:07:31.382Z",
"__updatedAt": "2015-06-26T21:07:31.399Z"
},
{
"$id": "4",
"children": [
{
"$id": "5",
"children": [],
"parent": {
"$ref": "4"
},
"text": "Item 2-1",
"parentId": "fdf50979-e191-41c7-b6c4-c2067cd88dc9",
"id": "9417571f-f89e-4183-8c14-ba7da3629624",
"__version": "AAAAAAABbzo=",
"__createdAt": "2015-06-26T21:07:31.634Z",
"__updatedAt": "2015-06-26T21:07:31.634Z"
},
{
"$id": "6",
"children": [],
"parent": {
"$ref": "4"
},
"text": "Item 2-2",
"parentId": "fdf50979-e191-41c7-b6c4-c2067cd88dc9",
"id": "b077165c-1e3e-456f-b4c5-6b116941ba30",
"__version": "AAAAAAABbzw=",
"__createdAt": "2015-06-26T21:07:31.693Z",
"__updatedAt": "2015-06-26T21:07:31.694Z"
}
],
"text": "Node 0-2",
"id": "fdf50979-e191-41c7-b6c4-c2067cd88dc9",
"__version": "AAAAAAABbzg=",
"__createdAt": "2015-06-26T21:07:31.575Z",
"__updatedAt": "2015-06-26T21:07:31.575Z"
}
]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다