locked
no [NotMapped] or [NonSerialized] on the client side RRS feed

  • Question

  • I'm using silverlight to display objects retrieved from a WCF Data Service in a datagrid (i.e. the datagrid is being bound to a DataServiceCollection<GponOnt>).  My GponOnt object has a navigation property called GponOntLocations that contains all the locations the GponOnt serves.  I can of course bind directly to GponOntLocations and use a value converter to convert the collection of GponOntLocation objects to a string for display but in order to use all the filtering/grouping provided natively by the grid, it would be better if I bound the column to a simple "string" type instead of the collection.  Given that, I thought I would just use partial classes on the client side like:

      public partial class SrvStreetLocation
      {
        public override string ToString()
        {
          string addr = this.HouseNumber.Trim();
    
          if (AdrStreet != null)
          {
            addr = (addr + " " + this.AdrStreet.PreDirectional).Trim();
            addr = (addr + " " + this.AdrStreet.Street).Trim();
            addr = (addr + " " + this.AdrStreet.StreetSuffix).Trim();
          }
    
          addr = (addr + " " + this.Apartment).Trim();
    
          return addr;
        }
      }
    
      public partial class GponOnt
      {
        public string Locations
        {
          get
          {
            if (GponOntLocations != null)
            {
              return string.Join(",", (from l in GponOntLocations select l.SrvLocation.SrvStreetLocation.ToString()).ToArray());
            }
            else
            {
              return "";
            }
          }
        }
      }
    


    This works great, but when I go to perform CRUD operations on the GponOnt objects and post them back to the server with SaveChanges, I of course get an error saying "property name 'Locations' is not valid.  I thought, simple , I'll just add a [NonMapped] or [NotSerialized] attribute to the above code but alas, I find the Silverlight version of the library doesn't support those.  Anyone know of an easy solution to make this synthetic property I've created not be serialized???

    Thursday, June 16, 2011 9:04 PM

Answers

  • There isn't any declarative way to do this today with WCF Data Services (regardless of silverlight vs desktop).

    One alternative is to listen on the WritingEntity event fired by DataServiceContext, and remove the property from the XML.


    Matt Meehan (WCF Data Services / OData)
    Thursday, June 16, 2011 11:20 PM
    Moderator

All replies

  • There isn't any declarative way to do this today with WCF Data Services (regardless of silverlight vs desktop).

    One alternative is to listen on the WritingEntity event fired by DataServiceContext, and remove the property from the XML.


    Matt Meehan (WCF Data Services / OData)
    Thursday, June 16, 2011 11:20 PM
    Moderator
  • Thank you so much - that works like a champ and is much simpler than all the pain that would have been required to get the filtering/grouping working when binding to the raw DataServiceCollection.  FWIW, now that I'm finally starting to understand it, I'm liking this WCF Data Service stuff more and more!  Nothing is perfect, and while HTML and ajax are infinitely flexible, I find them lacking on large projects because there is no compiler to help you with type safety and other mundane but easy to mess up stuff (I hate to say I've spent more than a few minutes tracking down subtle errors in Javascript that would have been easily caught in a compiled environment) - so I think oData + Silverlight is a great solution.  Thanks again for your help.
    Friday, June 17, 2011 12:56 AM