Esempio n. 1
0
        // Bind a T.
        private static SimpleTriggerArgumentBinding <TMessage, TTriggerValue> GetTriggerArgumentElementBinding <TMessage, TTriggerValue>(
            Type elementType,
            ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy,
            IConverterManager converterManager)
        {
            var argumentBinding = GetDirectTriggerBinding <TMessage, TTriggerValue>(elementType, bindingStrategy, converterManager);

            if (argumentBinding != null)
            {
                // Exact match in converter manager. Always takes precedence.
                return(argumentBinding);
            }

            if (elementType == typeof(TMessage))
            {
                return(new SimpleTriggerArgumentBinding <TMessage, TTriggerValue>(bindingStrategy, converterManager));
            }
            if (elementType == typeof(string))
            {
                return(new StringTriggerArgumentBinding <TMessage, TTriggerValue>(bindingStrategy, converterManager));
            }
            else
            {
                // Catch-all.
                // Default, assume a Poco
                return(new PocoTriggerArgumentBinding <TMessage, TTriggerValue>(bindingStrategy, converterManager, elementType));
            }
        }
        /// <summary>
        /// Bind a  parameter to an IAsyncCollector. Use this for things that have discrete output items (like sending messages or writing table rows)
        /// This will add additional adapters to connect the user's parameter type to an IAsyncCollector.
        /// </summary>
        /// <typeparam name="TMessage">The 'core type' for the IAsyncCollector.</typeparam>
        /// <typeparam name="TTriggerValue">The type of the trigger object to pass to the listener.</typeparam>
        /// <param name="bindingStrategy">A strategy object that describes how to do the binding</param>
        /// <param name="parameter">The user's parameter being bound to</param>
        /// <param name="converterManager">The converter manager, used to convert between the user parameter's type and the underlying native types used by the trigger strategy</param>
        /// <param name="createListener">A function to create the underlying listener for this parameter</param>
        /// <returns>A trigger binding</returns>
        public static ITriggerBinding GetTriggerBinding <TMessage, TTriggerValue>(
            ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy,
            ParameterInfo parameter,
            IConverterManager converterManager,
            Func <ListenerFactoryContext, bool, Task <IListener> > createListener)
        {
            if (bindingStrategy == null)
            {
                throw new ArgumentNullException("bindingStrategy");
            }
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            bool singleDispatch;
            var  argumentBinding = BindingFactoryHelpers.GetTriggerArgumentBinding(bindingStrategy, parameter, converterManager, out singleDispatch);

            var parameterDescriptor = new ParameterDescriptor
            {
                Name         = parameter.Name,
                DisplayHints = new ParameterDisplayHints
                {
                    Description = singleDispatch ? "message" : "messages"
                }
            };

            ITriggerBinding binding = new StrategyTriggerBinding <TMessage, TTriggerValue>(
                bindingStrategy, argumentBinding, createListener, parameterDescriptor, singleDispatch);

            return(binding);
        }
        // Bind a trigger argument to various parameter types.
        // Handles either T or T[],
        internal static ITriggerDataArgumentBinding <TTriggerValue> GetTriggerArgumentBinding <TMessage, TTriggerValue>(
            ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy,
            ParameterInfo parameter,
            IConverterManager converterManager,
            out bool singleDispatch)
        {
            ITriggerDataArgumentBinding <TTriggerValue> argumentBinding = null;

            if (parameter.ParameterType.IsArray)
            {
                // dispatch the entire batch in a single call.
                singleDispatch = false;

                var elementType = parameter.ParameterType.GetElementType();

                var innerArgumentBinding = GetTriggerArgumentElementBinding <TMessage, TTriggerValue>(elementType, bindingStrategy, converterManager);

                argumentBinding = new ArrayTriggerArgumentBinding <TMessage, TTriggerValue>(bindingStrategy, innerArgumentBinding, converterManager);

                return(argumentBinding);
            }
            else
            {
                // Dispatch each item one at a time
                singleDispatch = true;

                var elementType = parameter.ParameterType;
                argumentBinding = GetTriggerArgumentElementBinding <TMessage, TTriggerValue>(elementType, bindingStrategy, converterManager);
                return(argumentBinding);
            }
        }
Esempio n. 4
0
 public ArrayTriggerArgumentBinding(
     ITriggerBindingStrategy <TMessage, TTriggerValue> hooks,
     SimpleTriggerArgumentBinding <TMessage, TTriggerValue> innerBinding,
     IConverterManager converterManager) : base(hooks, converterManager, false)
 {
     this._innerBinding = innerBinding;
 }
 public SimpleTriggerArgumentBinding(ITriggerBindingStrategy <TMessage, TTriggerValue> hooks, IConverterManager converterManager)
 {
     this._hooks       = hooks;
     this.Contract     = Hooks.GetStaticBindingContract();
     this.ElementType  = typeof(TMessage);
     _converterManager = converterManager;
 }
Esempio n. 6
0
 public SimpleTriggerArgumentBinding(ITriggerBindingStrategy <TMessage, TTriggerValue> hooks, IConverterManager converterManager, bool isSingleDispatch = true)
 {
     this._hooks       = hooks;
     this.Contract     = Hooks.GetBindingContract(isSingleDispatch);
     this.ElementType  = typeof(TMessage);
     _converterManager = converterManager;
     _stringConverter  = _converterManager.GetConverter <TMessage, string, Attribute>();
 }
 public CustomTriggerArgumentBinding(
     ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy,
     IConverterManager converterManager,
     FuncConverter <TMessage, Attribute, TUserType> converter) :
     base(bindingStrategy, converterManager)
 {
     if (converter == null)
     {
         throw new ArgumentNullException("converter");
     }
     this._converter  = converter;
     this.ElementType = typeof(TUserType);
 }
Esempio n. 8
0
 public StrategyTriggerBinding(
     ITriggerBindingStrategy <TMessage, TTriggerValue> hooks,
     ITriggerDataArgumentBinding <TTriggerValue> argumentBinding,
     Func <ListenerFactoryContext, bool, Task <IListener> > createListener,
     ParameterDescriptor parameterDescriptor,
     bool singleDispatch)
 {
     this._hooks               = hooks;
     this._argumentBinding     = argumentBinding;
     this._createListener      = createListener;
     this._parameterDescriptor = parameterDescriptor;
     this._singleDispatch      = singleDispatch;
 }
Esempio n. 9
0
        GetDirectTriggerBindingWorker <TMessage, TTriggerValue, TUserType>(
            ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy,
            IConverterManager converterManager)
        {
            var directConvert = converterManager.GetConverter <TMessage, TUserType, Attribute>();

            if (directConvert != null)
            {
                var argumentBinding = new CustomTriggerArgumentBinding <TMessage, TTriggerValue, TUserType>(
                    bindingStrategy, converterManager, directConvert);
                return(argumentBinding);
            }
            return(null);
        }
Esempio n. 10
0
        // If a conversion function exists from TMessage --> exactType, then use it.
        // else return null.
        private static SimpleTriggerArgumentBinding <TMessage, TTriggerValue> GetDirectTriggerBinding <TMessage, TTriggerValue>(
            Type exactType,
            ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy,
            IConverterManager converterManager)
        {
            // Wrapper to convert runtime Type to a compile time  generic.
            var method = typeof(BindingFactoryHelpers).GetMethod("GetDirectTriggerBindingWorker", BindingFlags.NonPublic | BindingFlags.Static);

            method = method.MakeGenericMethod(typeof(TMessage), typeof(TTriggerValue), exactType);
            var argumentBinding = MethodInvoke <SimpleTriggerArgumentBinding <TMessage, TTriggerValue> >(
                method,
                bindingStrategy, converterManager);

            return(argumentBinding);
        }
        public PocoTriggerArgumentBinding(ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy, IConverterManager converterManager, Type elementType) :
            base(bindingStrategy, converterManager)
        {
            this.ElementType = elementType;

            // Add properties ot binding data. Null if type doesn't expose it.
            _provider = BindingDataProvider.FromType(elementType);
            if (_provider != null)
            {
                // Binding data from Poco properties takes precedence over builtins
                foreach (var kv in _provider.Contract)
                {
                    string name = kv.Key;
                    Type   type = kv.Value;
                    Contract[name] = type;
                }
            }
        }
 // Bind a T.
 private static SimpleTriggerArgumentBinding <TMessage, TTriggerValue> GetTriggerArgumentElementBinding <TMessage, TTriggerValue>(
     Type elementType,
     ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy,
     IConverterManager converterManager)
 {
     if (elementType == typeof(TMessage))
     {
         return(new SimpleTriggerArgumentBinding <TMessage, TTriggerValue>(bindingStrategy, converterManager));
     }
     if (elementType == typeof(string))
     {
         return(new StringTriggerArgumentBinding <TMessage, TTriggerValue>(bindingStrategy, converterManager));
     }
     else
     {
         // Default, assume a Poco
         return(new PocoTriggerArgumentBinding <TMessage, TTriggerValue>(bindingStrategy, converterManager, elementType));
     }
 }
Esempio n. 13
0
        // Bind a trigger argument to various parameter types.
        // Handles either T or T[],
        internal static ITriggerDataArgumentBinding <TTriggerValue> GetTriggerArgumentBinding <TMessage, TTriggerValue>(
            ITriggerBindingStrategy <TMessage, TTriggerValue> bindingStrategy,
            ParameterInfo parameter,
            IConverterManager converterManager,
            out bool singleDispatch)
        {
            ITriggerDataArgumentBinding <TTriggerValue> argumentBinding = null;

            // If there's a direct binding from TMessage to the parameter's exact type; use that.
            // This takes precedence over array bindings.
            argumentBinding = GetDirectTriggerBinding <TMessage, TTriggerValue>(parameter.ParameterType, bindingStrategy, converterManager);
            if (argumentBinding != null)
            {
                singleDispatch = true;
                return(argumentBinding);
            }

            // Or array
            if (parameter.ParameterType.IsArray)
            {
                // dispatch the entire batch in a single call.
                singleDispatch = false;

                var elementType          = parameter.ParameterType.GetElementType();
                var innerArgumentBinding = GetTriggerArgumentElementBinding <TMessage, TTriggerValue>(elementType, bindingStrategy, converterManager);

                argumentBinding = new ArrayTriggerArgumentBinding <TMessage, TTriggerValue>(bindingStrategy, innerArgumentBinding, converterManager);

                return(argumentBinding);
            }
            else
            {
                // Dispatch each item one at a time
                singleDispatch = true;

                var elementType = parameter.ParameterType;
                argumentBinding = GetTriggerArgumentElementBinding <TMessage, TTriggerValue>(elementType, bindingStrategy, converterManager);
                return(argumentBinding);
            }
        }
Esempio n. 14
0
        public static ITriggerBinding GetTriggerBinding <TMessage, TTriggerValue>(
            ITriggerBindingStrategy <TMessage, TTriggerValue> hooks,
            ParameterInfo parameter,
            IConverterManager converterManager,
            Func <ListenerFactoryContext, bool, Task <IListener> > createListener
            )
        {
            bool singleDispatch;
            var  argumentBinding = GenericBinder.GetTriggerArgumentBinding(hooks, parameter, converterManager, out singleDispatch);

            var parameterDescriptor = new ParameterDescriptor
            {
                Name         = parameter.Name,
                DisplayHints = new ParameterDisplayHints
                {
                    Description = singleDispatch ? "message" : "messages"
                }
            };

            ITriggerBinding binding = new CommonTriggerBinding <TMessage, TTriggerValue>(
                hooks, argumentBinding, createListener, parameterDescriptor, singleDispatch);

            return(binding);
        }
 public StringTriggerArgumentBinding(ITriggerBindingStrategy <TMessage, TTriggerValue> hooks, IConverterManager converterManager) :
     base(hooks, converterManager)
 {
     this._elementType = typeof(string);
 }