示例#1
0
        // This manually parses the XML that comes back.
        // This function uses code from this blog entry:
        // http://blogs.msdn.com/b/avkashchauhan/archive/2011/03/28/reading-and-saving-table-storage-entities-without-knowing-the-schema-or-updating-tablestorageentity-schema-at-runtime.aspx
        public static void OnReadingEntity(object sender, Client.ReadingWritingEntityEventArgs args)
        {
            GenericEntity entity = args.Entity as GenericEntity;

            if (entity == null)
            {
                return;
            }

            // read each property, type and value in the payload
            var properties = args.Entity.GetType().GetProperties();
            var q          = from p in args.Data.Element(AtomNamespace + "content")
                             .Element(MetadataNamespace + "properties")
                             .Elements()
                             where properties.All(pp => pp.Name != p.Name.LocalName)
                             select new
            {
                Name     = p.Name.LocalName,
                IsNull   = string.Equals("true", p.Attribute(MetadataNamespace + "null") == null ? null : p.Attribute(MetadataNamespace + "null").Value, StringComparison.OrdinalIgnoreCase),
                TypeName = p.Attribute(MetadataNamespace + "type") == null ? null : p.Attribute(MetadataNamespace + "type").Value,
                p.Value
            };

            foreach (var dp in q)
            {
                string value = dp.Value;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    value = string.Empty;
                }
                entity.properties[dp.Name] = dp.Value;
            }
        }
        void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e) {
            var bitbucketProp = e.Entity.GetType().GetProperty("DynamicProperties");

            IDictionary<string, object> bitbucket = null;
            if (bitbucketProp != null) {
                bitbucket = bitbucketProp.GetValue(e.Entity, null) as IDictionary<string, object>;
            }

            if (bitbucket != null) {
                var properties = e.Entity.GetType().GetProperties();

                var q = from p in e.Data.Element(AtomNamespace + "content")
                                        .Element(AstoriaMetadataNamespace + "properties")
                                        .Elements()
                        where properties.All(pp => pp.Name != p.Name.LocalName)
                        select new {
                            Name = p.Name.LocalName,
                            IsNull = string.Equals("true", p.Attribute(AstoriaMetadataNamespace + "null") == null ? null : p.Attribute(AstoriaMetadataNamespace + "null").Value, StringComparison.OrdinalIgnoreCase),
                            TypeName = p.Attribute(AstoriaMetadataNamespace + "type") == null ? null : p.Attribute(AstoriaMetadataNamespace + "type").Value,
                            p.Value
                        };

                foreach (var dp in q) {
                    bitbucket[dp.Name] = GetTypedEdmValue(dp.TypeName, dp.Value, dp.IsNull);
                }
            }
        }
        private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            // e.Data gives you the XElement for the Serialization of the Entity
            //Using XLinq  , you can  add/Remove properties to the element Payload
            var xnEntityProperties = XName.Get("properties", e.Data.GetNamespaceOfPrefix("m").NamespaceName);
            XElement xPayload = null;

            //var dNamespace = e.Data.GetNamespaceOfPrefix("d").NamespaceName;
            var dNamespace = DataNamespace;

            foreach (var property in e.Entity.GetType().GetProperties())
            {
                if (property.HasAttribute<CloudIncludeAttribute>())
                {
                    if (xPayload == null)
                    {
                        xPayload = e.Data.Descendants().First(xe => xe.Name == xnEntityProperties);
                    }

                    var xDummyPropertyName = XName.Get(string.Format("dummy{0}", property.Name), dNamespace);

                    var xElement = xPayload.Descendants().FirstOrDefault(xe => xe.Name == xDummyPropertyName);
                    if (xElement != null)
                    {
                        if (_converter == null)
                            throw new InvalidOperationException("There no converter specified.");

                        property.SetValue(e.Entity, _converter.Deserialize(xElement.Value, property.PropertyType), null);
                    }
                }
            }
        }
        public void OnReadingEntity(object sender, ReadingWritingEntityEventArgs args)
        {
            XNamespace atomNamespace = "http://www.w3.org/2005/Atom";
            XNamespace dataServiceNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices";
            XNamespace metadataNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

            TableGenericEntity entity = args.Entity as TableGenericEntity;
            if (entity == null)
            {
                return;
            }

            // read each property, type and value in the payload
            var properties = args.Entity.GetType().GetProperties();
            var q = from p in args.Data.Element(atomNamespace + "content")
                                    .Element(metadataNamespace + "properties")
                                    .Elements()
                    where properties.All(pp => pp.Name != p.Name.LocalName)
                    select new
                    {
                        Name = p.Name.LocalName,
                        IsNull = string.Equals("true", p.Attribute(metadataNamespace + "null") == null ? null : p.Attribute(metadataNamespace + "null").Value, StringComparison.OrdinalIgnoreCase),
                        TypeName = p.Attribute(metadataNamespace + "type") == null ? null : p.Attribute(metadataNamespace + "type").Value,
                        p.Value
                    };

            foreach (var dp in q)
            {
                entity[dp.Name] = new Property(dp.Name, dp.TypeName, GetTypedEdmValue(dp.TypeName, dp.Value, dp.IsNull));
            }
        }
        private void OnWritingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            // e.Data gives you the XElement for the Serialization of the Entity
            //Using XLinq  , you can  add/Remove properties to the element Payload
            var xnEntityProperties = XName.Get("properties", e.Data.GetNamespaceOfPrefix("m").NamespaceName);
            XElement xPayload = null;

            var dNamespace = DataNamespace;

            foreach (var property in e.Entity.GetType().GetProperties())
            {
                if (property.HasAttribute<CloudIgnoreAttribute>())
                {
                    if (xPayload == null)
                    {
                        xPayload = e.Data.Descendants().First(xe => xe.Name == xnEntityProperties);
                    }

                    //The XName of the property we are going to remove from the payload
                    var xPropertyName = XName.Get(property.Name, dNamespace);
                    //Get the Property of the entity  you don't want sent to the server
                    var xElement = xPayload.Descendants().FirstOrDefault(xe => xe.Name == xPropertyName);
                    //Remove this property from the Payload sent to the server
                    if (xElement != null)
                        xElement.Remove();
                }

                if (property.HasAttribute<CloudIncludeAttribute>())
                {
                    if (xPayload == null)
                    {
                        xPayload = e.Data.Descendants().First(xe => xe.Name == xnEntityProperties);
                    }

                    var xPropertyName = XName.Get(property.Name, dNamespace);
                    var xDummyPropertyName = XName.Get(string.Format("dummy{0}", property.Name), dNamespace);

                    var xElement = xPayload.Descendants().FirstOrDefault(xe => xe.Name == xPropertyName);
                    if (xElement != null)
                        xElement.Remove();

                    if (_converter == null)
                        throw new InvalidOperationException("There no converter specified.");

                    xElement = new XElement(xDummyPropertyName, _converter.Serialize(property.GetValue(e.Entity, null)));
                    xPayload.Add(xElement);
                }
            }
        }
        /// <summary>
        /// Handles entity reading event. Serialises generic entity properties into entity xml.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Reading writing entity event argument</param>
        public static void ReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            XNamespace d = DataServiceNamespace;
            XNamespace m = DataServiceMetadataNamespace;

            var entity = e.Entity as GenericEntity;
            var xProperties = e.Data.Descendants(m + "properties");

            foreach (var xProp in xProperties.Elements())
            {
                var name = xProp.Name.LocalName;
                var xType = xProp.Attribute(m + "type");
                var type = xType != null ? xType.Value : String.Empty;
                var value = xProp.Value;
                entity.Properties.Add(xProp.Name.LocalName, new EntityProperty { Type = type, Value = value });
            }
        }
示例#7
0
        /// <summary>
        /// Fires the reading atom entity event.
        /// </summary>
        /// <param name="materializerEntry">The materializer entry.</param>
        internal void FireReadingAtomEntityEvent(MaterializerEntry materializerEntry)
        {
            Debug.Assert(materializerEntry != null, "materializerEntry != null");

            if (this.ReadingAtomEntity != null)
            {
                if (materializerEntry.Format == ODataFormat.Atom)
                {
                    ReadingEntityInfo readingEntityInfo = materializerEntry.Entry.GetAnnotation <ReadingEntityInfo>();
                    Debug.Assert(readingEntityInfo != null, "readingEntityInfo != null");
                    ReadingWritingEntityEventArgs args = new ReadingWritingEntityEventArgs(materializerEntry.ResolvedObject, readingEntityInfo.EntryPayload, readingEntityInfo.BaseUri);
                    this.ReadingAtomEntity(this.sender, args);
                }
#if DEBUG
                else
                {
                    // For Json formats, there must not be any readingEntityInfo annotation on the entry.
                    ReadingEntityInfo readingEntityInfo = materializerEntry.Entry.GetAnnotation <ReadingEntityInfo>();
                    Debug.Assert(readingEntityInfo == null, "readingEntityInfo == null");
                }
#endif
            }
        }
        private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            var package = (DataServicePackage)e.Entity;

            // REVIEW: This is the only way (I know) to download the package on demand
            // GetReadStreamUri cannot be evaluated inside of OnReadingEntity. Lazily evaluate it inside DownloadPackage
            package.Context = Context;
            package.Downloader = _packageDownloader;
        }
 private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs args)
 {
     IMediaContextContainer mediaContextContainer = args.Entity as IMediaContextContainer;
     if (mediaContextContainer != null)
     {
         mediaContextContainer.SetMediaContext(this._mediaContext);
     }
 }
        /// <summary>
        /// Handles entity writing event. Deserialises entity xml properties into generic entity properties.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Reading writing entity event argument</param>
        public static void WritingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            XNamespace d = DataServiceNamespace;
            XNamespace m = DataServiceMetadataNamespace;
            var properties = e.Data.Descendants(m + "properties").First();
            var entity = e.Entity as GenericEntity;

            if (entity != null)
            {
                foreach (string propertyName in entity.Properties.Keys)
                {
                    var entityProperty = entity.Properties[propertyName] as EntityProperty;
                    XElement property = String.IsNullOrEmpty(entityProperty.Type)
                        ? new XElement(d + propertyName, entityProperty.Value)
                        : new XElement(d + propertyName,
                            new XAttribute(m + "type", entityProperty.Type),
                            entityProperty.Value);
                    properties.Add(property);
                }
            }
        }
示例#11
0
 private void context_WritingEntity(object sender, ReadingWritingEntityEventArgs e)
 {
     // e.Data gives you the XElement for the Serialization of the Entity
     //Using XLinq  , you can  add/Remove properties to the element Payload
     var xnEntityProperties = XName.Get("properties", e.Data.GetNamespaceOfPrefix("m").NamespaceName);
     XElement xePayload = null;
     foreach (var property in e.Entity.GetType().GetProperties())
     {
         var doNotSerializeAttributes = property.GetCustomAttributes(typeof (DoNotSerializeAttribute), false);
         if (doNotSerializeAttributes.Length > 0)
         {
             if (xePayload == null)
             {
                 xePayload =
                     e.Data.Descendants().First(xe => xe.Name == xnEntityProperties);
             }
             //The XName of the property we are going to remove from the payload
             var xnProperty = XName.Get(property.Name, e.Data.GetNamespaceOfPrefix("d").NamespaceName);
             //Get the Property of the entity  you don't want sent to the server
             foreach (var xeRemoveThisProperty in xePayload.Descendants(xnProperty).ToList())
             {
                 //Remove this property from the Payload sent to the server
                 xeRemoveThisProperty.Remove();
             }
         }
     }
 }
示例#12
0
        static void context_ReadingEntity(object sender, ReadingWritingEntityEventArgs args)
        {
            TableEntity entity = args.Entity as TableEntity;
            if (entity == null)
                return;

            // read each property, type and value in the payload
            var properties = args.Entity.GetType().GetProperties();
            var q = from p in args.Data.Element(AtomNamespace + "content")
                                    .Element(AstoriaMetadataNamespace + "properties")
                                    .Elements()
                    where properties.All(pp => pp.Name != p.Name.LocalName)
                    select new
                    {
                        Name = p.Name.LocalName,
                        p.Value
                    };

            foreach (var dp in q)
                entity[dp.Name] = dp.Value;
        }
        private static void OnWritingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            var properties = e.Data.Descendants(m + "properties").First();

            foreach (var property in e.Entity.GetType().GetProperties().Where(p=>p.CanRead && p.CanWrite))
            {
                var propertyType = property.PropertyType;

                if (!TypeHelpers.IsComplexType(propertyType))
                {
                    continue;
                }

                var node = properties.Element(d + property.Name);

                if (node != null)
                {
                    node.Remove();
                }

                var propertyValue = property.GetValue(e.Entity, null);

                if (TypeHelpers.IsCollection(propertyType))
                {
                    UnbindCollection(propertyValue, propertyType.GetGenericArguments()[0], property.Name, properties);
                }
                else
                {
                    UnbindModel(propertyValue, propertyType, property.Name, properties);
                }
            }
        }
示例#14
0
        private void DataToken_ReadingEntity(object sender, System.Data.Services.Client.ReadingWritingEntityEventArgs e)
        {
            var entity = (e.Entity as INotifyPropertyChanged);

            entity.PropertyChanged += Entity_PropertyChanged;
        }
        /// <summary>
        /// Called when the data services data context has finished trying to deserialize and entity
        /// from table storage.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Data.Services.Client.ReadingWritingEntityEventArgs"/> instance containing the event data.</param>
        private static void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            GenericEntity entity = e.Entity as GenericEntity;

            if (null == entity)
            {
                return;
            }

            entity.TableName = e.Data.Element(AtomNS + "link").Attribute("title").Value;

            var q = from p in e.Data.Element(AtomNS + "content").Element(mNS + "properties").Elements()
                    select new
                    {
                        Name = p.Name.LocalName,
                        IsNull = string.Equals("true", p.Attribute(mNS + "null") == null ? null : p.Attribute(mNS + "null").Value, StringComparison.OrdinalIgnoreCase),
                        TypeName = p.Attribute(mNS + "type") == null ? "Edm.String" : p.Attribute(mNS + "type").Value,
                        p.Value
                    };

            foreach (var dp in q)
            {
                entity[dp.Name] = dp.TypeName;
            }
        }
        private void OnReadingEntity1(object sender, ReadingWritingEntityEventArgs args)
        {
            XNamespace Atom = "http://www.w3.org/2005/Atom";
            XNamespace Meta = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
            XNamespace Data = "http://schemas.microsoft.com/ado/2007/08/dataservices";

            var entry = args.Entity as GenericEntity;

            if (entry == null)
                return;

            XElement properties = args.Data
                .Element(Atom + "content")
                .Element(Meta + "properties");

            //select metadata from the extended properties
            entry.ExtendedProperties = (from p in properties.Elements()
                                        where p.Name.Namespace == Data && !IsReservedPropertyName(p.Name.LocalName) && !string.IsNullOrEmpty(p.Value)
                                        select new Tuple<string, string>(p.Name.LocalName, p.Value)).ToArray();
        }
        private void OnWritingEntity(object sender, ReadingWritingEntityEventArgs args)
        {
            XNamespace Atom = "http://www.w3.org/2005/Atom";
            XNamespace Meta = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
            XNamespace Data = "http://schemas.microsoft.com/ado/2007/08/dataservices";

            var entity = args.Entity as GenericEntity;
            if (entity == null)
                return;

            //String combinedName = String.Format("{0}, {1}", city.Name, city.Country);
            //XElement xElement = new XElement(d + "CombinedName", combinedName);
            //XElement properties = args.Data.Descendants(m + "properties").First();
            //properties.Add(xElement);

            XElement Properties = args.Data
                    .Element(Atom + "content")
                    .Element(Meta + "properties");

            //add extended properties from the metadata
            foreach (var p in (from p in entity.properties
                                    where !IsReservedPropertyName(p.Key) && !string.IsNullOrEmpty((string)p.Value)
                                    select p))
            {
                Properties.Add(new XElement(Data + p.Key, p.Value));
            }
        }
示例#18
0
        private void ctx_WritingEntity(object sender, ReadingWritingEntityEventArgs args)
        {
            GenericWriterEntity entity = args.Entity as GenericWriterEntity;
            if (entity == null)
            {
                return;
            }

            XElement properties = args.Data.Descendants(GenericTableReader.MetadataNamespace + "properties").First();

            for(int iColumnn = 0; iColumnn < _edmTypeNames.Length; iColumnn++)
            {
                string edmTypeName = _edmTypeNames[iColumnn];
                if (edmTypeName == null)
                {
                    continue;
                }

                string value = entity._source.Values[iColumnn];
                string columnName = _columnNames[iColumnn];

                // framework will handle row + partition keys.
                XElement e = new XElement(GenericTableReader.DataNamespace + columnName, value);
                e.Add(new XAttribute(GenericTableReader.MetadataNamespace + "type", edmTypeName));

                properties.Add(e);
            }
        }
示例#19
0
        void HandleReadEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            if (!(e.Entity is WadTableEntity))
                return;

            var properties = e.Data.Descendants(XName.Get("properties", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")).FirstOrDefault();
            if (properties == null)
                return;

            var entity = e.Entity as WadTableEntity;
            foreach (var property in properties.Elements())
            {
                entity.Properties.Add(property.Name.LocalName, property.Value);
            }
        }
        private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            var package = (DataServicePackage)e.Entity;

            var downloadUri = e.Data.Element(e.Data.Name.Namespace.GetName("content"))
                .Attribute(System.Xml.Linq.XName.Get("src")).Value;
            package.DownloadUrl = new Uri(downloadUri);
            package.Downloader = _packageDownloader;
        }
示例#21
0
        static void context_WritingEntity(object sender, ReadingWritingEntityEventArgs e)
        {
            TableEntity entity = e.Entity as TableEntity;
            if (entity == null)
                return;

            XElement properties = e.Data.Element(AtomNamespace + "content").Element(AstoriaMetadataNamespace + "properties");

            properties.Element(AstoriaDataNamespace + "Values").Remove();

            foreach (KeyValuePair<string,string> pair in entity.GetProperties())
                properties.Add(new XElement(AstoriaDataNamespace + pair.Key, pair.Value));
        }
 private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs args)
 {
     ICloudMediaContextInit init = args.Entity as ICloudMediaContextInit;
     if (init != null)
     {
         init.InitCloudMediaContext(this._cloudMediaContext);
     }
 }