Ejemplo n.º 1
0
        /// <summary>
        /// Deletes the provided entity.
        /// </summary>
        /// <param name="entity">The entity to delete.</param>
        public override void Delete(IEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            using (LogGroup logGroup = LogGroup.StartDebug("Deleting provided '" + entity.ShortTypeName + "' entity."))
            {
                Db4oDataStore store = (Db4oDataStore)GetDataStore(entity);

                if (DataAccess.Data.IsStored(entity))
                {
                    using (Batch batch = BatchState.StartBatch())
                    {
                        IDataReader reader = Provider.InitializeDataReader();
                        reader.AutoRelease = false;

                        entity = reader.GetEntity(entity);

                        if (entity == null)
                        {
                            throw new Exception("The entity wasn't found.");
                        }

                        // PreDelete must be called to ensure all references are correctly managed
                        PreDelete(entity);

                        // Delete the entity
                        if (entity != null)
                        {
                            store.ObjectContainer.Delete(entity);

                            store.Commit();
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Updates the provided entity.
        /// </summary>
        /// <param name="entity">The entity to update.</param>
        public override void Update(IEntity entity)
        {
            using (LogGroup logGroup = LogGroup.Start("Updating the provided entity.", NLog.LogLevel.Debug))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                Db4oDataStore store = (Db4oDataStore)GetDataStore(entity);

                if (!store.IsStored(entity))
                {
                    throw new ArgumentException("entity", "The provided entity of type '" + entity.GetType() + "' is not found in the store with name '" + store.Name + "'.");
                }

                if (entity.ID == Guid.Empty)
                {
                    throw new ArgumentException("entity.ID must be set.");
                }

                using (Batch batch = BatchState.StartBatch())
                {
                    LogWriter.Debug("Entity type: " + entity.GetType().ToString());
                    LogWriter.Debug("Entity ID: " + entity.ID);

                    // Preupdate must be called to ensure all references are correctly stored
                    PreUpdate(entity);

                    // Get a bound copy of the entity
                    IDataReader reader = Provider.InitializeDataReader();
                    reader.AutoRelease = false;                     // Tell the reader not to unbind the entity from the store because it's still within the data access system

                    IEntity existingEntity = reader.GetEntity(entity);

                    if (existingEntity != null)
                    {
                        // Activate the found entity
                        Provider.Activator.Activate(existingEntity);

                        // Copy the provided data to the bound entity
                        entity.CopyTo(existingEntity);

                        // Remove all the referenced entities
                        existingEntity.Deactivate();

                        store.ObjectContainer.Store(existingEntity);

                        LogWriter.Debug("Entity updated.");

                        PostUpdate(entity);

                        store.Commit();
                    }
                    else
                    {
                        throw new InvalidOperationException("Cannot update an entity that doesn't already exist in the data store. Save the entity first.");
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Saves the provided entity into the provided data store.
        /// </summary>
        /// <param name="entity">The entity to save to the data store.</param>
        /// <param name="handleReferences">A value indicating whether to delete old references and save new references.</param>
        public override void Save(IEntity entity, bool handleReferences)
        {
            using (LogGroup logGroup = LogGroup.StartDebug("Saving entity of type '" + entity.ShortTypeName + "'."))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                if (entity.ID == Guid.Empty)
                {
                    throw new ArgumentException("entity.ID must be set.");
                }

                // Clone the entity so that it doesn't get bound to the store
                IEntity clonedEntity = entity.Clone();

                Db4oDataStore store = (Db4oDataStore)GetDataStore(clonedEntity);

                if (store.ObjectContainer == null)
                {
                    throw new InvalidOperationException("The ObjectContainer has not been initialized on the '" + store.Name + "' data store.");
                }

                using (Batch batch = BatchState.StartBatch())
                {
                    if (EntitiesUtilities.IsReference(clonedEntity.GetType()) && DataAccess.Data.IsStored(clonedEntity))
                    {
                        LogWriter.Debug("Existing reference found. Skipping save.");
                        // Just skip the saving altogether, if the reference already exists
                    }
                    else
                    {
                        if (clonedEntity == null)
                        {
                            throw new ArgumentNullException("entity");
                        }

                        LogWriter.Debug("Entity type: " + clonedEntity.GetType().ToString());
                        LogWriter.Debug("Entity ID: " + clonedEntity.ID.ToString());

                        PreSave(clonedEntity, handleReferences);

                        if (clonedEntity != null)
                        {
                            DataUtilities.StripReferences(clonedEntity);

                            // Save the entity
                            store.ObjectContainer.Store(clonedEntity);

                            // Post save the original entity NOT the cloned entity
                            PostSave(entity);

                            store.Commit();

                            LogWriter.Debug("Entity stored in '" + store.Name + "' store.");
                        }
                        else
                        {
                            LogWriter.Debug("Cloned entity == null. Not stored.");
                        }
                    }
                }
            }
        }