Inheritance: IDictionaryKeyBuilder, IDictionaryPropertyGetter, IDictionaryPropertySetter
		private void MergeBehaviorOverrides(DictionaryAdapterMeta meta)
		{
			if (Descriptor == null) return;

			var typeDescriptor = Descriptor as DictionaryDescriptor;

			if (typeDescriptor != null)
			{
				Initializers = Initializers.Prioritize(typeDescriptor.Initializers).ToArray();
			}

			Properties = new Dictionary<string, PropertyDescriptor>();

			foreach (var property in meta.Properties)
			{
				var propertyDescriptor = property.Value;

				var propertyOverride = new PropertyDescriptor(propertyDescriptor, false)
					.AddKeyBuilders(propertyDescriptor.KeyBuilders.Prioritize(Descriptor.KeyBuilders))
					.AddGetters(propertyDescriptor.Getters.Prioritize(Descriptor.Getters))
					.AddSetters(propertyDescriptor.Setters.Prioritize(Descriptor.Setters));

				Properties.Add(property.Key, propertyOverride);
			}
		}
		public DictionaryAdapterInstance(IDictionary dictionary, PropertyDescriptor descriptor, 
										 IDictionaryAdapterFactory factory)
		{
			Dictionary = dictionary;
			Descriptor = descriptor;
			Factory = factory;
		}
		bool IDictionaryPropertySetter.SetPropertyValue(IDictionaryAdapter dictionaryAdapter,
			string key, ref object value, PropertyDescriptor property)
		{
			if (value != null)
			{
				value = GetPropertyAsString(property, value);
			}
			return true;
		}
Beispiel #4
0
        public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, object storedValue,
                                       PropertyDescriptor descriptor, bool ifExists)
        {
            var attr = descriptor.Property.GetCustomAttribute <SettingsAttribute>();

            if (attr != null)
            {
                var behavior = new SettingsBehavior()
                {
                    KeyPrefix       = key,
                    PrefixSeparator = this.PrefixSeparator                     //attr.PrefixSeparator ?? DEFAULT_PREFIX_SEPARATOR
                };
                var desc = new PropertyDescriptor(new[] { behavior });
                desc.AddBehavior(behavior);

                storedValue = dictionaryAdapter.This.Factory.GetAdapter(descriptor.PropertyType, dictionaryAdapter.This.Dictionary, desc);
            }

            if (ValueIsNullOrDefault(descriptor, storedValue))
            {
                var defaultValue = descriptor.Annotations.OfType <DefaultValueAttribute>().SingleOrDefault();

                if (IsRequired(descriptor, ifExists))
                {
                    throw new ValidationException(string.Format("No valid value for '{0}' found", key));
                }
                else if (defaultValue != null)
                {
                    storedValue = defaultValue.Value;
                }
            }

            // Convert value if needed.
            if (storedValue != null && !descriptor.PropertyType.IsAssignableFrom(storedValue.GetType()))
            {
                storedValue = descriptor.TypeConverter.CanConvertFrom(storedValue.GetType()) ?
                              descriptor.TypeConverter.ConvertFrom(storedValue)
                                        : Convert.ChangeType(storedValue, descriptor.PropertyType);
            }

#if !USE_DAVALIDATOR
            if (storedValue != null)
            {
                var propinfo = descriptor.Property;
                var context  = new ValidationContext(storedValue)
                {
                    DisplayName = propinfo.Name,
                    MemberName  = propinfo.Name
                };
                {
                    var attrs = propinfo.GetCustomAttributes(true).OfType <ValidationAttribute>().ToArray();
                    Validator.ValidateValue(storedValue, context, attrs);
                }
            }
#endif
            return(storedValue);
        }
 public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, object storedValue, PropertyDescriptor property, bool ifExists)
 {
     if (storedValue != null)
     {
         return storedValue;
     }
     
     throw new InvalidOperationException(string.Format("App setting '{0}' not found!", key));
 }
		public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter,
			string key, object storedValue, PropertyDescriptor property, bool ifExists)
		{
			if (storedValue == null || storedValue.Equals(UnassignedGuid))
			{
				storedValue = Guid.NewGuid();
				property.SetPropertyValue(dictionaryAdapter, key, ref storedValue, dictionaryAdapter.This.Descriptor);
			}

			return storedValue;
		}
		public DictionaryAdapterInstance(IDictionary dictionary, DictionaryAdapterMeta meta,
										 PropertyDescriptor descriptor, IDictionaryAdapterFactory factory)
		{
			Dictionary = dictionary;
			Descriptor = descriptor;
			Factory = factory;

			Properties = meta.Properties;
			Initializers = meta.Initializers;
			MergeBehaviorOverrides(meta);
		}
Beispiel #8
0
        public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, object storedValue, PropertyDescriptor property, bool ifExists)
        {
            if (property.PropertyType.IsAssignableFrom(storedValue.GetType()))
            {
                return storedValue;
            }

            if (property.PropertyType.IsInterface && IsDictionary(storedValue.GetType()))
            {
                return dictionaryAdapter.This.Factory.GetAdapter(property.PropertyType, storedValue as IDictionary);
            }

            return storedValue;
        }
        public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, object storedValue,
            PropertyDescriptor property, bool ifExists)
        {
            var defaultValue = property.Annotations
                .OfType<DefaultValueAttribute>()
                .SingleOrDefault();

            if (storedValue == null && IsRequired(ifExists) && defaultValue == null)
                throw new KeyNotFoundException("key '" + key + "' not found");

            if (storedValue == null && defaultValue != null)
                return defaultValue.Value;

            return storedValue;
        }
		object IDictionaryPropertyGetter.GetPropertyValue(
			IDictionaryAdapterFactory factory, IDictionary dictionary,
			string key, object storedValue, PropertyDescriptor property)
		{
			if (storedValue == null)
			{
				PropertyDescriptor descriptor = 
					new PropertyDescriptor(property.Property);
				descriptor.AddKeyBuilder(new DictionaryKeyPrefixAttribute(key));

				return factory.GetAdapter(
					property.Property.PropertyType, dictionary, descriptor);
			}

			return storedValue;
		}
		private string GetPropertyAsString(PropertyDescriptor property, object value)
		{
			if (string.IsNullOrEmpty(Format) == false)
			{
				return String.Format(Format, value);
			}

			var converter = property.TypeConverter;

			if (converter != null && converter.CanConvertTo(typeof(string)))
			{
				return (string) converter.ConvertTo(value, typeof(string));
			}

			return value.ToString();
		}
Beispiel #12
0
        public string Validate(IDictionaryAdapter dictionaryAdapter, PropertyDescriptor property)
        {
            var key      = dictionaryAdapter.GetKey(property.PropertyName);
            var value    = dictionaryAdapter.GetProperty(property.PropertyName, true);
            var propinfo = property.Property;
            var context  = new ValidationContext(value)
            {
                DisplayName = key,
                MemberName  = propinfo.Name
            };

            var attrs   = propinfo.GetCustomAttributes(true).OfType <ValidationAttribute>().ToArray();
            var results = new System.Collections.Generic.List <ValidationResult>();

            return(Validator.TryValidateValue(value, context, results, attrs) ?
                   String.Empty
                                : string.Join(Environment.NewLine, results.Select(x => x.ErrorMessage)));
        }
		public DictionaryAdapterInstance(IDictionary dictionary, DictionaryAdapterMeta meta,
										 PropertyDescriptor descriptor, IDictionaryAdapterFactory factory)
		{
			Dictionary = dictionary;
			Descriptor = descriptor;
			Factory    = factory;

			List<IDictionaryBehavior> behaviors;

			if (null == descriptor || null == (behaviors = descriptor.BehaviorsInternal))
			{
				Initializers = meta.Initializers;
				Properties   = MergeProperties(meta.Properties);
			}
			else
			{
				Initializers = MergeInitializers(meta.Initializers, behaviors);
				Properties   = MergeProperties(meta.Properties, behaviors);
			}
		}
		object IDictionaryPropertyGetter.GetPropertyValue(IDictionaryAdapter dictionaryAdapter,
			string key, object storedValue, PropertyDescriptor property, bool ifExists)
		{
			if (storedValue == null)
			{
				var component = dictionaryAdapter.This.ExtendedProperties[property.PropertyName];

				if (component == null)
				{
					var descriptor = new PropertyDescriptor(property.Property, null);
					descriptor.AddKeyBuilder(new KeyPrefixAttribute(key));
					component = dictionaryAdapter.This.Factory.GetAdapter(
						property.Property.PropertyType, dictionaryAdapter.This.Dictionary, descriptor);
					dictionaryAdapter.This.ExtendedProperties[property.PropertyName] = component;
				}

				return component;
			}

			return storedValue;
		}
		private static bool IsCollection(PropertyDescriptor property, out Type collectionItemType)
		{
			collectionItemType = null;
			var propertyType = property.PropertyType;
			if (propertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(propertyType))
			{
				if (propertyType.IsArray)
				{
					collectionItemType = propertyType.GetElementType();
				}
				else if (propertyType.IsGenericType)
				{
					var arguments = propertyType.GetGenericArguments();
					collectionItemType = arguments[0];
				}
				else
				{
					collectionItemType = typeof(object);
				}
				return true;
			}
			return false;
		}
		public bool SetPropertyValue(IDictionaryAdapter dictionaryAdapter,
			string key, ref object value, PropertyDescriptor property)
		{
			dictionaryAdapter.This.ExtendedProperties.Remove(property.PropertyName);
			return true;
		}
Beispiel #17
0
        public object CreateInstance(IDictionary dictionary, PropertyDescriptor descriptor)
        {
            var instance = new DictionaryAdapterInstance(dictionary, this, descriptor, Factory);

            return(creator(instance));
        }
		public override PropertyDescriptor CopyBehaviors(PropertyDescriptor other)
		{
			if (other is DictionaryDescriptor)
			{
				var otherDict = (DictionaryDescriptor)other;
				CopyMetaInitializers(otherDict).CopyInitializers(otherDict);
			}
			return base.CopyBehaviors(other);
		}
		String IDictionaryKeyBuilder.GetKey(IDictionary dictionary, String key,
		                                   PropertyDescriptor property)
		{
			return keyPrefix + key;
		}
Beispiel #20
0
		private static bool IsVolatileProperty(IDictionaryAdapter dictionaryAdapter, PropertyDescriptor property)
		{
			return dictionaryAdapter.Meta.Behaviors.Union(property.Behaviors).Any(behavior => behavior is VolatileAttribute);
		}
		public bool SetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, ref object value, PropertyDescriptor property)
		{
			value = (value != null) ? value.ToString() : null;
			return true;
		}
Beispiel #22
0
		object IDictionaryPropertyGetter.GetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, object storedValue,
														  PropertyDescriptor property, bool ifExists)
		{
			if (ShouldIgnoreProperty(property) == false &&
				(storedValue == null || IsVolatileProperty(dictionaryAdapter, property)))
			{
				var result = EvaluateProperty(key, property, dictionaryAdapter);
				storedValue = ReadProperty(result, ifExists, dictionaryAdapter);
				if (storedValue != null)
				{
					dictionaryAdapter.StoreProperty(property, key, storedValue);
				}
			}
			return storedValue;
		}
Beispiel #23
0
		private XPathResult EvaluateProperty(string key, PropertyDescriptor property, IDictionaryAdapter dictionaryAdapter)
		{
			object result;
			XPathExpression xpath = null;
			object matchingBehavior = null;
			Func<XPathNavigator> create = null;

			var context = GetEffectiveContext(dictionaryAdapter);
			var xmlMeta = dictionaryAdapter.GetXmlMeta(property.Property.DeclaringType);
			var keyContext = context.CreateChild(xmlMeta, property.Behaviors);

			foreach (var behavior in property.Behaviors)
			{
				string name = key, ns = null;
				Func<XPathNavigator> matchingCreate = null;

				if (behavior is XmlElementAttribute)
				{
					xpath = XPathElement;
					var attrib = (XmlElementAttribute)behavior;
					if (string.IsNullOrEmpty(attrib.ElementName) == false)
						name = attrib.ElementName;
					if (string.IsNullOrEmpty(attrib.Namespace) == false)
						ns = attrib.Namespace;
					matchingCreate = () => keyContext.AppendElement(name, ns, root.Clone());
				}
				else if (behavior is XmlAttributeAttribute)
				{
					xpath = XPathAttribute;
					var attrib = (XmlAttributeAttribute)behavior;
					if (string.IsNullOrEmpty(attrib.AttributeName) == false)
						name = attrib.AttributeName;
					if (string.IsNullOrEmpty(attrib.Namespace) == false)
						ns = attrib.Namespace;
					matchingCreate = () => keyContext.CreateAttribute(name, ns, root.Clone());
				}
				else if (behavior is XmlArrayAttribute)
				{
					xpath = XPathElement;
					var attrib = (XmlArrayAttribute)behavior;
					if (string.IsNullOrEmpty(attrib.ElementName) == false)
						name = attrib.ElementName;
					if (string.IsNullOrEmpty(attrib.Namespace) == false)
						ns = attrib.Namespace;
					matchingCreate = () => keyContext.AppendElement(name, ns, root.Clone());
				}
				else if (behavior is XPathAttribute)
				{
					var attrib = (XPathAttribute)behavior;
					xpath = attrib.CompiledExpression;
				}
				else
				{
					continue;
				}

				if (xpath != null)
				{
					keyContext.Arguments.Clear();
					keyContext.Arguments.AddParam("key", "", name);
					keyContext.Arguments.AddParam("ns", "", ns ?? XPathContext.IgnoreNamespace);
					if (keyContext.Evaluate(xpath, Root, out result))
					{
						create = matchingCreate ?? create;
						return new XPathResult(property, key, result, keyContext, behavior, create);
					}
				}

				matchingBehavior = matchingBehavior ?? behavior;
				create = create ?? matchingCreate;
			}

			if (xpath != null)
				return new XPathResult(property, key, null, keyContext, matchingBehavior, create);

			keyContext.Arguments.Clear();
			keyContext.Arguments.AddParam("key", "", key);
			keyContext.Arguments.AddParam("ns", "", XPathContext.IgnoreNamespace);
			create = create ?? (() => keyContext.AppendElement(key, null, root));
			keyContext.Evaluate(XPathElementOrAttribute, Root, out result);
			return new XPathResult(property, key, result, keyContext, null, create);
		}
		string IDictionaryKeyBuilder.GetKey(IDictionaryAdapter dictionaryAdapter, String key, PropertyDescriptor property)
		{
			return key.Replace(oldValue, newValue);
		}
        bool IDictionaryPropertySetter.SetPropertyValue(IDictionaryAdapter dictionaryAdapter,
                                                        string key, ref object value, PropertyDescriptor property)
        {
            var enumerable = value as IEnumerable;

            if (enumerable != null)
            {
                value = BuildString(enumerable, Separator);
            }
            return(true);
        }
Beispiel #26
0
 private bool ValueIsNullOrDefault(PropertyDescriptor descriptor, object value)
 {
     return(descriptor.PropertyType.IsByRef ?
            Activator.CreateInstance(descriptor.PropertyType) == value
                         : value == null);
 }
Beispiel #27
0
 private static bool IsRequired(PropertyDescriptor property, bool ifExists)
 {
     return(property.Annotations.Any(x => x is RequiredAttribute) && ifExists == false);
 }
        object IDictionaryPropertyGetter.GetPropertyValue(IDictionaryAdapter dictionaryAdapter,
                                                          string key, object storedValue, PropertyDescriptor property, bool ifExists)
        {
            var propertyType = property.PropertyType;

            if (storedValue == null || !storedValue.GetType().IsInstanceOfType(propertyType))
            {
                if (propertyType.IsGenericType)
                {
                    var genericDef = propertyType.GetGenericTypeDefinition();

                    if (genericDef == typeof(IList <>) || genericDef == typeof(ICollection <>) ||
                        genericDef == typeof(List <>) || genericDef == typeof(IEnumerable <>))
                    {
                        var paramType = propertyType.GetGenericArguments()[0];
                        var converter = TypeDescriptor.GetConverter(paramType);

                        if (converter != null && converter.CanConvertFrom(typeof(string)))
                        {
                            var genericList = typeof(StringListWrapper <>).MakeGenericType(new[] { paramType });
                            return(Activator.CreateInstance(genericList, key, storedValue, Separator, dictionaryAdapter.This.Dictionary));
                        }
                    }
                }
            }

            return(storedValue);
        }
		object IDictionaryPropertyGetter.GetPropertyValue(IDictionaryAdapter dictionaryAdapter,
			string key, object storedValue, PropertyDescriptor property)
		{
			return string.Format(Format, GetFormatArguments(dictionaryAdapter, property.Property.Name)).Trim();
		}
Beispiel #30
0
 private string GetPrefixFor(PropertyDescriptor property)
 {
     return(string.IsNullOrEmpty(KeyPrefix) ?
            (property.Property.DeclaringType.Namespace + PrefixSeparator)
                         : (KeyPrefix + PrefixSeparator));
 }
Beispiel #31
0
		bool IDictionaryPropertySetter.SetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, ref object value, PropertyDescriptor property)
		{
			if (ShouldIgnoreProperty(property) == false)
			{
				EnsureOffRoot();

				if (root.CanEdit)
				{
					var result = EvaluateProperty(key, property, dictionaryAdapter);
					if (result.CanWrite)
					{
						WriteProperty(result, ref value, dictionaryAdapter);
					}
				}
			}
			return true;
		}
Beispiel #32
0
 public string GetKey(IDictionaryAdapter dictionaryAdapter, string key, PropertyDescriptor property)
 {
     return(GetPrefixFor(property) + key);
 }
Beispiel #33
0
		private static bool ShouldIgnoreProperty(PropertyDescriptor property)
		{
			return property.Behaviors.Any(behavior => behavior is XmlIgnoreAttribute);
		}
Beispiel #34
0
 public void Initialize(PropertyDescriptor propertyDescriptor, object[] behaviors)
 {
     propertyDescriptor.Fetch = true;
 }
		String IDictionaryKeyBuilder.GetKey(IDictionaryAdapter dictionaryAdapter, String key, PropertyDescriptor property)
		{
			return String.Format("{0}#{1}", property.Property.DeclaringType.FullName, key);
		}
Beispiel #36
0
 public Edit(PropertyDescriptor property, object propertyValue)
 {
     Property      = property;
     PropertyValue = propertyValue;
 }
Beispiel #37
0
 /// <summary>
 /// Copies the behaviors to the other <see cref="PropertyDescriptor"/>
 /// </summary>
 /// <param name="other"></param>
 /// <returns></returns>
 public virtual PropertyDescriptor CopyBehaviors(PropertyDescriptor other)
 {
     return(CopyKeyBuilders(other).CopyGetters(other).CopySetters(other));
 }
		public override PropertyDescriptor CopyBehaviors(PropertyDescriptor other, Func<IDictionaryBehavior, bool> selector)
		{
			if (other is DictionaryDescriptor)
			{
				var otherDict = (DictionaryDescriptor)other;
				CopyMetaInitializers(otherDict, selector).CopyInitializers(otherDict, selector);
			}
			return base.CopyBehaviors(other, selector);
		}
		String IDictionaryKeyBuilder.GetKey(IDictionary dictionary, String key,
		                                   PropertyDescriptor property)
		{
			return property.Property.DeclaringType.FullName + "#" + key;
		}
		public void Initialize(PropertyDescriptor propertyDescriptor, object[] behaviors)
		{
			propertyDescriptor.SuppressNotifications = true;
		}
Beispiel #41
0
 bool IDictionaryPropertySetter.SetPropertyValue(IDictionaryAdapter dictionaryAdapter,
                                                 string key, ref object value, PropertyDescriptor property)
 {
     if (value != null)
     {
         value = GetPropertyAsString(property, value);
     }
     return(true);
 }
		string IDictionaryKeyBuilder.GetKey(IDictionaryAdapter dictionaryAdapter, string key,
		                                    PropertyDescriptor property)
		{
			return prefix ?? key + "_";
		}
		public override PropertyDescriptor CopyBehaviors(PropertyDescriptor other, Func<IDictionaryBehavior, bool> selector)
		{
			if (other is DictionaryDescriptor)
			{
				var otherDict = (DictionaryDescriptor)other;
#if DOTNET35 || SL4 || MONO
				// no co-contra variance support in .NET 3.5, SL4 or mono
				CopyMetaInitializers(otherDict, i => selector(i)).CopyInitializers(otherDict, i => selector(i));
#else
				CopyMetaInitializers(otherDict, selector).CopyInitializers(otherDict, selector);
#endif
			}
			return base.CopyBehaviors(other, selector);
		}
 public bool ShouldClearProperty(PropertyDescriptor property, object value)
 {
     return(property == null ||
            property.Setters.OfType <RemoveIfAttribute>().Where(remove => remove.ShouldRemove(value)).Any());
 }