T IReferences.GetOrCreateObject <T>(JObject parent, string name, Func <ModelDataAccessor, T> factory, bool createIfNull)
        {
            if (_objects.TryGet(parent, name, out T obj))
            {
                return(obj);
            }

            var property = Get(parent, name);

            if (property == null)
            {
                if (createIfNull)
                {
                    var jObj = _refs.Create();
                    if (parent.ContainsKey(name))
                    {
                        parent[name] = jObj;
                    }
                    else
                    {
                        parent.Add(name, jObj);
                    }
                    property = new ModelDataAccessor(jObj, this);
                }
                else
                {
                    return(default(T));
                }
            }
            obj = (factory ?? Mappings.GetFactory <T>()).Invoke(property);
            _objects.Add(parent, name, obj);
            return(obj);
        }
Exemple #2
0
        public T Add(Action <IReferences, JObject> init = null)
        {
            JObject obj;

            if (typeof(T).IsSubclassOf(typeof(RefModel)))
            {
                obj = _refs.Create();
            }
            else
            {
                obj = new JObject();
            }
            init?.Invoke(_refs, obj);
            var accessor = new ModelDataAccessor(obj, _refs);
            var item     = _factory.Invoke(accessor);

            _array.Add(obj);
            _items.Add(item);
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
            return(item);
        }