/// <summary>
 /// Writes all of the items in the changeset to the database.
 /// </summary>
 /// <returns></returns>
 public virtual void SaveChanges()
 {
     EntityIndexWriter.ApplyIndexing();
     EntityRelationshipWriter.CommitEntityRelationships(ChangeTracker);
     ChangeTracker.DetectChanges();
     CheckEntityValidation();
     EntityWriter.Write(ChangeTracker);
     ChangeTracker.CommitChanges();
 }
        /// <summary>
        /// Writes all of the items in the changeset to the database.
        /// </summary>
        /// <returns></returns>
        public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            await EntityIndexWriter.ApplyIndexingAsync(cancellationToken).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();
            await EntityRelationshipWriter.CommitEntityRelationshipsAsync(ChangeTracker, cancellationToken).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();
            ChangeTracker.DetectChanges();
            CheckEntityValidation();
            cancellationToken.ThrowIfCancellationRequested();
            await EntityWriter.WriteAsync(ChangeTracker, cancellationToken).ConfigureAwait(false);

            ChangeTracker.CommitChanges();
        }
        /// <summary>
        /// Initialise a new entity reader and writer to the specified database.
        /// </summary>
        /// <param name="database"></param>
        public void SetDatabase(IMongoDatabase database)
        {
            Database = database;

            var entityMapper = new EntityMapper <TEntity>();

            EntityWriter = new DbEntityWriter <TEntity>(database, entityMapper);
            EntityReader = new DbEntityReader <TEntity>(database, entityMapper);

            //TODO: Look at this again in the future, this seems unnecessarily complex
            var indexMapper = new EntityIndexMapper <TEntity>(entityMapper);
            var collection  = database.GetCollection <TEntity>(entityMapper.GetCollectionName());

            EntityIndexWriter = new EntityIndexWriter <TEntity>(collection, indexMapper);

            EntityRelationshipWriter = new EntityRelationshipWriter <TEntity>(database, entityMapper);
        }
        public async Task AddRelationshipToNewEntityAsync()
        {
            var connection = TestConfiguration.GetConnection();
            var entity     = new SingleEntityIntegrationModel
            {
                RelatedItem = new StringIdModel
                {
                    Description = "SaveNewEntity-RelatedItem"
                }
            };

            var entityRelationshipWriter = new EntityRelationshipWriter <SingleEntityIntegrationModel>(connection);

            await entityRelationshipWriter.CommitEntityRelationshipsAsync(new[] { entity }).ConfigureAwait(false);

            Assert.IsNotNull(entity.RelatedItemId);
            Assert.IsTrue(entity.RelatedItemId == entity.RelatedItem.Id);
        }
        public void AddRelationshipToNewEntity()
        {
            var database = TestConfiguration.GetDatabase();
            var entity   = new SingleEntityIntegrationModel
            {
                RelatedItem = new StringIdModel
                {
                    Description = "SaveNewEntity-RelatedItem"
                }
            };

            var entityMapper             = new EntityMapper <SingleEntityIntegrationModel>();
            var entityRelationshipWriter = new EntityRelationshipWriter <SingleEntityIntegrationModel>(database, entityMapper);

            entityRelationshipWriter.CommitEntityRelationships(new[] { entity });

            Assert.IsNotNull(entity.RelatedItemId);
            Assert.IsTrue(entity.RelatedItemId == entity.RelatedItem.Id);
        }