See http://amazedsaint.blogspot.com/2010/02/introducing-elasticobject-for-net-40.html for details
Inheritance: System.Dynamic.DynamicObject, IElasticHierarchyWrapper, INotifyPropertyChanged
        public void Run()
        {
            string xml = @"
            <People>
              <Person Name=""Shay Friedman"" TwitterHandle=""@IronShay"" Blog=""http://IronShay.com"" />
              <Person Name=""John Doe"" TwitterHandle=""@JohnDoe"" Blog=""http://Anonymous.com"" />
            </People>
            ";
            dynamic people = XElement.Parse(xml).ToElastic();
            foreach (dynamic person in people["Person"])
            {
                Console.WriteLine("{0}, {1}, {2}", person.Name, person.TwitterHandle, person.Blog);
            }

            dynamic band = new ElasticObject("Band");
            band.Name = "The Scropions";
            dynamic song1 = band.Songs.Song();
            song1.Name = "Winds of Change";
            song1.Length = "5:12";
            dynamic song2 = band.Songs.Song();
            song2.Name = "Still Loving You";
            song2.Length = "6:26";

            XElement element = band > FormatType.Xml;
            Console.WriteLine(element.ToString());
        }
        /// <summary>
        /// Build an expando from an XElement
        /// </summary>
        /// <param name="el"></param>
        /// <returns></returns>
        public static ElasticObject ElasticFromXElement(XElement el)
        {
            var exp = new ElasticObject();

            if (!string.IsNullOrEmpty(el.Value))
                exp.InternalValue = el.Value;

            exp.InternalName = el.Name.LocalName;

            foreach (var a in el.Attributes())
                exp.CreateOrGetAttribute(a.Name.LocalName, a.Value);

            var textNode= el.Nodes().FirstOrDefault();
             if (textNode is XText)
                {
                    exp.InternalContent = textNode.ToString();
                }

            foreach (var c in el.Elements())
            {

                var child = ElasticFromXElement(c);
                child.InternalParent = exp;
                exp.AddElement(child);
            }
            return exp;
        }
 public void AddElement(ElasticObject element)
 {
     if (!elements.ContainsKey(element.InternalName))
     {
         elements[element.InternalName] = new List <ElasticObject>();
     }
     elements[element.InternalName].Add(element);
 }
 public void RemoveElement(ElasticObject element)
 {
     if (elements.ContainsKey(element.InternalName))
     {
         if (elements[element.InternalName].Contains(element))
         {
             elements[element.InternalName].Remove(element);
         }
     }
 }
        public void Check_Named_Parameters()
        {
            dynamic model = new ElasticObject();
            var c1 = model.@class(new { name = "Class1" });
            c1.property(new { name = "Property1", type = "string" });
            c1.property(new { name = "Property2", type = "string" });
            var c2 = model.@class(new { name = "Class2" });
            c2.property(new { name = "Property1", type = "string" });
            c2.property(new { name = "Property2", type = "string" });

            Assert.AreEqual(model["class"].Count, 2);
            Assert.AreEqual(model["class"][0]["property"].Count, 2);
        }
        public void Check_The_Element_And_Attributes_Can_Be_Added_Using_Operators()
        {
            dynamic myobj = new ElasticObject("MyObject");

            var attrib1= (myobj << "element") < "attribute1";
            attrib1 <<= "hello1";
            var attrib2 = (myobj << "element") < "attribute1";
            attrib2 <<= "hello2";

            //first element
            Assert.AreEqual(myobj[0].attribute1, "hello1");

            //second element
            Assert.AreEqual(myobj[1].attribute1, "hello2");
        }
        /// <summary>
        /// Returns an XElement from an ElasticObject
        /// </summary>
        /// <param name="elastic"></param>
        /// <returns></returns>
        public static XElement XElementFromElastic(ElasticObject elastic)
        {
            var exp = new XElement(elastic.InternalName);

            foreach (var a in elastic.Attributes)
            {
                    if (a.Value.InternalValue != null)
                        exp.Add(new XAttribute(a.Key, a.Value.InternalValue));
            }

            if (null != elastic.InternalContent && elastic.InternalContent is string)
            {
                exp.Add(new XText(elastic.InternalContent as string));
            }

            foreach (var c in elastic.Elements)
            {
                    var child = XElementFromElastic(c);
                    exp.Add(child);
            }
            return exp;
        }
Exemple #8
0
        /// <summary>
        /// Catch a get member invocation
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (elasticProvider.HasAttribute(binder.Name))
            {
                result = elasticProvider.Attribute(binder.Name).InternalValue;
            }
            else
            {
                var obj = elasticProvider.Element(binder.Name);
                if (obj != null)
                {
                    result = obj;
                }
                else
                {
                    var exp = new ElasticObject(binder.Name, null);
                    elasticProvider.AddElement(exp);
                    result = exp;
                }
            }

            return(true);
        }
Exemple #9
0
        /// <summary>
        /// Interpret a method call
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="args"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
        {
            var obj = new ElasticObject(binder.Name, null);

            foreach (var a in args)
            {
#if NETFX_CORE
                foreach (var p in a.GetType().GetTypeInfo().DeclaredProperties)
                {
                    AddAttribute(p.Name, p.GetValue(a, null));
                }
#else
                foreach (var p in a.GetType().GetProperties())
                {
                    AddAttribute(p.Name, p.GetValue(a, null));
                }
#endif
            }

            AddElement(obj);
            result = obj;
            return(true);
        }
        /// <summary>
        /// Returns an XElement from an ElasticObject
        /// </summary>
        /// <param name="elastic"></param>
        /// <param name="nameSpace"></param>
        /// <returns></returns>
        public static XElement XElementFromElastic(ElasticObject elastic, XNamespace nameSpace = null)
        {
            // we default to empty namespace
            nameSpace = nameSpace ?? string.Empty;
            var exp = new XElement(nameSpace + elastic.InternalName);

            foreach (var a in elastic.Attributes)
            {
                if (a.Value.InternalValue != null)
                {
                    // if we have xmlns attribute add it like XNamespace instead of regular attribute
                    if (a.Key.Equals("xmlns", StringComparison.OrdinalIgnoreCase))
                    {
                        nameSpace = a.Value.InternalValue.ToString();
                        exp.Name = nameSpace.GetName(exp.Name.LocalName);
                    }
                    else
                    {
                        exp.Add(new XAttribute(a.Key, a.Value.InternalValue));
                    }
                }
            }

            if (null != elastic.InternalContent && elastic.InternalContent is string)
            {
                exp.Add(new XText(elastic.InternalContent as string));
            }

            foreach (var c in elastic.Elements)
            {
                    // for child element add current XNamespace
                    var child = XElementFromElastic(c, nameSpace);
                    exp.Add(child);
            }
            return exp;
        }
        public void Check_Nested_Objects_Traversing()
        {
            dynamic model = new ElasticObject();
            var c1 = model.@class();
            c1.name = "Class1";
            var p = c1.property();
            p.name = "Property1";
            p.type = "string";
            p = c1.property();
            p.name = "Property1";
            p.type = "string";

            var c2 = model.@class();
            p = c2.property();
            c2.name = "Class2";
            p.name = "Property1";
            p.type = "string";
            p = c2.property();
            p.name = "Property1";
            p.type = "string";

            Assert.AreEqual(model["class"].Count,2);
            Assert.AreEqual(model["class"][0]["property"].Count,2);
        }
        public void Check_The_LeftShift_Operator_Can_Add_An_Element_By_Name()
        {
            dynamic myobj = new ElasticObject("MyObject");

            for (int i = 0; i < 10; i++)
            {
                var newItem = myobj << "Item";
                newItem.CountNumber = i;
            }

            Assert.AreEqual(10, myobj["Item"].Count);
        }
        public void Check_The_LessThan_Operator_Can_Add_An_Attribute_By_Name()
        {
            dynamic myobj = new ElasticObject("MyObject");

            for (int i = 0; i < 10; i++)
            {
                var newItem = myobj < "Attrib" + i;
                newItem <<= "somevalue";
            }

            //Few random checks

            Assert.AreEqual(myobj.Attrib1, "somevalue");
            Assert.AreEqual(myobj.Attrib8, "somevalue");
        }
Exemple #14
0
 public void RemoveElement(ElasticObject element)
 {
     elasticProvider.RemoveElement(element);
 }
        /// <summary>
        /// Interpret the invocation of a binary operation
        /// </summary>
        public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result)
        {
            if (binder.Operation == ExpressionType.LeftShiftAssign && nodeType==NodeType.Element)
            {
                InternalContent = arg;
                result = this;
                return true;
            }
            else if (binder.Operation == ExpressionType.LeftShiftAssign  && nodeType == NodeType.Attribute)
            {
                InternalValue = arg;
                result = this;
                return true;
            }

            else if (binder.Operation == ExpressionType.LeftShift)
            {
                if (arg is string)
                {
                    var exp = new ElasticObject(arg as string, null) { nodeType = NodeType.Element };
                    AddElement(exp);
                    result = exp;
                    return true;
                }

                else if (arg is ElasticObject)
                {
                    var eobj = arg as ElasticObject;
                    if (!Elements.Contains(eobj))
                        AddElement(eobj);
                    result = eobj;
                    return true;
                }
            }

            else if (binder.Operation == ExpressionType.LessThan)
            {
                string memberName = arg as string;
                if (arg is string)
                {
                    if (!HasAttribute(memberName))
                    {
                        var att = new ElasticObject(memberName, null);
                        AddAttribute(memberName, att);
                        result = att;
                        return true;
                    }
                    else
                    {
                        throw new InvalidOperationException("An attribute with name" + memberName +  " already exists");
                    }
                }
                else if (arg is ElasticObject)
                {
                    var eobj = arg as ElasticObject;
                    AddAttribute(memberName, eobj);
                    result = eobj;
                    return true;
                }
            }
            else if (binder.Operation == ExpressionType.GreaterThan)
            {
                if (arg is FormatType)
                {
                    result = this.ToXElement();
                    return true;
                }
            }
            return base.TryBinaryOperation(binder, arg, out result);
        }
Exemple #16
0
 public void AddAttribute(string key, ElasticObject value)
 {
     value.nodeType       = NodeType.Attribute;
     value.InternalParent = this;
     elasticProvider.AddAttribute(key, value);
 }
Exemple #17
0
 public void AddElement(ElasticObject element)
 {
     element.nodeType       = NodeType.Element;
     element.InternalParent = this;
     elasticProvider.AddElement(element);
 }
        /// <summary>
        /// Interpret a method call
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="args"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
        {
            var obj = new ElasticObject(binder.Name,null);

            foreach (var a in args)
            {
            #if NETFX_CORE
                foreach(var p in a.GetType().GetTypeInfo().DeclaredProperties)
                    AddAttribute(p.Name,p.GetValue(a,null));
            #else
                foreach (var p in a.GetType().GetProperties())
                    AddAttribute(p.Name,p.GetValue(a,null));
            #endif
            }

            AddElement(obj);
            result = obj;
            return true;
        }
 public void RemoveElement(ElasticObject element)
 {
     if (elements.ContainsKey(element.InternalName))
     {
         if (elements[element.InternalName].Contains(element))
             elements[element.InternalName].Remove(element);
     }
 }
 public void AddAttribute(string key, ElasticObject value)
 {
     attributes.Add(key, value);
 }
        public void Check_The_Object_Indexer_Can_Accept_Integers()
        {
            dynamic myobj = new ElasticObject("MyObject");

            for (int i = 0; i < 10; i++)
            {
                var newItem = myobj << "Item";
                newItem.CountNumber = i;
            }

            //Check the 3rd and 9th items
            Assert.AreEqual(3, myobj[3].CountNumber);
            Assert.AreEqual(9, myobj[9].CountNumber);
        }
 public void AddAttribute(string key, ElasticObject value)
 {
     attributes.Add(key, value);
 }
 public void AddElement(ElasticObject element)
 {
     if (!elements.ContainsKey(element.InternalName))
     {
         elements[element.InternalName] = new List<ElasticObject>();
     }
     elements[element.InternalName].Add(element);
 }
 public void RemoveElement(ElasticObject element)
 {
     elasticProvider.RemoveElement(element);
 }
        /// <summary>
        /// A private helper method
        /// </summary>
        /// <returns></returns>
        private dynamic CreateStoreObject()
        {
            dynamic store = new ElasticObject("Store");
            store.Name = "Acme Store";
            store.Location.Address = "West Avenue, Heaven Street Road, LA";
            store.Products.Count = 2;

            store.Owner.FirstName = "Jack";
            store.Owner.SecondName = "Jack";

            //try to set the internal content for an element
            store.Owner <<= "this is some internal content for owner";

            //Add a new product
            var p1 = store.Products.Product();
            p1.Name = "Acme Floor Cleaner";
            p1.Price = 20;

            //Add another product
            var p2 = store.Products.Product();
            p2.Name = "Acme Bun";
            p2.Price = 22;

            return store;
        }
 public void AddElement(ElasticObject element)
 {
     element.nodeType = NodeType.Element;
     element.InternalParent = this;
     elasticProvider.AddElement(element);
 }
Exemple #27
0
        /// <summary>
        /// Interpret the invocation of a binary operation
        /// </summary>
        public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result)
        {
            if (binder.Operation == ExpressionType.LeftShiftAssign && nodeType == NodeType.Element)
            {
                InternalContent = arg;
                result          = this;
                return(true);
            }
            else if (binder.Operation == ExpressionType.LeftShiftAssign && nodeType == NodeType.Attribute)
            {
                InternalValue = arg;
                result        = this;
                return(true);
            }

            else if (binder.Operation == ExpressionType.LeftShift)
            {
                if (arg is string)
                {
                    var exp = new ElasticObject(arg as string, null)
                    {
                        nodeType = NodeType.Element
                    };
                    AddElement(exp);
                    result = exp;
                    return(true);
                }

                else if (arg is ElasticObject)
                {
                    var eobj = arg as ElasticObject;
                    if (!Elements.Contains(eobj))
                    {
                        AddElement(eobj);
                    }
                    result = eobj;
                    return(true);
                }
            }

            else if (binder.Operation == ExpressionType.LessThan)
            {
                string memberName = arg as string;
                if (arg is string)
                {
                    if (!HasAttribute(memberName))
                    {
                        var att = new ElasticObject(memberName, null);
                        AddAttribute(memberName, att);
                        result = att;
                        return(true);
                    }
                    else
                    {
                        throw new InvalidOperationException("An attribute with name" + memberName + " already exists");
                    }
                }
                else if (arg is ElasticObject)
                {
                    var eobj = arg as ElasticObject;
                    AddAttribute(memberName, eobj);
                    result = eobj;
                    return(true);
                }
            }
            else if (binder.Operation == ExpressionType.GreaterThan)
            {
                if (arg is FormatType)
                {
                    result = this.ToXElement();
                    return(true);
                }
            }
            return(base.TryBinaryOperation(binder, arg, out result));
        }
        /// <summary>
        /// Catch a get member invocation
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (elasticProvider.HasAttribute(binder.Name))
            {
                result = elasticProvider.Attribute(binder.Name).InternalValue;
            }
            else
            {
                var obj = elasticProvider.Element(binder.Name);
                if (obj != null)
                {
                    result = obj;
                }
                else
                {
                    var exp = new ElasticObject(binder.Name,null);
                    elasticProvider.AddElement(exp);
                    result = exp;
                }
            }

            return true;
        }
        public void Check_The_Indexer_Take_A_Delegate()
        {
            dynamic myobj = new ElasticObject("MyObject");

            for (int i = 0; i < 10; i++)
            {
                var newItem = myobj << "Item";
                newItem.CountNumber = i;
            }

            var filter=new Func<dynamic,bool>((obj)=>obj.CountNumber>5);
            var result = myobj[filter];

            //4 items remains above 5
            Assert.AreEqual(4, result.Count);
        }
 public void AddAttribute(string key, ElasticObject value)
 {
     value.nodeType = NodeType.Attribute;
     value.InternalParent = this;
     elasticProvider.AddAttribute(key, value);
 }