locked
Client Only Properties RRS feed

  • Question

  • I need to have some properties from a class to be persisted in the client local database (sqlitestore) but ignored when sending the object to the server.

    For example:

    class Letter {

    public string Id { get; set; }

    public string DestinationAddress { get; set; }

    public bool IsSending { get; set; } // <- Only for client, but needs to be persisted

    }

    But when I send it (I use both Offline and Online Sync Tables) like this:

    await client.GetTable<Letter> ().InsertAsync (myLetter);

    I get "{"message":"The request is invalid.","modelState":{"item.IsSending":["An error has occurred."]}}"

    I could try different approaches, like making a separate table with these properties and use it only locally using SqliteStore directly, but I have to do two queries for one object every time. Or maybe using automapper but I haven't seen this on the client. If I can do it as I showed I think is the easiest and simplest, I just need to ignore the "client only" properties on the server or not send them to the server. Is it possible? I'm working on Xamarin.iOS. Thanks!

    Friday, August 5, 2016 7:57 PM

Answers

All replies

  • What happens when you [JsonIgnore] the IsSending field on the client?

    It's not a scenario we were even thinking of, so I know we don't test it.  I'd have to do some experimentation and that is where I would start.  At the end of the day, the Model is serialized according to normal Newtonsoft.Json rules, so that's a good guess.

    You could also ignore it on the remote end.  You add the [NotMapped] attribute into your model on an ASP.NET backend project.  This means it will get transferred to the backend, but not persisted in the database.

    Friday, August 5, 2016 9:24 PM

  • Thanks for your answer Adrian.

    I just tried with [JsonIgnore], the problem with it is that SqliteStore also uses Newtonsoft.Json so the "IsSending" property does not persist locally in the database. I guess [JsonIgnore] is a good option for properties in memory.

    If it gets transferred to the remote end is not that bad in my case as it is not sensitive or a lot of data. For now I found the solution of just adding that properties to the DTO on the remote end and then the automapper just ignore these properties (I'm using a simple MappedEntityDomainManager in my TableControllers).

    It would be great to have something like:

    [Local]
    public bool IsSending { get; set; }

    And these do not send to the server.

    Thanks!


    Friday, August 5, 2016 11:12 PM
  • This request came up the other day as well, so I'm creating a GitHub issue to track. You're right that [JsonIgnore] is used to serialize to the local store. Here's the issue: https://github.com/Azure/azure-mobile-apps-net-client/issues/207

    As a workaround, you can create a DelegatingHandler that removes the fields you don't want to send to the server. See this loggingHandler as an example: https://github.com/Azure-Samples/app-service-mobile-dotnet-todo-list-files/blob/master/src/client/MobileAppsFilesSample/Helpers/LoggingHandler.cs#L63

    • Marked as answer by alex782 Saturday, August 6, 2016 3:37 AM
    Saturday, August 6, 2016 1:52 AM
  • Cool, thanks Donna! and good alternative with the delegating handler. I gonna stick with my temporal solution for now and then see. Nice work.
    Saturday, August 6, 2016 3:37 AM