示例#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);
        }