Beispiel #1
0
        public ODocument SetField <T>(string fieldPath, T value)
        {
            if (fieldPath.Contains("."))
            {
                var       fields           = fieldPath.Split('.');
                int       iteration        = 1;
                ODocument embeddedDocument = this;

                foreach (var field in fields)
                {
                    if (iteration == fields.Length)
                    {
                        if (embeddedDocument.ContainsKey(field))
                        {
                            embeddedDocument[field] = value;
                        }
                        else
                        {
                            embeddedDocument.Add(field, value);
                        }
                        break;
                    }

                    if (embeddedDocument.ContainsKey(field))
                    {
                        embeddedDocument = (ODocument)embeddedDocument[field];
                    }
                    else
                    {
                        // if document which contains the field doesn't exist create it first
                        ODocument tempDocument = new ODocument();
                        embeddedDocument.Add(field, tempDocument);
                        embeddedDocument = tempDocument;
                    }

                    iteration++;
                }
            }
            else
            {
                if (ContainsKey(fieldPath))
                {
                    this[fieldPath] = value;
                }
                else
                {
                    Add(fieldPath, value);
                }
            }

            return(this);
        }
Beispiel #2
0
        public bool HasField(string fieldPath)
        {
            if (ContainsKey(fieldPath))
            {
                return(true);
            }

            bool contains = false;

            if (fieldPath.Contains("."))
            {
                var       fields           = fieldPath.Split('.');
                int       iteration        = 1;
                ODocument embeddedDocument = this;

                foreach (var field in fields)
                {
                    if (iteration == fields.Length)
                    {
                        contains = embeddedDocument.ContainsKey(field);
                        break;
                    }

                    if (embeddedDocument.ContainsKey(field))
                    {
                        embeddedDocument = (ODocument)embeddedDocument[field];
                        iteration++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(contains);
        }
        public T GetField <T>(string fieldPath)
        {
            Type type = typeof(T);
            T    value;

            if (type.IsPrimitive || type.IsArray || (type.Name == "String"))
            {
                value = default(T);
            }
            else
            {
                value = (T)Activator.CreateInstance(type);
            }

            if (fieldPath.Contains("."))
            {
                var       fields           = fieldPath.Split('.');
                int       iteration        = 1;
                ODocument embeddedDocument = this;

                foreach (var field in fields)
                {
                    if (iteration == fields.Length)
                    {
                        // if value is collection type, get element type and enumerate over its elements
                        if (value is IList)
                        {
                            Type        elementType = ((IEnumerable)value).GetType().GetGenericArguments()[0];
                            IEnumerator enumerator  = ((IEnumerable)embeddedDocument[field]).GetEnumerator();

                            while (enumerator.MoveNext())
                            {
                                // if current element is ODocument type which is dictionary<string, object>
                                // map its dictionary data to element instance
                                if (enumerator.Current is ODocument)
                                {
                                    var instance = Activator.CreateInstance(elementType);
                                    ((ODocument)enumerator.Current).Map(ref instance);

                                    ((IList)value).Add(instance);
                                }
                                else
                                {
                                    ((IList)value).Add(enumerator.Current);
                                }
                            }
                        }
                        else if (type.Name == "HashSet`1")
                        {
                            Type        elementType = ((IEnumerable)value).GetType().GetGenericArguments()[0];
                            IEnumerator enumerator  = ((IEnumerable)this[fieldPath]).GetEnumerator();

                            var addMethod = type.GetMethod("Add");

                            while (enumerator.MoveNext())
                            {
                                // if current element is ODocument type which is Dictionary<string, object>
                                // map its dictionary data to element instance
                                if (enumerator.Current is ODocument)
                                {
                                    var instance = Activator.CreateInstance(elementType);
                                    ((ODocument)enumerator.Current).Map(ref instance);

                                    addMethod.Invoke(value, new object[] { instance });
                                }
                                else
                                {
                                    addMethod.Invoke(value, new object[] { enumerator.Current });
                                }
                            }
                        }
                        else
                        {
                            value = (T)embeddedDocument[field];
                        }
                        break;
                    }

                    if (embeddedDocument.ContainsKey(field))
                    {
                        embeddedDocument = (ODocument)embeddedDocument[field];
                        iteration++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                if (this.ContainsKey(fieldPath))
                {
                    // if value is list or set type, get element type and enumerate over its elements
                    if (value is IList)
                    {
                        Type        elementType = ((IEnumerable)value).GetType().GetGenericArguments()[0];
                        IEnumerator enumerator  = ((IEnumerable)this[fieldPath]).GetEnumerator();

                        while (enumerator.MoveNext())
                        {
                            // if current element is ODocument type which is Dictionary<string, object>
                            // map its dictionary data to element instance
                            if (enumerator.Current is ODocument)
                            {
                                var instance = Activator.CreateInstance(elementType);
                                ((ODocument)enumerator.Current).Map(ref instance);

                                ((IList)value).Add(instance);
                            }
                            else
                            {
                                ((IList)value).Add(enumerator.Current);
                            }
                        }
                    }
                    else if (type.Name == "HashSet`1")
                    {
                        Type        elementType = ((IEnumerable)value).GetType().GetGenericArguments()[0];
                        IEnumerator enumerator  = ((IEnumerable)this[fieldPath]).GetEnumerator();

                        var addMethod = type.GetMethod("Add");

                        while (enumerator.MoveNext())
                        {
                            // if current element is ODocument type which is Dictionary<string, object>
                            // map its dictionary data to element instance
                            if (enumerator.Current is ODocument)
                            {
                                var instance = Activator.CreateInstance(elementType);
                                ((ODocument)enumerator.Current).Map(ref instance);

                                addMethod.Invoke(value, new object[] { instance });
                            }
                            else
                            {
                                addMethod.Invoke(value, new object[] { enumerator.Current });
                            }
                        }
                    }
                    else
                    {
                        value = (T)this[fieldPath];
                    }
                }
            }

            return(value);
        }