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

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

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

            foreach (var field in model.Fields) {
                var fieldName = field.Name;
                var partFieldRecord = record.ContentPartFieldDefinitionRecords.SingleOrDefault(r => r.Name == fieldName);
                if (partFieldRecord == null) {
                    partFieldRecord = new ContentPartFieldDefinitionRecord {
                        ContentFieldDefinitionRecord = Acquire(field.FieldDefinition),
                        Name = field.Name
                    };
                    record.ContentPartFieldDefinitionRecords.Add(partFieldRecord);
                }
                Apply(field, partFieldRecord);
            }
        }
 ContentPartDefinition Build(ContentPartDefinitionRecord source)
 {
     return new ContentPartDefinition(
         source.Name,
         source.ContentPartFieldDefinitionRecords.Select(Build),
         _settingsFormatter.Map(Parse(source.Settings)));
 }
        private ContentPartDefinitionRecord Acquire(ContentPartDefinition contentPartDefinition)
        {
            var result = _contentStorageManager
                .Query<ContentPartDefinitionRecord>(x => x.Name == contentPartDefinition.Name)
                .SingleOrDefault();

            if (result == null) {
                result = new ContentPartDefinitionRecord { Name = contentPartDefinition.Name };
                _contentStorageManager.Store(result);
            }
            return result;
        }