Example #1
0
        /// <summary>
        /// Serializes an entity using the XmlSerializer.
        /// </summary>
        /// <param name="serviceBusHelper">A ServiceBusHelper object.</param>
        /// <param name="xmlWriter">The XmlWriter object to use.</param>
        /// <param name="entity">The entity to serialize.</param>
        /// <returns>A XML string.</returns>
        private static void SerializeEntity(ServiceBusHelper serviceBusHelper, XmlWriter xmlWriter, object entity)
        {
            if (xmlWriter == null ||
                entity == null)
            {
                return;
            }
            var type     = entity.GetType();
            var typeName = type.Name;

            if (type.FullName == null || !propertyCache.ContainsKey(type.FullName))
            {
                return;
            }
            var propertyDictionary = propertyCache[type.FullName];

            xmlWriter.WriteStartElement(MapClassToEntity(type));
            foreach (var keyValuePair in propertyDictionary)
            {
                var value = keyValuePair.Value.GetValue(entity, null);
                xmlWriter.WriteStartElement(keyValuePair.Value.Name);
                if (value is Filter || value is RuleAction)
                {
                    SerializeEntity(serviceBusHelper, xmlWriter, value);
                }
                else
                {
                    xmlWriter.WriteString(string.Format(EntityFormat, value));
                }
                xmlWriter.WriteEndElement();
            }
            if (entity is TopicDescription)
            {
                var topic            = entity as TopicDescription;
                var subscriptionList = serviceBusHelper.GetSubscriptions(topic.Path);
                if (subscriptionList.Any())
                {
                    xmlWriter.WriteStartElement(SubscriptionEntityList);
                    foreach (var subscription in subscriptionList)
                    {
                        SerializeEntity(serviceBusHelper, xmlWriter, subscription);
                    }
                    xmlWriter.WriteEndElement();
                }
            }
            if (entity is SubscriptionDescription)
            {
                var subscription = entity as SubscriptionDescription;
                var ruleList     = serviceBusHelper.GetRules(subscription.TopicPath, subscription.Name);
                if (ruleList.Any())
                {
                    xmlWriter.WriteStartElement(RuleEntityList);
                    foreach (var rule in ruleList)
                    {
                        SerializeEntity(serviceBusHelper, xmlWriter, rule);
                    }
                    xmlWriter.WriteEndElement();
                }
            }
            xmlWriter.WriteEndElement();
            switch (typeName)
            {
            case QueueDescriptionClass:
                var queueDescription = entity as QueueDescription;
                if (queueDescription != null)
                {
                    MainForm.StaticWriteToLog(string.Format(QueueExported, queueDescription.Path));
                }
                break;

            case TopicDescriptionClass:
                var topicDescription = entity as TopicDescription;
                if (topicDescription != null)
                {
                    MainForm.StaticWriteToLog(string.Format(TopicExported, topicDescription.Path));
                }
                break;

            case SubscriptionDescriptionClass:
                var subscriptionDescription = entity as SubscriptionDescription;
                if (subscriptionDescription != null)
                {
                    MainForm.StaticWriteToLog(string.Format(SubscriptionExported,
                                                            subscriptionDescription.Name,
                                                            subscriptionDescription.TopicPath));
                }
                break;

            case RuleDescriptionClass:
                var ruleDescription = entity as RuleDescription;
                if (ruleDescription != null)
                {
                    MainForm.StaticWriteToLog(string.Format(RuleExported,
                                                            ruleDescription.Name));
                }
                break;
            }
        }