Пример #1
0
        /// <summary>Implements basic check and convertion logic for ConvertFromJson method.</summary>
        protected static object CheckAndConvertFromJson(
            IEntityConfig entityConfig, JsonObject source, bool throwOnError, Func <JsonObject, object> convert)
        {
            var documentObject = source.DeepClone();
            var documentId     = documentObject.GetPrimitiveProperty <string>(Document.IdPropertyName);
            var revision       = documentObject.GetPrimitiveProperty <string>(Document.RevisionPropertyName);

            var documentType = documentObject.GetPrimitiveProperty <string>(Document.TypePropertyName);

            if (documentType != null)
            {
                documentObject.Remove(Document.TypePropertyName);
            }

            if (documentType.HasNoValue())
            {
                return(LogAndThrowIfNeeded <object>(throwOnError, new DocumentTypeMissingException(documentObject.ToString())));
            }

            if (entityConfig.DocumentType != documentType)
            {
                return(LogAndThrowInvalidOperationExceptionIfNeeded <object>(
                           throwOnError,
                           "Deserializing document's type {0} does not match type {1} in configuration.", documentType,
                           entityConfig.DocumentType
                           ));
            }

            if (string.IsNullOrWhiteSpace(documentId))
            {
                return(LogAndThrowIfNeeded <object>(throwOnError, new DocumentIdMissingException(documentObject.ToString())));
            }

            if (string.IsNullOrWhiteSpace(revision))
            {
                return(LogAndThrowIfNeeded <object>(throwOnError, new DocumentRevisionMissingException(documentObject.ToString())));
            }

            var entityId = entityConfig.ConvertDocumentIdToEntityId(documentId);

            if (string.IsNullOrWhiteSpace(entityId))
            {
                return(LogAndThrowInvalidOperationExceptionIfNeeded <object>(
                           throwOnError,
                           "IEntityConfig.ConvertDocumentIdToEntityId() should not ever return null, empty or whitespace string."
                           ));
            }

            var entity = convert(documentObject);

            entityConfig.SetId(entity, entityId);
            entityConfig.SetRevision(entity, revision);
            return(entity);
        }
Пример #2
0
        private static void GenerateIdIfNeeded(object entity, IEntityConfig entityConfiguration, IIdGenerator idGenerator)
        {
            var id = entityConfiguration.GetId(entity);

            if (id == null)
            {
                var generatedId = idGenerator.GenerateId();
                Debug.Assert(!string.IsNullOrEmpty(generatedId));
                entityConfiguration.SetId(entity, generatedId);
            }
        }