private void Apply(ContentPartDefinition model, ContentPartRecord record) {
            record.Settings = _settingsFormatter.Map(model.Settings).ToString();

            var toRemove = record.ContentPartFieldRecords
                .Where(partFieldDefinitionRecord => model.Fields.All(partField => partFieldDefinitionRecord.Name != partField.Name))
                .ToList();

            foreach (var remove in toRemove) {
                record.ContentPartFieldRecords.Remove(remove);
            }

            foreach (var field in model.Fields) {
                var fieldName = field.Name;
                var partFieldRecord = record.ContentPartFieldRecords.SingleOrDefault(r => r.Name == fieldName);
                if (partFieldRecord == null) {
                    partFieldRecord = new ContentPartFieldRecord {
                        ContentFieldRecord = Acquire(field.FieldDefinition),
                        Name = field.Name
                    };
                    record.ContentPartFieldRecords.Add(partFieldRecord);
                }
                Apply(field, partFieldRecord);
            }
        }
 ContentPartDefinition Build(ContentPartRecord source) {
     return new ContentPartDefinition(
         source.Name,
         source.ContentPartFieldRecords.Select(Build),
         _settingsFormatter.Map(Parse(source.Settings)));
 }
 private ContentPartRecord Acquire(ContentPartDefinition contentPartDefinition) {
     var result = _partDefinitionRepository.Table.SingleOrDefault(x => x.Name == contentPartDefinition.Name);
     if (result == null) {
         result = new ContentPartRecord { Name = contentPartDefinition.Name };
         _partDefinitionRepository.Create(result);
     }
     return result;
 }