Beispiel #1
0
		void IDictionaryInitializer.Initialize(IDictionaryAdapter dictionaryAdapter, object[] behaviors)
		{
			var meta = dictionaryAdapter.Meta;
			if (meta.MetaInitializers.OfType<XPathBehavior>().FirstOrDefault() == null)
			{
				throw new InvalidOperationException(string.Format(
					"Interface {0} requested xpath support, but was not configured properly.  " +
					"Did you forget to add an XPathBehavior?", meta.Type.FullName));
			}

			var xmlMeta = dictionaryAdapter.GetXmlMeta();

			if (dictionaryAdapter.This.CreateStrategy == null)
			{
				dictionaryAdapter.This.CreateStrategy = this;
				dictionaryAdapter.This.AddCopyStrategy(this);
			}

			if (rootXmlMeta == null)
			{
				rootXmlMeta = xmlMeta;
				Context.ApplyBehaviors(rootXmlMeta, behaviors);

				if (Parent == null)
				{
					foreach (var behavior in behaviors)
					{
						if (behavior is XPathAttribute)
						{
							var attrib = (XPathAttribute)behavior;
							var compiledExpression = attrib.CompiledExpression;
							if (MoveOffRoot(root, XPathNodeType.Element) == false || Context.Matches(compiledExpression, root))
							{
								break;
							}

							var navigator = Context.SelectSingleNode(compiledExpression, root);
							if (navigator != null)
							{
								root = navigator;
								break;
							}
						}
					}
					MoveOffRoot(root, XPathNodeType.Element);
				}
			}
			else
			{
				if (overlays == null)
					overlays = new Dictionary<Type, XPathContext>();

				XPathContext overlay;
				if (overlays.TryGetValue(meta.Type, out overlay) == false)
				{
					overlay = new XPathContext().ApplyBehaviors(xmlMeta, behaviors);
					overlays.Add(meta.Type, overlay);
				}
			}
		}
		public override void SetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor, object oldValue, ref object value)
		{
			var newNode = (XmlNode) value;

			using (var writer = new XmlSubtreeWriter(node))
				newNode.WriteTo(writer);
		}
		private object GetValueCore(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor)
		{
			var itemType    = node.ClrType.GetGenericArguments()[0];
			var listType    = ListTypeConstructor.MakeGenericType(itemType);
			var subaccessor = accessor.GetCollectionAccessor(itemType);
			return Activator.CreateInstance(listType, node, parent, subaccessor);
		}
Beispiel #4
0
		public override void SetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor, ref object value)
		{
			var source      = (Array) value;
			var target      = (Array) null;
			var itemType    = source.GetType().GetElementType();
			var subaccessor = accessor.GetCollectionAccessor(itemType);
			var cursor      = subaccessor.SelectCollectionItems(node, true);
			var serializer  = subaccessor.Serializer;
			var references  = XmlAdapter.For(parent).References;

			for (var i = 0; i < source.Length; i++)
			{
				var originalItem = source.GetValue(i);
				var assignedItem = originalItem;

				subaccessor.SetValue(cursor, parent, references, cursor.MoveNext(), null /* TODO: Get Value */, ref assignedItem);

				if (target != null)
				{
					target.SetValue(assignedItem, i);
				}
				else if (!Equals(assignedItem, originalItem))
				{
					target = Array.CreateInstance(itemType, source.Length);
					Array.Copy(source, target, i);
					target.SetValue(assignedItem, i);
				}
			}

			cursor.RemoveAllNext();

			if (target != null)
				value = target;
		}
		public override void SetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor, object oldValue, ref object value)
		{
			if (node.ClrType != typeof(object))
				XmlTypeSerializer.For(node.ClrType).SetValue(node, parent, accessor, oldValue, ref value);
			else
				node.Clear();
		}
Beispiel #6
0
		public override object GetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor)
		{
            using (var reader = new XmlSubtreeReader(node, Root))
                return serializer.CanDeserialize(reader)
                    ? serializer.Deserialize(reader)
                    : null;
		}
		public void CopyTo(IDictionaryAdapter other, Func<PropertyDescriptor, bool> selector)
		{
			if (ReferenceEquals(this, other))
			{
				return;
			}

			if (other.Meta.Type.IsAssignableFrom(Meta.Type) == false)
			{
				throw new ArgumentException(string.Format(
					"Unable to copy to {0}.  The type must be assignable from {1}.",
					other.Meta.Type.FullName, Meta.Type.FullName));
			}

			if (This.CopyStrategies.Aggregate(false, (copied, s) => copied | s.Copy(this, other, ref selector)))
			{
				return;
			}

			selector = selector ?? (property => true);

			foreach (var property in This.Properties.Values.Where(property => selector(property)))
			{
				var propertyValue = GetProperty(property.PropertyName, true);
				other.SetProperty(property.PropertyName, ref propertyValue);
			}
		}
		public override object GetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor)
		{
			var source = node.AsRealizable<XmlNode>();

			return (source != null && source.IsReal)
				? source.Value
				: null;
		}
		bool IDictionaryPropertySetter.SetPropertyValue(IDictionaryAdapter dictionaryAdapter,
			string key, ref object value, PropertyDescriptor property)
		{
			if (value != null)
			{
				value = GetPropertyAsString(property, value);
			}
			return true;
		}
 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));
 }
Beispiel #11
0
		private static DictionaryAdapterMeta GetDictionaryMeta(IDictionaryAdapter dictionaryAdapter, Type otherType)
		{
			var meta = dictionaryAdapter.Meta;
			if (otherType != null && otherType != meta.Type)
			{
				meta = dictionaryAdapter.This.Factory.GetAdapterMeta(otherType,
					new DictionaryDescriptor().AddBehavior(XPathBehavior.Instance));
			}
			return meta;
		}
		object IDictionaryCreateStrategy.Create(IDictionaryAdapter adapter, Type type, IDictionary dictionary)
		{
			dictionary = dictionary ??
#if SILVERLIGHT
			             new Dictionary<object, object>();
#else
			             new Hashtable();
#endif
			return adapter.This.Factory.GetAdapter(type, dictionary, adapter.This.Descriptor); ;
		}
		public bool Equals(IDictionaryAdapter adapter1, IDictionaryAdapter adapter2)
		{
			var id1 = adapter1.GetProperty("Id", false);
			if (id1 == null) return false;

			var id2 = adapter2.GetProperty("Id", false);
			if (id2 == null) return false;

			return id1.Equals(id2);
		}
		private void ApplyValidationRules(IDictionaryAdapter dictionaryAdapter, IEnumerable<IValidationRule> rules,
										  PropertyDescriptor property, object propertyValue, IList<String> errors)
		{
			if (rules != null)
			{
				foreach (var rule in rules)
				{
					rule.Apply(dictionaryAdapter, property, propertyValue, errors);
				}
			}
		}
		public bool GetHashCode(IDictionaryAdapter adapter, out int hashCode)
		{
			var id = adapter.GetProperty("Id", false);
			if (id != null)
			{
				hashCode = id.GetHashCode();
				return true;
			}
			hashCode = 0;
			return false;
		}
		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;
		}
Beispiel #17
0
		private static DictionaryAdapterMeta GetDictionaryMeta(IDictionaryAdapter dictionaryAdapter, Type otherType)
		{
			var meta = dictionaryAdapter.Meta;
			if (otherType != null && otherType != meta.Type)
			{
				var descriptor = new DictionaryDescriptor();
				dictionaryAdapter.This.Descriptor.CopyBehaviors(descriptor);
				meta = dictionaryAdapter.This.Factory.GetAdapterMeta(otherType, descriptor);
			}
			return meta;
		}
		public string Validate(IDictionaryAdapter dictionaryAdapter, PropertyDescriptor property)
		{
			List<String> errors = new List<string>();
			var globalRules = AttributesUtil.GetTypeAttributes<IValidationRule>(dictionaryAdapter.Meta.Type);

			var propertyRules = AttributesUtil.GetAttributes<IValidationRule>(property.Property);
			var propertyValue = dictionaryAdapter.GetProperty(property.PropertyName, true);
			ApplyValidationRules(dictionaryAdapter, propertyRules, property, propertyValue, errors);
			ApplyValidationRules(dictionaryAdapter, globalRules, property, propertyValue, errors);

			return String.Join(Environment.NewLine, errors.ToArray());
		}
Beispiel #19
0
		public override object GetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor)
		{
			var items      = new ArrayList();
			var itemType   = node.ClrType.GetElementType();
			var references = XmlAdapter.For(parent).References;

			accessor
				.GetCollectionAccessor(itemType)
				.GetCollectionItems(node, parent, references, items);

			return items.ToArray(itemType);
		}
Beispiel #20
0
		public override void SetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor, object oldValue, ref object value)
		{
		    var serializable = (IXmlSerializable) value;
			var root = XmlDefaultSerializer.Root;

			using (var writer = new XmlSubtreeWriter(node))
			{
				// Pre-write containing element
				writer.WriteStartElement(string.Empty, root.ElementName, root.Namespace);
				serializable.WriteXml(writer);
				writer.WriteEndElement();
			}
		}
Beispiel #21
0
		public override object GetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor)
		{
            var serializable = (IXmlSerializable) Activator.CreateInstance(node.ClrType);

			using (var reader = new XmlSubtreeReader(node, XmlDefaultSerializer.Root))
			{
				// Do NOT pre-read containing element
				// ...IXmlSerializable is not a symmetric contract
				serializable.ReadXml(reader);
			}

            return serializable;
		}
Beispiel #22
0
		public override void SetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor, ref object value)
		{
			var current = value as IXmlCollection;
			if (current != null && current.Node.PositionEquals(node))
				return;

			var source = value as IEnumerable;
			if (source == null)
				throw Error.NotSupported();

			var collection = (IXmlCollection) GetValue(node, parent, accessor);
			collection.Replace(source);
			value = collection;
		}
Beispiel #23
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 string Validate(IDictionaryAdapter dictionaryAdapter)
		{
			List<String> errors = new List<string>();
			var globalRules = AttributesUtil.GetTypeAttributes<ValidationRuleAttribute>(dictionaryAdapter.Meta.Type);

			foreach (var property in dictionaryAdapter.This.Properties.Values)
			{
				var propertyRules = AttributesUtil.GetAttributes<ValidationRuleAttribute>(property.Property).Select(x => (IValidationRule)x);
				var propertyValue = dictionaryAdapter.GetProperty(property.PropertyName, true);
				ApplyValidationRules(dictionaryAdapter, propertyRules, property, propertyValue, errors);
				ApplyValidationRules(dictionaryAdapter, globalRules, property, propertyValue, errors);
			}

			return String.Join(Environment.NewLine, errors.ToArray());
		}
Beispiel #25
0
        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
        public string Validate(IDictionaryAdapter dictionaryAdapter)
        {
            List <string> errors      = new List <string>();
            var           globalRules = AttributesUtil.GetTypeAttributes <ValidationRuleAttribute>(dictionaryAdapter.Meta.Type);

            foreach (var property in dictionaryAdapter.This.Properties.Values)
            {
                var propertyRules = AttributesUtil.GetAttributes <ValidationRuleAttribute>(property.Property).Select(x => (IValidationRule)x);
                var propertyValue = dictionaryAdapter.GetProperty(property.PropertyName, true);
                ApplyValidationRules(dictionaryAdapter, propertyRules, property, propertyValue, errors);
                ApplyValidationRules(dictionaryAdapter, globalRules, property, propertyValue, errors);
            }

            return(string.Join(Environment.NewLine, errors.ToArray()));
        }
Beispiel #27
0
        private void HandleListChanged(object value, ListChangedEventArgs args, IDictionaryAdapter dictionaryAdapter, PropertyDescriptor property)
        {
            var change = args.ListChangedType;
            var changed
                = change == ListChangedType.ItemAdded ||
                  change == ListChangedType.ItemDeleted ||
                  change == ListChangedType.ItemMoved ||
                  change == ListChangedType.Reset;

            if (changed && dictionaryAdapter.ShouldClearProperty(property, value))
            {
                value = null;
                dictionaryAdapter.SetProperty(property.PropertyName, ref value);
            }
        }
Beispiel #28
0
        private object ReadArray(XPathResult result, IDictionaryAdapter dictionaryAdapter)
        {
            var itemType  = result.Type.GetElementType();
            var itemNodes = result.GetNodes(itemType, type => dictionaryAdapter.GetXmlMeta(type));

            if (itemNodes == null)
            {
                return(null);
            }
            var items = itemNodes.Select(node => ReadProperty(node, false, dictionaryAdapter)).ToArray();
            var array = Array.CreateInstance(itemType, items.Length);

            items.CopyTo(array, 0);
            return(array);
        }
Beispiel #29
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);
 }
        /// <summary>
        /// Gets the effective dictionary value.
        /// </summary>
        /// <param name="dictionaryAdapter">The dictionary adapter.</param>
        /// <param name="key">The key.</param>
        /// <param name="storedValue">The stored value.</param>
        /// <param name="property">The property.</param>
        /// <param name="ifExists">true if return only existing.</param>
        /// <returns>The effective property value.</returns>
        public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter,
                                       string key, object storedValue, PropertyDescriptor property, bool ifExists)
        {
            var propertyType = property.PropertyType;

            if (storedValue != null && propertyType.IsInstanceOfType(storedValue) == false)
            {
                if (converter != null && converter.CanConvertFrom(storedValue.GetType()))
                {
                    return(converter.ConvertFrom(storedValue));
                }
            }

            return(storedValue);
        }
Beispiel #31
0
        public string Validate(IDictionaryAdapter dictionaryAdapter)
        {
            var results = new List <string>();

            foreach (var property in dictionaryAdapter.This.Properties)
            {
                var result = Validate(dictionaryAdapter, property.Value);
                if (result != null)
                {
                    results.Add(result);
                }
            }

            return(results.Count > 0 ? string.Join("", results) : String.Empty);
        }
		/// <summary>
		/// Gets the effective dictionary value.
		/// </summary>
		/// <param name="dictionaryAdapter">The dictionary adapter.</param>
		/// <param name="key">The key.</param>
		/// <param name="storedValue">The stored value.</param>
		/// <param name="property">The property.</param>
		/// <param name="ifExists">true if return only existing.</param>
		/// <returns>The effective property value.</returns>
		public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter,
			string key, object storedValue, PropertyDescriptor property, bool ifExists)
		{
			var propertyType = property.PropertyType;

			if (storedValue != null && propertyType.IsInstanceOfType(storedValue) == false)
			{
				if (converter != null && converter.CanConvertFrom(storedValue.GetType()))
				{
					return converter.ConvertFrom(storedValue);
				}
			}

			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;
        }
Beispiel #34
0
        public override object GetValue(
            IXmlNode node,
            IDictionaryAdapter parent,
            IXmlAccessor accessor
            )
        {
            var items      = new ArrayList();
            var itemType   = node.ClrType.GetElementType();
            var references = XmlAdapter.For(parent).References;

            accessor
            .GetCollectionAccessor(itemType)
            .GetCollectionItems(node, parent, references, items);

            return(items.ToArray(itemType));
        }
Beispiel #35
0
        object IDictionaryPropertyGetter.GetPropertyValue(IDictionaryAdapter dictionaryAdapter,
                                                          string key, object storedValue, PropertyDescriptor property, bool ifExists)
        {
            XmlAccessor accessor;

            if (TryGetAccessor(key, property, null != storedValue, out accessor))
            {
                storedValue = accessor.GetPropertyValue(node, dictionaryAdapter, references, !ifExists);
                if (null != storedValue)
                {
                    AttachObservers(storedValue, dictionaryAdapter, property);
                    dictionaryAdapter.StoreProperty(property, key, storedValue);
                }
            }
            return(storedValue);
        }
        /// <summary>
        /// Gets the property value.
        /// </summary>
        /// <param name="dictionaryAdapter">The dictionary adapter.</param>
        /// <param name="key">The key.</param>
        /// <param name="storedValue">The stored value.</param>
        /// <param name="descriptor">The descriptor.</param>
        /// <param name="ifExists">true if return only existing.</param>
        /// <returns></returns>
        public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter,
                                       string key, object storedValue, PropertyDescriptor descriptor, bool ifExists)
        {
            key         = GetKey(dictionaryAdapter, key, descriptor);
            storedValue = storedValue ?? dictionaryAdapter.ReadProperty(key);

            if (getters != null)
            {
                foreach (var getter in getters)
                {
                    storedValue = getter.GetPropertyValue(dictionaryAdapter, key, storedValue, this, ifExists);
                }
            }

            return(storedValue);
        }
Beispiel #37
0
 private void ApplyValidationRules(
     IDictionaryAdapter dictionaryAdapter,
     IEnumerable <IValidationRule> rules,
     PropertyDescriptor property,
     object propertyValue,
     IList <string> errors
     )
 {
     if (rules != null)
     {
         foreach (var rule in rules)
         {
             rule.Apply(dictionaryAdapter, property, propertyValue, errors);
         }
     }
 }
Beispiel #38
0
        private void WriteComponent(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
        {
            var source = value as IDictionaryAdapter;

            if (source != null)
            {
                var node = result.RemoveChildren();
                if (result.Type != source.Meta.Type && result.OmitPolymorphism == false)
                {
                    var xmlType = source.GetXmlMeta().XmlType;
                    Context.SetXmlType(xmlType.TypeName, xmlType.Namespace, node);
                }
                var element = (IDictionaryAdapter)ReadComponent(result, false, dictionaryAdapter);
                source.CopyTo(element);
            }
        }
Beispiel #39
0
        bool IDictionaryPropertySetter.SetPropertyValue(IDictionaryAdapter dictionaryAdapter,
                                                        string key, ref object value, PropertyDescriptor property)
        {
            XmlAccessor accessor;

            if (TryGetAccessor(key, property, false, out accessor))
            {
                if (value != null && dictionaryAdapter.ShouldClearProperty(property, value))
                {
                    value = null;
                }
                var oldValue = dictionaryAdapter.ReadProperty(key);
                accessor.SetPropertyValue(node, dictionaryAdapter, references, oldValue, ref value);
            }
            return(true);
        }
Beispiel #40
0
		public XmlNodeList
		(
			IXmlNode               parentNode,
			IDictionaryAdapter     parentObject,
			IXmlCollectionAccessor accessor
		)
		: base
		(
			new XmlCollectionAdapter<T>
			(
				parentNode,
				parentObject,
				accessor
			)
		)
		{ }
Beispiel #41
0
        private void WriteCollection(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
        {
            result.Remove(value == null);

            if (value != null)
            {
                if (result.Type.IsArray)
                {
                    WriteArray(result, value, dictionaryAdapter);
                }
                else if (result.Type.IsGenericType)
                {
                    WriteList(result, value, dictionaryAdapter);
                }
            }
        }
Beispiel #42
0
        public bool HasProperty(string propertyName, IDictionaryAdapter dictionaryAdapter)
        {
            var key = dictionaryAdapter.GetKey(propertyName);

            if (key == null)
            {
                return(false);
            }

            PropertyDescriptor property;
            XmlAccessor        accessor;

            return(dictionaryAdapter.This.Properties.TryGetValue(propertyName, out property) &&
                   TryGetAccessor(key, property, false, out accessor) &&
                   accessor.IsPropertyDefined(node));
        }
        public virtual bool VisitDictionaryAdapter(
            IDictionaryAdapter dictionaryAdapter,
            Func <PropertyDescriptor, bool> selector,
            object state
            )
        {
            if (PushScope(dictionaryAdapter) == false)
            {
                return(false);
            }

            try
            {
                foreach (var property in dictionaryAdapter.This.Properties.Values)
                {
                    if (Cancelled)
                    {
                        break;
                    }

                    if (selector != null && selector(property) == false)
                    {
                        continue;
                    }

                    Type collectionItemType;
                    if (IsCollection(property, out collectionItemType))
                    {
                        VisitCollection(dictionaryAdapter, property, collectionItemType, state);
                    }
                    else if (property.PropertyType.IsInterface)
                    {
                        VisitInterface(dictionaryAdapter, property, state);
                    }
                    else
                    {
                        VisitProperty(dictionaryAdapter, property, state);
                    }
                }
            }
            finally
            {
                PopScope(dictionaryAdapter);
            }

            return(true);
        }
Beispiel #44
0
        public override object GetValue(
            IXmlNode node,
            IDictionaryAdapter parent,
            IXmlAccessor accessor
            )
        {
            var serializable = (IXmlSerializable)Activator.CreateInstance(node.ClrType);

            using (var reader = new XmlSubtreeReader(node, XmlDefaultSerializer.Root))
            {
                // Do NOT pre-read containing element
                // ...IXmlSerializable is not a symmetric contract
                serializable.ReadXml(reader);
            }

            return(serializable);
        }
Beispiel #45
0
 public override void SetPropertyValue(
     IXmlNode parentNode,
     IDictionaryAdapter parentObject,
     XmlReferenceManager references,
     object oldValue,
     ref object value
     )
 {
     if (SelectsNodes)
     {
         base.SetPropertyValue(parentNode, parentObject, references, oldValue, ref value);
     }
     else
     {
         throw Error.XPathNotCreatable(path);
     }
 }
Beispiel #46
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 #47
0
        void IDictionaryInitializer.Initialize(IDictionaryAdapter dictionaryAdapter, object[] behaviors)
        {
            var meta = dictionaryAdapter.Meta;

            if (primaryXmlMeta == null)
            {
                InitializePrimary(meta, dictionaryAdapter);
            }
            else
            {
                InitializeSecondary(meta);
            }

            InitializeBaseTypes(meta);
            InitializeStrategies(dictionaryAdapter);
            InitializeReference(dictionaryAdapter);
        }
Beispiel #48
0
        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);
        }
Beispiel #49
0
        void IDictionaryInitializer.Initialize(IDictionaryAdapter dictionaryAdapter, object[] behaviors)
        {
            var meta = dictionaryAdapter.Meta;

            if (meta.MetaInitializers.OfType <XPathBehavior>().FirstOrDefault() == null)
            {
                throw new InvalidOperationException(string.Format(
                                                        "Interface {0} requested xpath support, but was not configured properly.  " +
                                                        "Did you forget to add an XPathBehavior?", meta.Type.FullName));
            }

            dictionaryAdapter.This.CreateStrategy = this;

            Context.ApplyBehaviors(behaviors);
            xmlMeta = dictionaryAdapter.GetXmlMeta();

            if (string.IsNullOrEmpty(xmlMeta.XmlType.Namespace) == false)
            {
                Context.AddNamespace(string.Empty, xmlMeta.XmlType.Namespace);
            }

            if (Parent == null)
            {
                foreach (var behavior in behaviors)
                {
                    if (behavior is XPathAttribute)
                    {
                        var attrib             = (XPathAttribute)behavior;
                        var compiledExpression = attrib.CompiledExpression;
                        if (MoveOffRoot(root, XPathNodeType.Element) == false || Context.Matches(compiledExpression, root))
                        {
                            break;
                        }

                        var navigator = Context.SelectSingleNode(compiledExpression, root);
                        if (navigator != null)
                        {
                            root = navigator;
                            break;
                        }
                    }
                }
                MoveOffRoot(root, XPathNodeType.Element);
            }
        }
Beispiel #50
0
        public bool Equals(IDictionaryAdapter adapter1, IDictionaryAdapter adapter2)
        {
            var id1 = adapter1.GetProperty("Id", false);

            if (id1 == null)
            {
                return(false);
            }

            var id2 = adapter2.GetProperty("Id", false);

            if (id2 == null)
            {
                return(false);
            }

            return(id1.Equals(id2));
        }
        /// <summary>
        /// Gets the key.
        /// </summary>
        /// <param name="dictionaryAdapter">The dictionary adapter.</param>
        /// <param name="key">The key.</param>
        /// <param name="descriptor">The descriptor.</param>
        /// <returns></returns>
        public string GetKey(IDictionaryAdapter dictionaryAdapter, String key, PropertyDescriptor descriptor)
        {
            var behaviors = dictionaryBehaviors;

            if (behaviors != null)
            {
                var count = behaviors.Count;
                for (int i = 0; i < count; i++)
                {
                    var builder = behaviors[i] as IDictionaryKeyBuilder;
                    if (builder != null)
                    {
                        key = builder.GetKey(dictionaryAdapter, key, this);
                    }
                }
            }
            return(key);
        }
		public override void SetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor, object oldValue, ref object value)
		{
			var current = value as IXmlNodeSource;
			if (current != null && current.Node.PositionEquals(node))
				return;

			var newItems = value as IEnumerable;
			if (newItems == null)
				throw Error.NotSupported();

			var oldCollection = oldValue as ICollectionProjection;
			if (oldCollection != null)
				oldCollection.ClearReferences();

			var newCollection = (ICollectionProjection) GetValue(node, parent, accessor);
			newCollection.Replace(newItems);
			value = newCollection;
		}
Beispiel #53
0
        public virtual object GetPropertyValue(
            IXmlNode parentNode,
            IDictionaryAdapter parentObject,
            XmlReferenceManager references,
            bool orStub
            )
        {
            if (orStub)
            {
                orStub &= serializer.CanGetStub;
            }

            var cursor = IsCollection
                ? SelectCollectionNode(parentNode, orStub)
                : SelectPropertyNode(parentNode, orStub);

            return(GetValue(cursor, parentObject, references, cursor.MoveNext(), orStub));
        }
 public override void SetValue(
     IXmlCursor cursor,
     IDictionaryAdapter parentObject,
     XmlReferenceManager references,
     bool hasCurrent,
     object oldValue,
     ref object newValue
     )
 {
     if (newValue == null && IsCollection)
     {
         base.RemoveCollectionItems(cursor, references, oldValue);
     }
     else
     {
         base.SetValue(cursor, parentObject, references, hasCurrent, oldValue, ref newValue);
     }
 }
		private object[] GetFormatArguments(IDictionaryAdapter dictionaryAdapter, string formattedPropertyName)
		{
			var properties = Properties.Split(PropertyDelimeters, StringSplitOptions.RemoveEmptyEntries);
			var arguments = new object[properties.Length];
			for (int i = 0; i < properties.Length; ++i)
			{
				var propertyName = properties[i];
				if (propertyName != formattedPropertyName)
				{
					arguments[i] = dictionaryAdapter.GetProperty(propertyName);
				}
				else
				{
					arguments[i] = "(recursive)";
				}
			}
			return arguments;
		}
Beispiel #56
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)));
        }
Beispiel #57
0
        private object GetValueCore(IXmlNode node, IDictionaryAdapter parentObject, bool nodeExists, bool orStub)
        {
            if (nodeExists)
            {
                if (!node.IsNil)
                {
                    return(serializer.GetValue(node, parentObject, this));
                }
                else if (IsNillable)
                {
                    return(null);
                }
            }

            return(orStub
                                ? serializer.GetStub(node, parentObject, this)
                                : null);
        }
Beispiel #58
0
        public XmlCollectionAdapter(
            IXmlNode parentNode,
            IDictionaryAdapter parentObject,
            IXmlCollectionAccessor accessor)
        {
            items = new List <XmlCollectionItem <T> >();

            this.accessor     = accessor;
            this.cursor       = accessor.SelectCollectionItems(parentNode, true);
            this.parentNode   = parentNode;
            this.parentObject = parentObject;
            this.references   = XmlAdapter.For(parentObject).References;

            while (cursor.MoveNext())
            {
                items.Add(new XmlCollectionItem <T>(cursor.Save()));
            }
        }
Beispiel #59
0
		void IDictionaryInitializer.Initialize(IDictionaryAdapter dictionaryAdapter, object[] behaviors)
		{
			var meta = dictionaryAdapter.Meta;
			if (meta.MetaInitializers.OfType<XPathBehavior>().FirstOrDefault() == null)
			{
				throw new InvalidOperationException(string.Format(
					"Interface {0} requested xpath support, but was not configured properly.  " +
					"Did you forget to add an XPathBehavior?", meta.Type.FullName));
			}

			dictionaryAdapter.This.CreateStrategy = this;

			Context.ApplyBehaviors(behaviors);
			xmlMeta = dictionaryAdapter.GetXmlMeta();

			if (string.IsNullOrEmpty(xmlMeta.XmlType.Namespace) == false)
			{
				Context.AddNamespace(string.Empty, xmlMeta.XmlType.Namespace);
			}

			if (Parent == null)
			{
				foreach (var behavior in behaviors)
				{
					if (behavior is XPathAttribute)
					{
						var attrib = (XPathAttribute)behavior;
						var compiledExpression = attrib.CompiledExpression;
						if (MoveOffRoot(root, XPathNodeType.Element) == false || Context.Matches(compiledExpression, root))
						{
							break;
						}

						var navigator = Context.SelectSingleNode(compiledExpression, root);
						if (navigator != null)
						{
							root = navigator;
							break;
						}
					}
				}
				MoveOffRoot(root, XPathNodeType.Element);
			}
		}
		public virtual void VisitDictionaryAdapter(IDictionaryAdapter dictionaryAdapter)
		{
			foreach (var property in dictionaryAdapter.Meta.Properties.Values)
			{
				Type collectionItemType;
				if (IsCollection(property, out collectionItemType))
				{
					VisitCollection(dictionaryAdapter, property, collectionItemType);
				}
				else if (property.PropertyType.IsInterface)
				{
					VisitInterface(dictionaryAdapter, property);
				}
				else
				{
					VisitProperty(dictionaryAdapter, property);
				}
			}
		}