public CompositeObject(byte[] serializedObject, IBusinessObjectDefinitionProvider businessObjectDefinitionProvider) { var stream = new MemoryStream(serializedObject); var header = Serializer.DeserializeWithLengthPrefix <CompositeObjectHeader>(stream, PrefixStyle.Base128, 1); Id = header.Id; CompositeObjectTypeId = header.CompositeObjectTypeId; CreatedByUserId = header.CreatedByUserId; DateTimeCreated = new DateTimeOffset(header.DateTimeCreated, new TimeSpan(0)); LastUpdatedByUserId = header.LastUpdatedByUserId; LastModified = new DateTimeOffset(header.DateTimeLastUpdated, new TimeSpan(0)); Revision = header.Revision; compositeObjectDefinition = businessObjectDefinitionProvider.GetCompositeObjectDefinition(CompositeObjectTypeId); for (var i = 0; i < header.AttributeCount; i++) { var attribute = Serializer.DeserializeWithLengthPrefix <AttributeValue>(stream, PrefixStyle.Base128, 2); if (compositeObjectDefinition.Attributes.ContainsKey(attribute.Name)) { var attributeDefinition = compositeObjectDefinition.Attributes[attribute.Name]; attributes.Add(attributeDefinition, attributeDefinition.Serializer.Deserialize(attributeDefinition.Name, attributeDefinition.AttributeType, attribute.Value)); } } for (var i = 0; i < header.SimpleObjectCount; i++) { var serializedSimpleObject = Serializer.DeserializeWithLengthPrefix <byte[]>(stream, PrefixStyle.Base128, 3); var simpleObject = new SimpleObject(serializedSimpleObject, businessObjectDefinitionProvider, CompositeObjectTypeId); simpleObjects.Add(simpleObject.Id, simpleObject); } Attributes = new ReadOnlyDictionary <AttributeDefinition, object>(attributes); SimpleObjects = new ReadOnlyDictionary <Guid, SimpleObject>(simpleObjects); }
/// <summary> /// Applying a changeset. This method is NOT thread safe. /// </summary> /// <param name="changeSet"></param> /// <param name="definitionProvider"></param> public void ApplyChangeSet(CompositeObjectChangeSet changeSet, IBusinessObjectDefinitionProvider definitionProvider) { if (appliedChangeSets.Contains(changeSet.ChangeSetId)) { return; } foreach (var modification in changeSet.AttributeValues) { if (!compositeObjectDefinition.Attributes.ContainsKey(modification.Name)) { continue; } var attribute = compositeObjectDefinition.Attributes[modification.Name]; attributes[attribute] = attribute.Serializer.Deserialize(attribute.Name, attribute.AttributeType, modification.Value); } var lastModified = new DateTimeOffset(changeSet.Timestamp, new TimeSpan(0)); var changeSetUserId = changeSet.UserId ?? Guid.Empty; // TODO: a hack, changeSet.UserId should not be nullable foreach (var simpleObjectChangeSet in changeSet.ChildObjectChangeSets) { if (simpleObjectChangeSet.Action == SimpleObjectChangeSetType.Delete && simpleObjects.ContainsKey(simpleObjectChangeSet.SimpleObjectId)) { simpleObjects.Remove(simpleObjectChangeSet.SimpleObjectId); } else if (simpleObjectChangeSet.Action == SimpleObjectChangeSetType.UpdateParent && simpleObjects.ContainsKey(simpleObjectChangeSet.SimpleObjectId)) { var simpleObject = simpleObjects[simpleObjectChangeSet.SimpleObjectId]; var updateSimpleObjectParentChangeSet = (UpdateSimpleObjectParentChangeSet)simpleObjectChangeSet; simpleObject.UpdateParentId(updateSimpleObjectParentChangeSet.ParentId); } else { SimpleObject simpleObject; var createOrUpdateSimpleObjectCommand = simpleObjectChangeSet as CreateOrUpdateSimpleObjectChangeSet; SimpleObjectDefinition simpleObjectDefinition; if (simpleObjectChangeSet.Action == SimpleObjectChangeSetType.Create) { var commandCreate = (CreateSimpleObjectChangeSet)simpleObjectChangeSet; simpleObjectDefinition = definitionProvider.FindSimpleObjectDefinition(CompositeObjectTypeId, commandCreate.SimpleObjectType); if (simpleObjectDefinition == null) { continue; } simpleObject = new SimpleObject(commandCreate.SimpleObjectId, commandCreate.ParentId, simpleObjectDefinition, changeSetUserId, lastModified, changeSet.ChangeSetId); } else if (simpleObjects.ContainsKey(simpleObjectChangeSet.SimpleObjectId)) { simpleObject = simpleObjects[simpleObjectChangeSet.SimpleObjectId]; simpleObjectDefinition = simpleObject.SimpleObjectDefinition; } else { continue; } try { simpleObject.ApplyChangeSet(createOrUpdateSimpleObjectCommand, simpleObjectDefinition, changeSet.ChangeSetId, lastModified, changeSetUserId); } catch (Exception ex) { simpleObjectChangeSetExceptions.Add(new SimpleObjectChangeSetExceptionRecord(simpleObjectChangeSet, ex)); } if (!simpleObjects.ContainsKey(simpleObjectChangeSet.SimpleObjectId)) { simpleObjects.Add(simpleObjectChangeSet.SimpleObjectId, simpleObject); } } } appliedChangeSets.Add(changeSet.ChangeSetId); LastModified = lastModified; LastUpdatedByUserId = changeSetUserId; Revision = changeSet.ChangeSetId; }