Beispiel #1
0
        public T GetOrCreate <T>(string objectId) where T : class, new()
        {
            PersistencePathValidator.ThrowIfPathInvalid(objectId);

            lock (locker)
            {
                PersistentObject po;
                if (!objects.TryGetValue(objectId.ToUpperInvariant(), out po))
                {
                    var data = dataStorage.TryLoad(setId, objectId);
                    T   o;
                    if (data != null)
                    {
                        o = serializer.Deserialize <T>(data);
                        if (o != null)
                        {
                            // unexpectedly, 'o' can be null, if 'data' is a NUL string.
                            po = new PersistentObject(objectId, o);
                        }
                    }

                    // if object never existed or could not be deserialized, create a new object
                    if (po == null)
                    {
                        o  = new T();
                        po = new PersistentObject(objectId, o);
                        po.SaveAndFlagUnchanged(serializer, dataStorage, setId);
                    }

                    objects[objectId.ToUpperInvariant()] = po;
                }
                return((T)po.Obj);
            }
        }
        /// <summary>
        /// </summary>
        /// <param name="setId">Case insensitive</param>
        /// <returns></returns>
        ObjectSet GetOrCreateObjectSetPrivate(string setId)
        {
            PersistencePathValidator.ThrowIfPathInvalid(setId);

            lock (locker)
            {
                ObjectSet pc;
                if (!sets.TryGetValue(setId.ToUpperInvariant(), out pc))
                {
                    pc = new ObjectSet(setId, serializer, dataStorage, locker);
                    sets[setId.ToUpperInvariant()] = pc;
                }
                return(pc);
            }
        }