private IEnumerable <IWriteCommand <TEntity> > GetWriteCommands()
        {
            var results = new List <IWriteCommand <TEntity> >();

            foreach (var collection in ManagedCollections)
            {
                foreach (var entry in collection.GetEntries())
                {
                    if (entry.State == EntityEntryState.Added)
                    {
                        EntityMutation <TEntity> .MutateEntity(entry.Entity, MutatorType.Insert, Connection);
                    }
                    else if (entry.State == EntityEntryState.Updated)
                    {
                        EntityMutation <TEntity> .MutateEntity(entry.Entity, MutatorType.Update, Connection);
                    }

                    var validationContext = new ValidationContext(entry.Entity);
                    Validator.ValidateObject(entry.Entity, validationContext);

                    var command = EntityCommandBuilder <TEntity> .CreateCommand(entry);

                    if (command != null)
                    {
                        results.Add(command);
                    }
                }
            }

            results.AddRange(StagedCommands);
            return(results);
        }
Example #2
0
        public virtual TEntity Create()
        {
            var entity = Activator.CreateInstance <TEntity>();

            EntityMutation <TEntity> .MutateEntity(entity, MutatorType.Create, Connection);

            Add(entity);
            return(entity);
        }
Example #3
0
        private IEnumerable <WriteModel <TEntity> > BuildWriteModel(IDbEntityCollection <TEntity> entityCollection)
        {
            var idFieldName = EntityMapper.GetIdName();
            var writeModel  = new List <WriteModel <TEntity> >();

            foreach (var entry in entityCollection.GetEntries())
            {
                if (entry.State == DbEntityEntryState.Added)
                {
                    EntityMutation <TEntity> .MutateEntity(entry.Entity, MutatorType.Insert, Database);

                    writeModel.Add(new InsertOneModel <TEntity>(entry.Entity));
                }
                else if (entry.State == DbEntityEntryState.Updated)
                {
                    EntityMutation <TEntity> .MutateEntity(entry.Entity, MutatorType.Update, Database);

                    var idFieldValue = EntityMapper.GetIdValue(entry.Entity);
                    var filter       = Builders <TEntity> .Filter.Eq(idFieldName, idFieldValue);

                    var updateDefintion = UpdateDefinitionHelper.CreateFromDiff <TEntity>(entry.OriginalValues, entry.CurrentValues);

                    //MongoDB doesn't like it if an UpdateDefinition is empty.
                    //This is primarily to work around a mutation that may set an entity to its default state.
                    if (updateDefintion.HasChanges())
                    {
                        writeModel.Add(new UpdateOneModel <TEntity>(filter, updateDefintion));
                    }
                }
                else if (entry.State == DbEntityEntryState.Deleted)
                {
                    var idFieldValue = EntityMapper.GetIdValue(entry.Entity);
                    var filter       = Builders <TEntity> .Filter.Eq(idFieldName, idFieldValue);

                    writeModel.Add(new DeleteOneModel <TEntity>(filter));
                }
            }

            return(writeModel);
        }
Example #4
0
 public void ProcessEntity(TEntity entity)
 {
     EntityMutation <TEntity> .MutateEntity(entity, MutatorType.Select, Database);
 }
 public void ProcessEntity(TEntity entity, IMongoDbConnection connection)
 {
     EntityMutation <TEntity> .MutateEntity(entity, MutatorType.Select, connection);
 }