public static IServiceCollection AddBookingPipelines(this IServiceCollection services)
        {
            var sp = services.BuildServiceProvider();

            // Create each step of the pipeline
            var mapping     = new MappingStep(sp.GetRequiredService <ILogger <MappingStep> >());
            var getRequest  = new GetRequestStep(sp.GetRequiredService <ILogger <GetRequestStep> >());
            var getResponse = new GetResponseStep(sp.GetRequiredService <ILogger <GetResponseStep> >());
            var aggregate   = new AggregateStep(sp.GetRequiredService <ILogger <AggregateStep> >());
            var dedupe      = new DedupeStep(sp.GetRequiredService <ILogger <DedupeStep> >());

            // Build the pipeline using the steps, in the desired order
            var searchPipeline = new Pipeline <HubRequest, HubResponse>(hubRq => hubRq
                                                                        .AddStep(mapping)
                                                                        // Sub-pipeline with three steps, two of them also use the input from parent
                                                                        .AddStep(new Pipeline <ConnectorRequest, ConnectorResponse>(connRq => connRq
                                                                                                                                    .AddStep(getRequest, hubRq)
                                                                                                                                    .AddStep(getResponse, hubRq)
                                                                                                                                    .AddStep(aggregate))
                                                                                 )
                                                                        .AddStep(dedupe));

            // Inject this pipeline to the service provider (it is used in the controller)
            return(services.AddSingleton <IPipelineStep <HubRequest, HubResponse> >(searchPipeline));
        }
Exemple #2
0
        public void CreateAggregate(string name, int numberOfArguments, AggregateStep step, AggregateFinal final)
        {
            name = name.ToUpperInvariant();

            var nativeAggregateStep = new AggregateStepNative((context, numberArguments, nativeArguments) =>
            {
                var contextGuidPtr = this.sqlite3Provider.Sqlite3AggregateContext(context, 16);

                if (contextGuidPtr != IntPtr.Zero)
                {
                    var contextGuidBytes = new byte[16];
                    this.platformMarshal.Copy(contextGuidPtr, contextGuidBytes, 0, 16);
                    var contextGuid = new Guid(contextGuidBytes);

                    if (contextGuid == Guid.Empty)
                    {
                        contextGuid      = Guid.NewGuid();
                        contextGuidBytes = contextGuid.ToByteArray();
                        this.platformMarshal.Copy(contextGuidBytes, contextGuidPtr, 0, 16);
                    }

                    object[] mangedArguments = ObtainManagedArguments(nativeArguments);

                    // TODO
                    if (!this.aggregateContextDataDic.ContainsKey(contextGuid))
                    {
                        this.aggregateContextDataDic[contextGuid] = new Dictionary <string, object>();
                    }

                    var aggregateContextData = this.aggregateContextDataDic[contextGuid];
                    try
                    {
                        step.Invoke(aggregateContextData, mangedArguments);
                    }
                    catch (Exception ex)
                    {
                        SetError(context, ex);
                    }
                }
                else
                {
                    SetError(context, new Exception("Unable to initialize aggregate context."));
                }
            });

            var aggregateStepDelegate = this.platformMarshal.ApplyNativeCallingConventionToAggregateStep(nativeAggregateStep);

            this.aggregateStepDelegates[name] = aggregateStepDelegate;

            var aggregateStepPtr = this.platformMarshal.MarshalDelegateToNativeFunctionPointer(aggregateStepDelegate);

            var nativeAggregateFinal = new AggregateFinalNative((context) =>
            {
                IDictionary <string, object> aggregateContextData = new Dictionary <string, object>();

                var contextGuidPtr = this.sqlite3Provider.Sqlite3AggregateContext(context, 0);

                if (contextGuidPtr != IntPtr.Zero)
                {
                    var contextGuidBytes = new byte[16];
                    this.platformMarshal.Copy(contextGuidPtr, contextGuidBytes, 0, 16);
                    var contextGuid = new Guid(contextGuidBytes);

                    aggregateContextData = this.aggregateContextDataDic[contextGuid];
                }

                try
                {
                    var result = final.Invoke(aggregateContextData);
                    SetResult(context, result);
                }
                catch (Exception ex)
                {
                    SetError(context, ex);
                }
            });

            var aggregateFinalDelegate = this.platformMarshal.ApplyNativeCallingConventionToAggregateFinal(nativeAggregateFinal);

            this.aggregateFinalDelegates[name] = aggregateFinalDelegate;

            var aggregateFinalPtr = this.platformMarshal.MarshalDelegateToNativeFunctionPointer(aggregateFinalDelegate);

            int nameLength;
            var namePtr = this.platformMarshal.MarshalStringManagedToNativeUTF8(name, out nameLength);

            try
            {
                this.sqlite3Provider.Sqlite3CreateAggregate(this.db, namePtr, numberOfArguments, aggregateStepPtr, aggregateFinalPtr);
            }
            finally
            {
                if (namePtr != IntPtr.Zero)
                {
                    this.platformMarshal.CleanUpStringNativeUTF8(namePtr);
                }
            }
        }
        public void CreateAggregate(string name, int numberOfArguments, AggregateStep step, AggregateFinal final)
        {
            name = name.ToUpperInvariant();

            var nativeAggregateStep = new AggregateStepNative((context, numberArguments, nativeArguments) =>
            {
                var contextGuidPtr = this.sqlite3Provider.Sqlite3AggregateContext(context, 16);

                if (contextGuidPtr != IntPtr.Zero)
                {
                    var contextGuidBytes = new byte[16];
                    this.platformMarshal.Copy(contextGuidPtr, contextGuidBytes, 0, 16);
                    var contextGuid = new Guid(contextGuidBytes);

                    if (contextGuid == Guid.Empty)
                    {
                        contextGuid = Guid.NewGuid();
                        contextGuidBytes = contextGuid.ToByteArray();
                        this.platformMarshal.Copy(contextGuidBytes, contextGuidPtr, 0, 16);
                    }

                    object[] mangedArguments = ObtainManagedArguments(nativeArguments);

                    // TODO
                    if (!this.aggregateContextDataDic.ContainsKey(contextGuid))
                    {
                        this.aggregateContextDataDic[contextGuid] = new Dictionary<string, object>();
                    }

                    var aggregateContextData = this.aggregateContextDataDic[contextGuid];
                    try
                    {
                        step.Invoke(aggregateContextData, mangedArguments);
                    }
                    catch (Exception ex)
                    {
                        SetError(context, ex);
                    }
                }
                else
                {
                    SetError(context, new Exception("Unable to initialize aggregate context."));
                }
            });

            var aggregateStepDelegate = this.platformMarshal.ApplyNativeCallingConventionToAggregateStep(nativeAggregateStep);
            this.aggregateStepDelegates[name] = aggregateStepDelegate;

            var aggregateStepPtr = this.platformMarshal.MarshalDelegateToNativeFunctionPointer(aggregateStepDelegate);

            var nativeAggregateFinal = new AggregateFinalNative((context) =>
            {
                IDictionary<string, object> aggregateContextData = new Dictionary<string, object>();

                var contextGuidPtr = this.sqlite3Provider.Sqlite3AggregateContext(context, 0);

                if (contextGuidPtr != IntPtr.Zero)
                {
                    var contextGuidBytes = new byte[16];
                    this.platformMarshal.Copy(contextGuidPtr, contextGuidBytes, 0, 16);
                    var contextGuid = new Guid(contextGuidBytes);

                    aggregateContextData = this.aggregateContextDataDic[contextGuid];
                }

                try
                {
                    var result = final.Invoke(aggregateContextData);
                    SetResult(context, result);
                }
                catch (Exception ex)
                {
                    SetError(context, ex);
                }
            });

            var aggregateFinalDelegate = this.platformMarshal.ApplyNativeCallingConventionToAggregateFinal(nativeAggregateFinal);
            this.aggregateFinalDelegates[name] = aggregateFinalDelegate;

            var aggregateFinalPtr = this.platformMarshal.MarshalDelegateToNativeFunctionPointer(aggregateFinalDelegate);

            int nameLength;
            var namePtr = this.platformMarshal.MarshalStringManagedToNativeUTF8(name, out nameLength);

            try
            {
                this.sqlite3Provider.Sqlite3CreateAggregate(this.db, namePtr, numberOfArguments, aggregateStepPtr, aggregateFinalPtr);
            }
            finally
            {
                if (namePtr != IntPtr.Zero)
                {
                    this.platformMarshal.CleanUpStringNativeUTF8(namePtr);
                }
            }
        }