public static ArraySegment <byte>[] SerializePropertyBag(IDictionary <XName, InstanceValue> properties, InstanceEncodingOption encodingOption)
        {
            ArraySegment <byte>[] dataArrays = new ArraySegment <byte> [4];

            if (properties.Count > 0)
            {
                IObjectSerializer            serializer                   = ObjectSerializerFactory.GetObjectSerializer(encodingOption);
                XmlPropertyBag               primitiveProperties          = new XmlPropertyBag();
                XmlPropertyBag               primitiveWriteOnlyProperties = new XmlPropertyBag();
                Dictionary <XName, object>   complexProperties            = new Dictionary <XName, object>();
                Dictionary <XName, object>   complexWriteOnlyProperties   = new Dictionary <XName, object>();
                Dictionary <XName, object>[] propertyBags                 = new Dictionary <XName, object>[] { primitiveProperties, complexProperties,
                                                                                                               primitiveWriteOnlyProperties, complexWriteOnlyProperties };

                foreach (KeyValuePair <XName, InstanceValue> property in properties)
                {
                    bool isComplex   = (XmlPropertyBag.GetPrimitiveType(property.Value.Value) == PrimitiveType.Unavailable);
                    bool isWriteOnly = (property.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.WriteOnly;
                    int  index       = (isWriteOnly ? 2 : 0) + (isComplex ? 1 : 0);
                    propertyBags[index].Add(property.Key, property.Value.Value);
                }

                // Remove the properties that are already stored as individual columns from the serialized blob
                primitiveWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.StatusPropertyName);
                primitiveWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.LastUpdatePropertyName);
                primitiveWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.PendingTimerExpirationPropertyName);

                complexWriteOnlyProperties.Remove(SqlWorkflowInstanceStoreConstants.BinaryBlockingBookmarksPropertyName);

                for (int i = 0; i < propertyBags.Length; i++)
                {
                    if (propertyBags[i].Count > 0)
                    {
                        if (propertyBags[i] is XmlPropertyBag)
                        {
                            dataArrays[i] = serializer.SerializeValue(propertyBags[i]);
                        }
                        else
                        {
                            dataArrays[i] = serializer.SerializePropertyBag(propertyBags[i]);
                        }
                    }
                }
            }

            return(dataArrays);
        }
        public static ArraySegment <byte> SerializeMetadataPropertyBag(SaveWorkflowCommand saveWorkflowCommand,
                                                                       InstancePersistenceContext context, InstanceEncodingOption instanceEncodingOption)
        {
            IObjectSerializer          serializer             = ObjectSerializerFactory.GetObjectSerializer(instanceEncodingOption);
            Dictionary <XName, object> propertyBagToSerialize = new Dictionary <XName, object>();

            if (context.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None)
            {
                foreach (KeyValuePair <XName, InstanceValue> metadataProperty in context.InstanceView.InstanceMetadata)
                {
                    if ((metadataProperty.Value.Options & InstanceValueOptions.WriteOnly) == 0)
                    {
                        propertyBagToSerialize.Add(metadataProperty.Key, metadataProperty.Value.Value);
                    }
                }
            }

            foreach (KeyValuePair <XName, InstanceValue> metadataChange in saveWorkflowCommand.InstanceMetadataChanges)
            {
                if (metadataChange.Value.IsDeletedValue)
                {
                    if (context.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None)
                    {
                        propertyBagToSerialize.Remove(metadataChange.Key);
                    }
                    else
                    {
                        propertyBagToSerialize[metadataChange.Key] = new DeletedMetadataValue();
                    }
                }
                else if ((metadataChange.Value.Options & InstanceValueOptions.WriteOnly) == 0)
                {
                    propertyBagToSerialize[metadataChange.Key] = metadataChange.Value.Value;
                }
            }

            if (propertyBagToSerialize.Count > 0)
            {
                return(serializer.SerializePropertyBag(propertyBagToSerialize));
            }

            return(new ArraySegment <byte>());
        }
        public static ArraySegment <byte> SerializeKeyMetadata(IDictionary <XName, InstanceValue> metadataProperties, InstanceEncodingOption encodingOption)
        {
            if (metadataProperties != null && metadataProperties.Count > 0)
            {
                Dictionary <XName, object> propertyBag = new Dictionary <XName, object>();

                foreach (KeyValuePair <XName, InstanceValue> property in metadataProperties)
                {
                    if ((property.Value.Options & InstanceValueOptions.WriteOnly) != InstanceValueOptions.WriteOnly)
                    {
                        propertyBag.Add(property.Key, property.Value.Value);
                    }
                }

                IObjectSerializer serializer = ObjectSerializerFactory.GetObjectSerializer(encodingOption);
                return(serializer.SerializePropertyBag(propertyBag));
            }

            return(new ArraySegment <byte>());
        }
        public static ArraySegment <byte> SerializeMetadataPropertyBag(SaveWorkflowCommand saveWorkflowCommand, InstancePersistenceContext context, InstanceEncodingOption instanceEncodingOption)
        {
            IObjectSerializer          objectSerializer = ObjectSerializerFactory.GetObjectSerializer(instanceEncodingOption);
            Dictionary <XName, object> dictionary       = new Dictionary <XName, object>();

            if (context.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None)
            {
                foreach (KeyValuePair <XName, InstanceValue> pair in context.InstanceView.InstanceMetadata)
                {
                    if ((pair.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.None)
                    {
                        dictionary.Add(pair.Key, pair.Value.Value);
                    }
                }
            }
            foreach (KeyValuePair <XName, InstanceValue> pair2 in saveWorkflowCommand.InstanceMetadataChanges)
            {
                if (pair2.Value.IsDeletedValue)
                {
                    if (context.InstanceView.InstanceMetadataConsistency == InstanceValueConsistency.None)
                    {
                        dictionary.Remove(pair2.Key);
                    }
                    else
                    {
                        DeletedMetadataValue value2 = new DeletedMetadataValue();
                        dictionary[pair2.Key] = value2;
                    }
                }
                else if ((pair2.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.None)
                {
                    dictionary[pair2.Key] = pair2.Value.Value;
                }
            }
            if (dictionary.Count > 0)
            {
                return(objectSerializer.SerializePropertyBag(dictionary));
            }
            return(new ArraySegment <byte>());
        }
 public static ArraySegment <byte>[] SerializePropertyBag(IDictionary <XName, InstanceValue> properties, InstanceEncodingOption encodingOption)
 {
     ArraySegment <byte>[] segmentArray = new ArraySegment <byte> [4];
     if (properties.Count > 0)
     {
         IObjectSerializer            objectSerializer = ObjectSerializerFactory.GetObjectSerializer(encodingOption);
         XmlPropertyBag               bag             = new XmlPropertyBag();
         XmlPropertyBag               bag2            = new XmlPropertyBag();
         Dictionary <XName, object>   dictionary      = new Dictionary <XName, object>();
         Dictionary <XName, object>   dictionary2     = new Dictionary <XName, object>();
         Dictionary <XName, object>[] dictionaryArray = new Dictionary <XName, object>[] { bag, dictionary, bag2, dictionary2 };
         foreach (KeyValuePair <XName, InstanceValue> pair in properties)
         {
             bool flag  = XmlPropertyBag.GetPrimitiveType(pair.Value.Value) == PrimitiveType.Unavailable;
             int  index = (((pair.Value.Options & InstanceValueOptions.WriteOnly) == InstanceValueOptions.WriteOnly) ? 2 : 0) + (flag ? 1 : 0);
             dictionaryArray[index].Add(pair.Key, pair.Value.Value);
         }
         bag2.Remove(SqlWorkflowInstanceStoreConstants.StatusPropertyName);
         bag2.Remove(SqlWorkflowInstanceStoreConstants.LastUpdatePropertyName);
         bag2.Remove(SqlWorkflowInstanceStoreConstants.PendingTimerExpirationPropertyName);
         dictionary2.Remove(SqlWorkflowInstanceStoreConstants.BinaryBlockingBookmarksPropertyName);
         for (int i = 0; i < dictionaryArray.Length; i++)
         {
             if (dictionaryArray[i].Count > 0)
             {
                 if (dictionaryArray[i] is XmlPropertyBag)
                 {
                     segmentArray[i] = objectSerializer.SerializeValue(dictionaryArray[i]);
                 }
                 else
                 {
                     segmentArray[i] = objectSerializer.SerializePropertyBag(dictionaryArray[i]);
                 }
             }
         }
     }
     return(segmentArray);
 }