예제 #1
0
        /// <summary>
        /// Adds an object to be tracked by this objecttype.
        /// </summary>
        /// <param name="existingObject">Object to track.</param>
        /// <param name="assumeNew">If false, ignores objects not ready to be saved (with id=-1).
        /// Else assign a new ID if the object has not been saved yet.</param>
        /// <returns>True on success. False if somehow the add failed.</returns>
        public virtual bool Add(ISaveable existingObject, bool assumeNew = true)
        {
            int id = existingObject.GetSaveID();

            if (assumeNew && id == -1)
            {
                // SetSaveID is only called by two things: Loading, and this method. By locking here, this should be threadsafe.
                lock (this)
                {
                    id = existingObject.GetSaveID();
                    if (id == -1)
                    {
                        id = NextFreeNumber(); //Note that this may get a lock on ItemDictionary also, and then the file stream.
                        existingObject.SetSaveID(id);
                        existingObject.Save();
                    }
                }
            }
            if (id >= 0)
            {
                Add(existingObject, id, false);
                return(true);
            }
            return(false);
        }