我正在使用表存储创建一个基本(我的第一个)Azure移动服务来控制一个简单的事件应用程序。我的DataObjects由2个对象类型组成:Coordinator
和Event
,我希望协调器是一个单独的表来存储特定信息,我不希望它在事件中被非规范化,但事件也有一个内部object存储事件位置详细信息的位置,但我想存储非规范化的位置,因为我不想将此详细信息与事件分开维护。
这是我到目前为止的目标: 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只会发布协调员的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);
}
}
从客户端发布数据按预期工作,我有一个包含正确数据的协调员表:
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,其中{I}}和Coordinator
对象都没有返回,而我的EventController的Json只是这样:< / p>
Location
所以我的两个问题是:
1)Location对象由服务器上的[{"id":"961abbf839bf4e3481ff43a214372b7f","attendees":0,"eventDate":"2015-11-04T09:00:00Z"}]
正确加载,如果我在返回之前中断,我可以看到正确加载的属性,对我来说看起来像Json序列化问题,但我已经尝试过了更改序列化程序的配置(在EventController
上)没有太大影响,我尝试的最后一个选项是WebApiConfig
,但仍然没有返回Location对象。
2)协调器对象我根本没有加载到服务器上,甚至没有Id(正确存储在表上)所以我不能强制加载整个对象,当然它不会返回到客户。
关于我在这里做错了什么想法?
提前致谢
Claiton Lovato
答案 0 :(得分:4)
这是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();
}
答案 1 :(得分:1)
我已设法将复杂属性序列化并发送回客户端。也许解决方案不是最干净的,但它适用于我的情况。希望其他人也会发现它也很有用。
首先,创建一个继承自EnableQueryAttribute
并覆盖GetModel
方法的类,如下所示:
public class CustomEnableQueryAttribute : EnableQueryAttribute
{
public override IEdmModel GetModel(Type elementClrType, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)
{
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Event>("Events");
return modelBuilder.GetEdmModel();
}
}
然后,在控制器的Initialize
方法中,添加以下行:
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
context = new MobileServiceContext();
DomainManager = new EntityDomainManager<Event>(context, Request, Services);
//Add these lines
var service = Configuration.Services.GetServices(typeof(IFilterProvider)).ToList();
service.Remove(service.FirstOrDefault(f => f.GetType() == typeof(TableFilterProvider)));
service.Add(new TableFilterProvider(new CustomEnableQueryAttribute()));
Configuration.Services.ReplaceRange(typeof(IFilterProvider), service.ToList().AsEnumerable());
Request.Properties.Add("MS_IsQueryableAction", true);
}