/// <summary>
        /// Converts the json document to an entity.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id">The id.</param>
        /// <param name="documentFound">The document found.</param>
        /// <param name="metadata">The metadata.</param>
        /// <returns></returns>
        protected T ConvertToEntity <T>(string id, RavenJObject documentFound, RavenJObject metadata)
        {
            if (typeof(T) == typeof(RavenJObject))
            {
                return((T)(object)documentFound);
            }

            var entity       = default(T);
            var documentType = Conventions.GetClrType(id, documentFound, metadata);

            if (documentType != null)
            {
                var type = Type.GetType(documentType);
                if (type != null)
                {
                    entity = (T)documentFound.Deserialize(type, Conventions);
                }
            }
            if (Equals(entity, default(T)))
            {
                entity = documentFound.Deserialize <T>(Conventions);
                var document = entity as RavenJObject;
                if (document != null)
                {
                    entity = (T)(object)(new DynamicJsonObject(document));
                }
            }
            TrySetIdentity(entity, id);
            return(entity);
        }
Beispiel #2
0
        private void NotifySubscribers(RavenJObject curDoc, out long lastReceivedEtag)
        {
            T   instance;
            var metadata = curDoc[Constants.Metadata.Key] as RavenJObject;

            lastReceivedEtag = metadata[Constants.Metadata.Etag].Value <long>();

            if (_isStronglyTyped)
            {
                instance = curDoc.Deserialize <T>(_conventions);

                var docId = metadata[Constants.Metadata.Id].Value <string>();

                if (string.IsNullOrEmpty(docId) == false)
                {
                    _generateEntityIdOnTheClient.TrySetIdentity(instance, docId);
                }
            }
            else
            {
                instance = (T)(object)curDoc;
            }

            foreach (var subscriber in _subscribers)
            {
                _proccessingCts.Token.ThrowIfCancellationRequested();
                try
                {
                    subscriber.OnNext(instance);
                }
                catch (Exception ex)
                {
                    Logger.WarnException(
                        string.Format(
                            "Subscription #{0}. Subscriber threw an exception", _options.SubscriptionId), ex);

                    if (_options.IgnoreSubscribersErrors == false)
                    {
                        IsErroredBecauseOfSubscriber = true;
                        LastSubscriberException      = ex;

                        try
                        {
                            subscriber.OnError(ex);
                        }
                        catch (Exception)
                        {
                            // can happen if a subscriber doesn't have an onError handler - just ignore it
                        }
                        break;
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Converts the json document to an entity.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id">The id.</param>
        /// <param name="documentFound">The document found.</param>
        /// <param name="metadata">The metadata.</param>
        /// <returns></returns>
        protected object ConvertToEntity <T>(string id, RavenJObject documentFound, RavenJObject metadata)
        {
            if (typeof(T) == typeof(RavenJObject))
            {
                return((T)(object)documentFound.CloneToken());
            }

            var entity = default(T);

            EnsureNotReadVetoed(metadata);
            var documentType = Conventions.GetClrType(id, documentFound, metadata);

            if (documentType != null)
            {
                var type = Type.GetType(documentType);
                if (type != null)
                {
                    entity = (T)documentFound.Deserialize(type, Conventions);
                }
            }
            if (Equals(entity, default(T)))
            {
                entity = documentFound.Deserialize <T>(Conventions);
#if !NET_3_5
                var document = entity as RavenJObject;
                if (document != null)
                {
                    entity = (T)(object)(new DynamicJsonObject(document));
                }
#endif
            }
            TrySetIdentity(entity, id);

            foreach (var documentConversionListener in listeners.ConversionListeners)
            {
                documentConversionListener.DocumentToEntity(entity, documentFound, metadata);
            }

            return(entity);
        }
Beispiel #4
0
        private object ProjectionToInstance(RavenJObject y, Type type)
        {
            foreach (var conversionListener in listeners.ExtendedConversionListeners)
            {
                conversionListener.BeforeConversionToEntity(null, y, null);
            }
            var instance = y.Deserialize(type, Conventions);

            foreach (var conversionListener in listeners.ConversionListeners)
            {
                conversionListener.DocumentToEntity(null, instance, y, null);
            }
            foreach (var conversionListener in listeners.ExtendedConversionListeners)
            {
                conversionListener.AfterConversionToEntity(null, y, null, instance);
            }
            return(instance);
        }