Exemplo n.º 1
0
        /// <summary>
        /// Converts a BlittableJsonReaderObject to an entity without a session.
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="id">The id.</param>
        /// <param name="document">The document found.</param>
        /// <param name="conventions">The conventions.</param>
        /// <returns>The converted entity</returns>
        public static object ConvertToEntity(Type entityType, string id, BlittableJsonReaderObject document, DocumentConventions conventions)
        {
            try
            {
                var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(entityType);
                var entity       = defaultValue;

                var documentType = conventions.GetClrType(id, document);
                if (documentType != null)
                {
                    var type = Type.GetType(documentType);
                    if (type != null && entityType.IsAssignableFrom(type))
                    {
                        entity = conventions.DeserializeEntityFromBlittable(type, document);
                    }
                }

                if (Equals(entity, defaultValue))
                {
                    entity = conventions.DeserializeEntityFromBlittable(entityType, document);
                }

                return(entity);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Could not convert document {id} to entity of type {entityType}",
                                                    ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts a BlittableJsonReaderObject to an entity.
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="id">The id.</param>
        /// <param name="document">The document found.</param>
        /// <returns>The converted entity</returns>
        public object ConvertToEntity(Type entityType, string id, ref BlittableJsonReaderObject document, bool trackEntity)
        {
            try
            {
                if (entityType == typeof(BlittableJsonReaderObject))
                {
                    return(document);
                }

                _session.OnBeforeConversionToEntityInvoke(id, entityType, ref document);

                var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(entityType);
                var entity       = defaultValue;

                ExtensionDataSetter dataSetter = null;
                if (trackEntity)
                {
                    dataSetter = RegisterMissingProperties;
                }

                using (DefaultRavenContractResolver.RegisterExtensionDataSetter(dataSetter))
                {
                    var documentType = _session.Conventions.GetClrType(id, document);
                    if (documentType != null)
                    {
                        var type = Type.GetType(documentType);
                        if (type != null && entityType.IsAssignableFrom(type))
                        {
                            entity = _session.Conventions.DeserializeEntityFromBlittable(type, document);
                        }
                    }

                    if (Equals(entity, defaultValue))
                    {
                        entity = _session.Conventions.DeserializeEntityFromBlittable(entityType, document);
                    }
                }

                if (id != null)
                {
                    _session.GenerateEntityIdOnTheClient.TrySetIdentity(entity, id);
                }

                _session.OnAfterConversionToEntityInvoke(id, document, entity);

                return(entity);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Could not convert document {id} to entity of type {entityType}",
                                                    ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts a BlittableJsonReaderObject to an entity.
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="id">The id.</param>
        /// <param name="document">The document found.</param>
        /// <returns>The converted entity</returns>
        public object ConvertToEntity(Type entityType, string id, BlittableJsonReaderObject document)
        {
            //TODO -  Add RegisterMissingProperties ???
            try
            {
                if (entityType == typeof(BlittableJsonReaderObject))
                {
                    return(document);
                }

                var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(entityType);
                var entity       = defaultValue;

                var documentType = _session.Conventions.GetClrType(id, document);
                if (documentType != null)
                {
                    var type = Type.GetType(documentType);
                    if (type != null && entityType.IsAssignableFrom(type))
                    {
                        entity = _session.Conventions.DeserializeEntityFromBlittable(type, document);
                    }
                }

                if (Equals(entity, defaultValue))
                {
                    entity = _session.Conventions.DeserializeEntityFromBlittable(entityType, document);
                }

                if (id != null)
                {
                    _session.GenerateEntityIdOnTheClient.TrySetIdentity(entity, id);
                }

                return(entity);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Could not convert document {id} to entity of type {entityType}",
                                                    ex);
            }
        }