internal static void SetModelItem(XObject xobject, EFObject efobject)
 {
     if (xobject.Annotation<EFObject>() == null)
     {
         xobject.AddAnnotation(efobject);
     }
     else
     {
         xobject.RemoveAnnotations<EFObject>();
         xobject.AddAnnotation(efobject);
     }
 }
Esempio n. 2
0
        public void AddStringAnnotation(XObject xo)
        {
            const string expected = "test string";

            xo.AddAnnotation(expected);
            ValidateAnnotations(xo, new string[] { expected });
            Assert.Equal(expected, xo.Annotation <string>());
            Assert.Equal(expected, (string)xo.Annotation(typeof(string)));
            Assert.Equal(expected, xo.Annotation(typeof(object)));
            Assert.Equal(expected, xo.Annotation <object>());
        }
Esempio n. 3
0
        public void RemoveInheritedAnnotation(XObject xo)
        {
            A a = new A();
            B b = new B();

            xo.AddAnnotation(a);
            xo.AddAnnotation(b);
            xo.RemoveAnnotations <B>();

            ValidateAnnotations(xo, new A[] { a });
            Assert.Equal(a, xo.Annotation <A>());
            Assert.Equal(a, (A)xo.Annotation(typeof(A)));
            Assert.Equal(a, xo.Annotation <object>());
            Assert.Equal(a, xo.Annotation(typeof(object)));

            Assert.Equal(0, CountAnnotations <B>(xo));

            xo.RemoveAnnotations(typeof(A));
            Assert.Equal(0, CountAnnotations <A>(xo));
        }
Esempio n. 4
0
        public static void SetTextRange(this XObject attribute, TextRange textRange)
        {
            var saa = attribute.Annotation <XmlAttributeAnnotation>();

            if (saa == null)
            {
                saa = new XmlAttributeAnnotation();
                attribute.AddAnnotation(saa);
            }
            saa.TextRange = textRange;
        }
Esempio n. 5
0
        public void AddGenericAnnotation(XObject xo)
        {
            Dictionary <string, string> d = new Dictionary <string, string>();

            xo.AddAnnotation(d);
            ValidateAnnotations(xo, new Dictionary <string, string>[] { d });

            Assert.Equal(d, xo.Annotation <Dictionary <string, string> >());
            Assert.Equal(d, (Dictionary <string, string>)xo.Annotation(typeof(Dictionary <string, string>)));
            Assert.Equal(d, xo.Annotation <object>());
            Assert.Equal(d, xo.Annotation(typeof(object)));
        }
        internal static long GetNextIdentity(XObject element)
        {
            var annotation = element.Annotation<ModelAnnotation>();
            if (annotation == null)
            {
                annotation = new ModelAnnotation();
                element.AddAnnotation(annotation);
            }

            var nextIdentity = annotation.NextIdentity;
            annotation.NextIdentity = ++nextIdentity;

            return nextIdentity;
        }
Esempio n. 7
0
        /// <summary>
        /// Gets the first annotation object of the specified type, or creates a new one.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object AnnotationOrCreate(this XObject self, Type type)
        {
            Contract.Requires <ArgumentNullException>(self != null);
            Contract.Requires <ArgumentNullException>(type != null);

            var value = self.Annotation(type);

            if (value == null)
            {
                self.AddAnnotation(value = Activator.CreateInstance(type));
            }

            return(value);
        }
Esempio n. 8
0
        /// <summary>
        /// Gets the first annotation object of the specified type, or creates a new one.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static T AnnotationOrCreate <T>(this XObject self)
            where T : class, new()
        {
            Contract.Requires <ArgumentNullException>(self != null);

            var value = self.Annotation <T>();

            if (value == null)
            {
                self.AddAnnotation(value = new T());
            }

            return(value);
        }
Esempio n. 9
0
        /// <summary>
        /// Gets the first annotation object of the specified type, or creates a new one.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="type"></param>
        /// <param name="create"></param>
        /// <returns></returns>
        public static object AnnotationOrCreate(this XObject self, Type type, Func <object> create)
        {
            Contract.Requires <ArgumentNullException>(self != null);
            Contract.Requires <ArgumentNullException>(type != null);
            Contract.Requires <ArgumentNullException>(create != null);

            var value = self.Annotation(type);

            if (value == null)
            {
                self.AddAnnotation(value = create());
            }

            return(value);
        }
Esempio n. 10
0
        /// <summary>
        /// Gets the first annotation object of the specified type, or creates a new one.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static T AnnotationOrCreate <T>(this XObject self, Func <T> create)
            where T : class
        {
            Contract.Requires <ArgumentNullException>(self != null);
            Contract.Requires <ArgumentNullException>(create != null);

            var value = self.Annotation <T>();

            if (value == null)
            {
                self.AddAnnotation(value = create());
            }

            return(value);
        }
Esempio n. 11
0
        internal static long GetNextIdentity(XObject element)
        {
            var annotation = element.Annotation <ModelAnnotation>();

            if (annotation == null)
            {
                annotation = new ModelAnnotation();
                element.AddAnnotation(annotation);
            }

            var nextIdentity = annotation.NextIdentity;

            annotation.NextIdentity = ++nextIdentity;

            return(nextIdentity);
        }
Esempio n. 12
0
        /// <summary>
        ///     Gets the first annotation object of the specified type, or creates a new one.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static T GetOrAddAnnotation <T>(this XObject self)
            where T : class, new()
        {
            if (self == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(self));
            }

            var value = self.Annotation <T>();

            if (value == null)
            {
                self.AddAnnotation(value = new T());
            }

            return(value);
        }
Esempio n. 13
0
        private void ReplaceSchemaInfo(XObject o, XmlSchemaInfo schemaInfo)
        {
            schemaInfos ??= new Dictionary <XmlSchemaInfo, XmlSchemaInfo>(new XmlSchemaInfoEqualityComparer());
            XmlSchemaInfo?si = o.Annotation <XmlSchemaInfo>();

            if (si != null)
            {
                if (!schemaInfos.ContainsKey(si))
                {
                    schemaInfos.Add(si, si);
                }
                o.RemoveAnnotations <XmlSchemaInfo>();
            }
            if (!schemaInfos.TryGetValue(schemaInfo, out si))
            {
                si = schemaInfo;
                schemaInfos.Add(si, si);
            }
            o.AddAnnotation(si);
        }
Esempio n. 14
0
        /// <summary>
        ///     Gets the first annotation object of the specified type, or creates a new one.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object GetOrAddAnnotation(this XObject self, Type type)
        {
            if (self == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(self));
            }

            if (type == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(type));
            }

            var value = self.Annotation(type);

            if (value == null)
            {
                self.AddAnnotation(value = Activator.CreateInstance(type));
            }

            return(value);
        }
Esempio n. 15
0
        /// <summary>
        ///     Gets the first annotation object of the specified type, or creates a new one.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="create"></param>
        /// <returns></returns>
        public static T GetOrAddAnnotation <T>(this XObject self, Func <T> create)
            where T : class
        {
            if (self == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(self));
            }

            if (create == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(create));
            }

            var value = self.Annotation <T>();

            if (value == null)
            {
                self.AddAnnotation(value = create());
            }

            return(value);
        }
Esempio n. 16
0
        private void ReplaceSchemaInfo(XObject o, XmlSchemaInfo schemaInfo)
        {
            if (this.schemaInfos == null)
            {
                this.schemaInfos = new Dictionary <XmlSchemaInfo, XmlSchemaInfo>(new XmlSchemaInfoEqualityComparer());
            }
            XmlSchemaInfo key = o.Annotation <XmlSchemaInfo>();

            if (key != null)
            {
                if (!this.schemaInfos.ContainsKey(key))
                {
                    this.schemaInfos.Add(key, key);
                }
                o.RemoveAnnotations <XmlSchemaInfo>();
            }
            if (!this.schemaInfos.TryGetValue(schemaInfo, out key))
            {
                key = schemaInfo;
                this.schemaInfos.Add(key, key);
            }
            o.AddAnnotation(key);
        }
Esempio n. 17
0
 public void AddNullString(XObject xo)
 {
     Assert.Throws <ArgumentNullException>("annotation", () => xo.AddAnnotation((string)null));
     Assert.Null(xo.Annotation <object>());
     Assert.Throws <ArgumentNullException>("annotation", () => xo.AddAnnotation((string)null));
 }
 public void AddNull(XObject xo)
 {
     AssertExtensions.Throws <ArgumentNullException>("annotation", () => xo.AddAnnotation(null));
     Assert.Null(xo.Annotation <object>());
     AssertExtensions.Throws <ArgumentNullException>("annotation", () => xo.AddAnnotation(null));
 }
Esempio n. 19
0
        private void BindProperty(PropertyInfo property, AstNode parentASTNode, object value, XObject sourceXObject, bool bLateBinding)
        {
            sourceXObject.AddAnnotation(value);

            AstNode astNode = value as AstNode;
            if (astNode != null && !bLateBinding)
            {
                astNode.BoundXElement = sourceXObject;
            }

            if (property.PropertyType.IsAssignableFrom(value.GetType()))
            {
                property.SetValue(parentASTNode, value, null);
            }
            else if (IsContainerOf(typeof(ICollection<object>), property.PropertyType) && property.PropertyType.GetGenericArguments()[0].IsAssignableFrom(value.GetType()))
            {
                object collection = property.GetValue(parentASTNode, null);
                MethodInfo addMethod = collection.GetType().GetMethod("Add");
                addMethod.Invoke(collection, new object[] { value });
            }
            else
            {
                // TODO: Message.Error("No Binding Mechanism Worked");
            }
        }
Esempio n. 20
-1
        public static void BindFinalValue(PropertyInfo property, object parentItem, object value, XObject sourceXObject, bool definition)
        {
            sourceXObject.AddAnnotation(value);
            Type propertyType = property.PropertyType;

            if (CommonUtility.IsContainerOf(typeof(ICollection<object>), propertyType) && propertyType.GetGenericArguments()[0].IsAssignableFrom(value.GetType()))
            {
                object collection = property.GetValue(parentItem, null);
                MethodInfo addMethod = collection.GetType().GetMethod("Add");
                addMethod.Invoke(collection, new[] { value });
            }
            else if (propertyType.IsAssignableFrom(value.GetType()))
            {
                property.SetValue(parentItem, value, null);
            }
            else
            {
                // TODO: Message.Error("No Binding Mechanism Worked");
            }

            var mappingProvider = value as IXObjectMappingProvider;
            if (mappingProvider != null && definition)
            {
                mappingProvider.BoundXObject = new XObjectMapping(sourceXObject, property.Name);
            }
        }