Esempio n. 1
0
 private void SaveFields(XElement element, IModelDefinition modelDefinition, IModelValueGetter getter)
 {
     foreach (IFieldDefinition fieldDefinition in modelDefinition.Fields)
     {
         if (fieldDefinition.Metadata.Get("IsPersistent", true))
         {
             if (getter.TryGetValue(fieldDefinition.Identifier, out object value))
             {
                 if (value != null && converters.TryConvert(value.GetType(), typeof(string), value, out object stringValue))
                 {
                     if (fieldDefinition.Metadata.Get("IsXmlElementContent", false))
                     {
                         string raw = (string)stringValue;
                         if (!String.IsNullOrEmpty(raw) && !String.IsNullOrWhiteSpace(raw))
                         {
                             element.Value = (string)stringValue;
                         }
                     }
                     else
                     {
                         element.SetAttributeValue(fieldDefinition.Identifier, (string)stringValue);
                     }
                 }
             }
         }
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Creates a visualization of the <paramref name="payload"/>.
        /// </summary>
        /// <param name="payload">An event payload to visualize.</param>
        /// <returns>A visualization of the <paramref name="payload"/>.</returns>
        public ObjectVisualization Create(IEvent payload)
        {
            Ensure.NotNull(payload, "payload");
            IModelDefinition payloadDefinition;

            if (!modelDefinitionProvider.TryGet(payload.GetType(), out payloadDefinition))
            {
                Debug.Fail(String.Format("Can't find a model definition for event '{0}'.", payload.GetType()));
                return(null);
            }

            IModelValueGetter            paylodValueGetter = modelValueGetterFactory.Create(payload);
            DictionaryModelValueProvider stringValueGetter = new DictionaryModelValueProvider();

            foreach (IFieldDefinition fieldDefinition in payloadDefinition.Fields)
            {
                object fieldValue;
                if (paylodValueGetter.TryGetValue(fieldDefinition.Identifier, out fieldValue))
                {
                    object rawValue;
                    if (converters.TryConvert(fieldDefinition.FieldType, typeof(string), fieldValue, out rawValue))
                    {
                        stringValueGetter.TrySetValue(fieldDefinition.Identifier, rawValue);
                    }
                    else
                    {
                        stringValueGetter.TrySetValue(fieldDefinition.Identifier, fieldValue);
                    }
                }
            }

            ObjectVisualization model = new ObjectVisualization(payloadDefinition, stringValueGetter, new KeyValueCollection());

            return(model);
        }
        public ICompositeStorage Add(string key, object value)
        {
            object targetValue = null;
            JToken jValue      = null;

            if (value == null)
            {
                jValue = null;
            }
            else if (converters.TryConvert(value.GetType(), typeof(JToken), value, out targetValue))
            {
                jValue = (JToken)targetValue;
            }

            root[key] = jValue;
            return(this);
        }
        public ICompositeStorage Add(string key, object value)
        {
            string rawValue = null;

            if (value != null)
            {
                if (converters.TryConvert(value.GetType(), typeof(string), value, out object converted))
                {
                    rawValue = (string)converted;
                }
                else
                {
                    throw Ensure.Exception.NotSupportedConversion(typeof(string), value);
                }
            }

            root.Values[key] = rawValue;
            return(this);
        }
        public bool TryGet <T>(string key, out T value)
        {
            object rawValue = settings[key];

            if (rawValue == null)
            {
                value = default(T);
                return(false);
            }

            if (converters.TryConvert(rawValue, out value))
            {
                return(true);
            }

            return(false);
        }
        public string Format(string scopeName, LogLevel level, object model)
        {
            object message;

            if (!converts.TryConvert(model.GetType(), typeof(string), model, out message))
            {
                message = model;
            }

            return(String.Format(
                       "{0} {1}({2}){3}{3}{4}{3}",
                       DateTime.Now,
                       scopeName,
                       level.ToString().ToUpperInvariant(),
                       Environment.NewLine,
                       message
                       ));
        }
Esempio n. 7
0
        public bool TryGet <T>(string key, out T value)
        {
            if (ValueProvider.TryGetValue(key, out object rawValue))
            {
                if (rawValue == null)
                {
                    value = default(T);
                    return(true);
                }

                if (converters.TryConvert(rawValue.GetType(), typeof(T), rawValue, out rawValue))
                {
                    value = (T)rawValue;
                    return(true);
                }
            }

            value = default(T);
            return(false);
        }
Esempio n. 8
0
        public bool TryConvert <TSource, TTarget>(TSource sourceValue, out TTarget targetValue)
        {
            Type sourceType = typeof(TSource);
            Type targetType = typeof(TTarget);

            // If target value is assignable from source, no conversion is needed.
            if (targetType.IsAssignableFrom(sourceType))
            {
                targetValue = (TTarget)(object)sourceValue;
                return(true);
            }

            // If source value is null, return default value.
            if (sourceValue == null)
            {
                targetValue = default(TTarget);
                return(true);
            }

            // Find converter, look in storage or find using search handler.
            IConverter converter = null;
            Dictionary <Type, IConverter> sourceStorage;

            if (!storage.TryGetValue(sourceType, out sourceStorage) || !sourceStorage.TryGetValue(targetType, out converter))
            {
                onSearchConverter.TryExecute(new ConverterSearchContext(sourceType, targetType), out converter);
            }

            // If no converter was found, try context converters.
            if (converter == null && !IsConverterContextType(sourceType))
            {
                return(TryConvert <IConverterContext <TSource>, TTarget>(new DefaultConverterContext <TSource>(sourceValue, this), out targetValue));
            }

            // If no converter was found, conversion is not possible.
            if (converter == null)
            {
                if (inner != null)
                {
                    return(inner.TryConvert(sourceValue, out targetValue));
                }

                targetValue = default(TTarget);
                return(false);
            }

            // Try cast to generic converter.
            IConverter <TSource, TTarget> genericConverter = converter as IConverter <TSource, TTarget>;

            if (genericConverter != null)
            {
                try
                {
                    return(genericConverter.TryConvert(sourceValue, out targetValue));
                }
                catch (Exception e)
                {
                    exceptionHandler(e);
                }
            }

            try
            {
                // Convert using general converter.
                object targetObject;
                if (converter.TryConvert(sourceType, targetType, sourceValue, out targetObject))
                {
                    targetValue = (TTarget)targetObject;
                    return(true);
                }
            }
            catch (Exception e)
            {
                exceptionHandler(e);
            }

            // No other options for conversion.
            targetValue = default(TTarget);
            return(false);
        }