Example #1
0
    private MethodInfo GetHandleMethod(IContainerDocument containerDocument) => HandleMethodCache
    .GetOrAdd(GetType(), _ => new ConcurrentDictionary <Type, MethodInfo>())
    .GetOrAdd(containerDocument.GetType(), containerDocumentType =>
    {
        var subscriberInterface = typeof(IDocumentSubscription <>)
                                  .MakeGenericType(containerDocument.GetType());

        if (subscriberInterface.IsAssignableFrom(GetType()))
        {
            return(subscriberInterface.GetMethod(nameof(HandleAsync), new Type[] { containerDocument.GetType(), typeof(DocumentSubscriptionEvent) }));
        }

        return(null);
    });
Example #2
0
    public virtual async Task ExpandAsync(IContainerDocument document)
    {
        if (document is null)
        {
            throw new ArgumentNullException(nameof(document));
        }

        if (CanExpand(document))
        {
            var stopwatch = Stopwatch.StartNew();

            var expandMethod = expandMethods.GetOrAdd(GetLookupKey(document), _ =>
                                                      typeof(IDocumentExpander <>).MakeGenericType(document.GetType()).GetMethod(nameof(ExpandAsync), new Type[] { document.GetType() }));

            var expandTask = (Task)expandMethod.Invoke(this, new object[] { document });

            await expandTask.ConfigureAwait(false);

            if (!expandMetric.TrackValue(stopwatch.ElapsedMilliseconds, this.GetType().Name, document.GetType().Name))
            {
                telemetryClient.TrackTrace($"Data series or dimension cap was reached for metric {expandMetric.Identifier.MetricId}.", SeverityLevel.Error);
            }
        }
        else
        {
            throw new NotImplementedException($"Missing document expander implementation IDocumentExpander<{document.GetType().Name}> at {GetType()}");
        }
    }
Example #3
0
    public virtual bool CanExpand(IContainerDocument document)
    {
        if (document is null)
        {
            throw new ArgumentNullException(nameof(document));
        }

        return(expandableDocumentTypes.GetOrAdd(GetLookupKey(document), _ =>
                                                typeof(IDocumentExpander <>).MakeGenericType(document.GetType()).IsAssignableFrom(GetType())));
    }
Example #4
0
 private string GetLookupKey(IContainerDocument document)
 => $"{this.GetType()}|{document.GetType()}";
Example #5
0
    public virtual Task HandleAsync(IContainerDocument containerDocument, DocumentSubscriptionEvent subscriptionEvent)
    {
        if (containerDocument is null)
        {
            throw new ArgumentNullException(nameof(containerDocument));
        }

        if (CanHandle(containerDocument))
        {
            return((Task)GetHandleMethod(containerDocument).Invoke(this, new object[] { containerDocument, subscriptionEvent }));
        }

        throw new NotImplementedException($"Missing document subscription implementation IDocumentSubscription<{containerDocument.GetType().Name}> at {GetType()}");
    }