编程语言
首页 > 编程语言> > c# – Azure Mobile Service TableController不返回内部对象

c# – Azure Mobile Service TableController不返回内部对象

作者:互联网

我正在创建一个基本(我的第一个)带有表存储的Azure移动服务来控制一个简单的事件应用程序.我的DataObjects由2个对象类型组成:Coordinator和Event,我希望Coordinators是一个单独的表,用于存储我不希望它在Events中被非规范化的特定信息,但是Events还有一个内部对象Location来存储事件的详细信息位置,但我想存储非规范化,因为我不想将此细节与事件分开维护.

这是我到目前为止的目标:
DataObjests:

public class Coordinator : EntityData {
    public string Name { get; set; }
    public int Points { get; set; }
    public bool IsActive { get; set; }
}
public class Event: EntityData {
        public Coordinator Coordinator { get; set; }
        public DateTime EventDate { get; set; }
        public int Attendees { get; set; }
        public Location Location { get; set; }
}
public class Location {
        public string Name { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public string PostCode { get; set; }
}

作为VS脚手架生成的基本TableController的控制器,我做的唯一改变是在事件控制器上公开MobileServiceContext以使Post方法在保存时找到现有的Coordinator,因为客户端只会发布Coordinator的ID:

public class EventController : TableController<Event> {
        private MobileServiceContext context;
        protected override void Initialize(HttpControllerContext controllerContext) {
            base.Initialize(controllerContext);
            context = new MobileServiceContext();
            DomainManager = new EntityDomainManager<Event>(context, Request, Services);
        }
        protected override void Dispose(bool disposing) {
            context?.Dispose();
            base.Dispose(disposing);
        }

        public IQueryable<Event> GetAllEvent() {
            return Query();
        }

        public async Task<IHttpActionResult> PostEvent(Event item) {
            var coordinator = context.Coordinators.Find(item.Coordinator.Id);
            item.Coordinator = coordinator;
            Event current = await InsertAsync(item);
            return CreatedAtRoute("Tables", new { id = current.Id }, current);
        }

}

从客户端发布数据按预期工作,我有一个包含正确数据的Coordinator表:

ID  Name    Points  IsActive    Version CreatedAt   UpdatedAt   Deleted
cdf96b0f93644f1e945bd16d63aa96e0    John Smith  10  True    0x00000000000007D2  04/09/2015 09:15:02 +00:00  04/09/2015 09:15:02 +00:00  False
f216406eb9ad4cdcb7b8866fdf126727    Rebecca Jones   10  True    0x00000000000007D4  04/09/2015 09:15:30 +00:00  04/09/2015 09:15:30 +00:00  False

与第一个协调员关联的事件:

Id  EventDate   Attendees   Location_Name   Location_Address1   Location_Address2   Location_City   Location_County Location_PostCode   Version CreatedAt   UpdatedAt   Deleted Coordinator_Id
961abbf839bf4e3481ff43a214372b7f    04/11/2015 09:00:00 0   O2 Arena    Peninsula Square        London      SE10 0DX    0x00000000000007D6  04/09/2015 09:18:11 +00:00  04/09/2015 09:18:11 +00:00  False   cdf96b0f93644f1e945bd16d63aa96e0

在这一点上所有看起来都很好,我的2个问题是Get of the Event,其中没有返回Coordinator和Location对象,而我的EventController的Get的Json就是这样:

[{"id":"961abbf839bf4e3481ff43a214372b7f","attendees":0,"eventDate":"2015-11-04T09:00:00Z"}]

所以我的两个问题是:

1)Location对象由服务器上的EventController正确加载,如果我在返回之前中断,我可以看到正确加载的属性,对我来说看起来像Json序列化问题,但我已经尝试更改序列化程序的配置(在WebApiConfig上) )没有太大影响,我尝试的最后一个选项是MaxDepth但仍然没有返回Location对象.

2)协调器对象我根本没有加载到服务器上,甚至没有Id(正确存储在表上)所以我不能强制加载整个对象,当然它不会返回给客户端.

我在这里做错了什么想法?

提前致谢

Claiton Lovato

解决方法:

这是TableController的默认行为.
为了实现您正在寻找的东西,您应该在控制器中实现OData $expand.

本文为解决方案提供了良好的步骤
Retrieving data from 1:n relationship using .NET backend Azure Mobile Services

作为进一步的扩展,我实现了一个自定义属性,您可以在控制器方法中使用该属性来指定客户端可以请求扩展的属性.您可能不希望始终返回所有子关系(扩展对象)

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class ExpandablePropertyAttribute : ActionFilterAttribute
{
    #region [ Constants ]
    private const string ODATA_EXPAND = "$expand=";
    #endregion

    #region [ Fields ]
    private string _propertyName;
    private bool _alwaysExpand;
    #endregion

    #region [ Ctor ]
    public ExpandablePropertyAttribute(string propertyName, bool alwaysExpand = false)
    {
        this._propertyName = propertyName;
        this._alwaysExpand = alwaysExpand;
    }
    #endregion

    #region [ Public Methods - OnActionExecuting ]
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        base.OnActionExecuting(actionContext);
        var uriBuilder = new UriBuilder(actionContext.Request.RequestUri);
        var queryParams = uriBuilder.Query.TrimStart('?').Split(new char[1] { '&' }, StringSplitOptions.RemoveEmptyEntries).ToList();
        int expandIndex = -1;

        for (var i = 0; i < queryParams.Count; i++)
        {
            if (queryParams[i].StartsWith(ODATA_EXPAND, StringComparison.Ordinal))
            {
                expandIndex = i;
                break;
            }
        }

        if (expandIndex >= 0 || this._alwaysExpand)
        {
            if (expandIndex < 0)
            {
                queryParams.Add(string.Concat(ODATA_EXPAND, this._propertyName));
            }
            else
            {
                queryParams[expandIndex] = queryParams[expandIndex] + "," + this._propertyName;
            }

            uriBuilder.Query = string.Join("&", queryParams);
            actionContext.Request.RequestUri = uriBuilder.Uri;
        }

    }
    #endregion
}

我以这种方式在我的控制器中使用它:

    [ExpandableProperty("Documents", false)]
    public IQueryable<ClientActivity> GetAllClientActivities()
    {
        return Query(); 
    }

标签:c,azure,azure-mobile-services,azure-table-storage
来源: https://codeday.me/bug/20190609/1203333.html