예제 #1
0
        public void When_an_exception_is_thrown_by_a_projector_then_it_continues_to_receive_events()
        {
            var count = 0;

            bus.Subscribe(Projector.Create <Order.Paid>(e =>
            {
                count++;
                if (count > 8)
                {
                    throw new Exception("oops!");
                }
            }));

            var events = Enumerable.Range(1, 10).Select(i => new Order.Paid(i)).ToArray();

            using (bus.PublishAsync(events).Subscribe())
            {
                count.Should().Be(10);
            }
        }
예제 #2
0
        private List <ODataEntity> ProcessOperationCollectionResponse(IEnumerable <Content> inputContents,
                                                                      ODataRequest req, HttpContext httpContext, out int count)
        {
            var x = ProcessODataFilters(inputContents, req, out var totalCount);

            var outContents = new List <ODataEntity>();
            var projector   = Projector.Create(req, true);

            foreach (var content in x)
            {
                var fields = CreateFieldDictionary(content, projector, httpContext);
                outContents.Add(fields);
            }

            count = totalCount ?? outContents.Count;
            if (req.CountOnly)
            {
                return(null);
            }
            return(outContents);
        }
예제 #3
0
        private async Task WriteActionsAsync(ODataActionItem[] actionItems, HttpContext httpContext, ODataRequest req)
        {
            if (actionItems == null)
            {
                return;
            }

            var projector = Projector.Create(req, true);

            var enumerable = Content.CreateCollection(actionItems, ODataActionItem.Ctd);

            var filteredEnumerable = new FilteredContentEnumerable(enumerable,
                                                                   (LambdaExpression)req.Filter, req.Sort, req.Top, req.Skip);

            var contents = filteredEnumerable
                           .Select(x => CreateFieldDictionary(x, projector, httpContext))
                           .ToArray();

            var count = req.InlineCount == InlineCount.AllPages ? filteredEnumerable.AllCount : contents.Length;

            await WriteMultipleContentAsync(httpContext, contents, count).ConfigureAwait(false);
        }
예제 #4
0
        private async Task WriteMultiRefContentsAsync(object references, HttpContext httpContext, ODataRequest req)
        {
            if (references == null)
            {
                return;
            }

            var node      = references as Node;
            var projector = Projector.Create(req, true);

            if (node != null)
            {
                var contents = new List <ODataEntity>
                {
                    CreateFieldDictionary(Content.Create(node), projector, httpContext)
                };
                //TODO: ODATA: multiref item: get available types from reference property
                await WriteMultipleContentAsync(httpContext, contents, 1)
                .ConfigureAwait(false);
            }
            else
            {
                if (references is IEnumerable enumerable)
                {
                    var filteredEnumerable = new FilteredContentEnumerable(enumerable,
                                                                           (LambdaExpression)req.Filter, req.Sort, req.Top, req.Skip);

                    var contents = filteredEnumerable
                                   .Select(x => CreateFieldDictionary(x, projector, httpContext))
                                   .ToArray();

                    var count = req.InlineCount == InlineCount.AllPages ? filteredEnumerable.AllCount : contents.Length;

                    await WriteMultipleContentAsync(httpContext, contents, count).ConfigureAwait(false);
                }
            }
        }
예제 #5
0
        private IEnumerable <ODataEntity> ProcessOperationQueryResponse(ChildrenDefinition qdef, ODataRequest req, HttpContext httpContext, out int count)
        {
            var queryText = qdef.ContentQuery;

            if (queryText.Contains("}}"))
            {
                queryText = ContentQuery.ResolveInnerQueries(qdef.ContentQuery, new QuerySettings
                {
                    EnableAutofilters    = qdef.EnableAutofilters,
                    EnableLifespanFilter = qdef.EnableLifespanFilter,
                    QueryExecutionMode   = qdef.QueryExecutionMode,
                    Sort = qdef.Sort
                });
            }

            var cdef = new ChildrenDefinition
            {
                PathUsage            = qdef.PathUsage,
                ContentQuery         = queryText,
                Top                  = req.Top > 0 ? req.Top : qdef.Top,
                Skip                 = req.Skip > 0 ? req.Skip : qdef.Skip,
                Sort                 = req.Sort != null && req.Sort.Any() ? req.Sort : qdef.Sort,
                CountAllPages        = req.HasInlineCount ? req.InlineCount == InlineCount.AllPages : qdef.CountAllPages,
                EnableAutofilters    = req.AutofiltersEnabled != FilterStatus.Default ? req.AutofiltersEnabled : qdef.EnableAutofilters,
                EnableLifespanFilter = req.LifespanFilterEnabled != FilterStatus.Default ? req.AutofiltersEnabled : qdef.EnableLifespanFilter,
                QueryExecutionMode   = req.QueryExecutionMode != QueryExecutionMode.Default ? req.QueryExecutionMode : qdef.QueryExecutionMode,
            };

            var snQuery = SnExpression.BuildQuery(req.Filter, typeof(Content), null, cdef);

            if (cdef.EnableAutofilters != FilterStatus.Default)
            {
                snQuery.EnableAutofilters = cdef.EnableAutofilters;
            }
            if (cdef.EnableLifespanFilter != FilterStatus.Default)
            {
                snQuery.EnableLifespanFilter = cdef.EnableLifespanFilter;
            }
            if (cdef.QueryExecutionMode != QueryExecutionMode.Default)
            {
                snQuery.QueryExecutionMode = cdef.QueryExecutionMode;
            }

            var result = snQuery.Execute(new SnQueryContext(null, User.Current.Id));

            // for optimization purposes this combined condition is examined separately
            if (req.InlineCount == InlineCount.AllPages && req.CountOnly)
            {
                count = result.TotalCount;
                return(null);
            }

            var ids = result.Hits.ToArray();

            count = req.InlineCount == InlineCount.AllPages ? result.TotalCount : ids.Length;
            if (req.CountOnly)
            {
                return(null);
            }

            var contents   = new List <ODataEntity>();
            var projector  = Projector.Create(req, true);
            var missingIds = new List <int>();

            foreach (var id in ids)
            {
                var content = Content.Load(id);
                if (content == null)
                {
                    // collect missing ids for logging purposes
                    missingIds.Add(id);
                    continue;
                }

                var fields = CreateFieldDictionary(content, projector, httpContext);
                contents.Add(fields);
            }

            if (missingIds.Count > 0)
            {
                // subtract missing count from result count
                count = Math.Max(0, count - missingIds.Count);

                // index anomaly: there are ids in the index that could not be loaded from the database
                SnLog.WriteWarning("Missing ids found in the index that could not be loaded from the database. See id list below.",
                                   EventId.Indexing,
                                   properties: new ODataEntity
                {
                    { "MissingIds", string.Join(", ", missingIds.OrderBy(id => id)) }
                });
            }

            return(contents);
        }
예제 #6
0
        private ODataEntity CreateFieldDictionary(Content content, bool isCollectionItem, HttpContext httpContext)
        {
            var projector = Projector.Create(this.ODataRequest, isCollectionItem, content);

            return(projector.Project(content, httpContext));
        }
예제 #7
0
        public void EventHandler_Name_returns_something_vaguely_informative_for_anonymous_projectors()
        {
            var wrapper = Projector.Create <Order.ItemAdded>(e => { });

            EventHandler.Name(wrapper).Should().Be("AnonymousProjector");
        }