Azure Mobile Service TableController does not return internal objects

I am building a basic (my first) Azure Mobile Service with Table Storage to manage a simple Events application. My DataObjects consist of 2 types of objects: Coordinatorand Event, I want the coordinators to be a separate table for storing certain information, which I do not want to denormalize inside events, but the events also have an internal object. A place to store details for the location of the event, but I want to keep it denormalized, because I do not want to maintain this information separately from the event.

Here are the objects that I have: 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; }
}

TableController, VS-, , , , MobileServiceContext , Post , :

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, Coordinator Location , Json EventController Get - :

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

, 2 :

1) Location EventController , , , , Json, ( WebApiConfig) , , , MaxDepth, Location .

2) , , ( ), , , , .

, ?

+4
2

TableController. , , , OData $ .


1: n .NET 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(); 
    }
+4

. , , . , - .

, 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);
    }
+1

Source: https://habr.com/ru/post/1605918/


All Articles