protected override async Task <IValueProvider> BuildAsync(TAttribute attrResolved, ValueBindingContext context)
            {
                string       invokeString = Cloner.GetInvokeString(attrResolved);
                IValueBinder valueBinder  = await _builder(attrResolved, _parameter.ParameterType);

                return(new Wrapper(valueBinder, invokeString));
            }
示例#2
0
        private async Task TestGetThenSet <T>() where T : class
        {
            // Arrange
            Document newDocument = new Document();

            newDocument.Id = Guid.NewGuid().ToString();
            newDocument.SetPropertyValue("text", "some text");

            Mock <IDocumentDBService> mockService;
            IValueBinder binder = CreateBinder <T>(out mockService);

            mockService
            .Setup(m => m.ReadDocumentAsync(_expectedUri, null))
            .ReturnsAsync(newDocument);

            // Act
            // get, then immediately set with no changes
            var value = await binder.GetValueAsync() as T;

            await binder.SetValueAsync(value, CancellationToken.None);

            // Assert
            // There should be no call to ReplaceDocumentAsync
            mockService.Verify();
        }
示例#3
0
 public Injector(IDependencyInjector injector, IDependencyFactory factory, IValueBinder valueBinder, IPresenterBinder controlBinder) : this()
 {
     SetInjector(injector);
     SetFactory(factory);
     SetBinder(valueBinder);
     SetControlBinder(controlBinder);
 }
示例#4
0
 public Injector(IValueBinder valueBinder, IPresenterBinder controlBinder) : this()
 {
     SetBinder(valueBinder);
     SetControlBinder(controlBinder);
     SetFactory(new DependencyFactory(valueBinder.Container, controlBinder.Container));
     SetInjector(new DependencyInjector(Factory, BindControl, Bind));
 }
示例#5
0
        IDependencyFactory IBuilder.CreateFactory(out IValueBinder valueBinder, out IPresenterBinder presenterBinder)
        {
            var factory = (this as IBuilder).CreateFactory(out IDependencyContainer values, out IDependencyContainer presenters);

            valueBinder     = (this as IBuilder).CreateValueBinder(values);
            presenterBinder = (this as IBuilder).CreatePresenterBinder(presenters);
            return(factory);
        }
 public DependencyInjector(IDependencyFactory factory, IPresenterBinder controlBinder, IValueBinder valueBinder, IBuildQueue find, IBuildQueue insert)
 {
     Factory     = factory;
     Value       = valueBinder;
     Presenter   = controlBinder;
     this.find   = find;
     this.insert = insert;
 }
        internal Task <IValueBinder> BindForItemAsync(MobileTableAttribute attribute, Type paramType)
        {
            MobileTableContext context = CreateContext(attribute);

            Type         genericType = typeof(MobileTableItemValueBinder <>).MakeGenericType(paramType);
            IValueBinder binder      = (IValueBinder)Activator.CreateInstance(genericType, context);

            return(Task.FromResult(binder));
        }
        internal Task <IValueBinder> BindForItemAsync(IRedisAttribute attribute, Type type)
        {
            var context = CreateContext(attribute);

            Type         genericType = typeof(RedisCacheValueBinder <>).MakeGenericType(type);
            IValueBinder binder      = (IValueBinder)Activator.CreateInstance(genericType, context);

            return(Task.FromResult(binder));
        }
        internal Task <IValueBinder> BindForItemAsync(DocumentDBAttribute attribute, Type type, TraceWriter trace)
        {
            DocumentDBContext context = CreateContext(attribute, trace);

            Type         genericType = typeof(DocumentDBItemValueBinder <>).MakeGenericType(type);
            IValueBinder binder      = (IValueBinder)Activator.CreateInstance(genericType, context);

            return(Task.FromResult(binder));
        }
示例#10
0
        public DependencyInjector(IDependencyFactory factory, IPresenterBinder controlBinder, IValueBinder valueBinder)
        {
            Factory   = factory;
            Value     = valueBinder;
            Presenter = controlBinder;

            find   = Builder.FindDependencies.InFieldsPropertiesAndMethods(Value.Container);
            insert = Builder.InsertDependencies.IntoFieldsPropertiesAndMethods(Value.Container);
        }
示例#11
0
        internal static async Task ExecuteWithWatchersAsync(IFunctionInvoker invoker,
                                                            IReadOnlyDictionary <string, IValueProvider> parameters,
                                                            TextWriter consoleOutput,
                                                            CancellationToken cancellationToken)
        {
            IReadOnlyList <string> parameterNames = invoker.ParameterNames;
            IDelayedException      delayedBindingException;

            object[] invokeParameters = PrepareParameters(parameterNames, parameters, out delayedBindingException);

            if (delayedBindingException != null)
            {
                // This is done inside a watcher context so that each binding error is publish next to the binding in
                // the parameter status log.
                delayedBindingException.Throw();
            }

            // Cancellation token is provide by invokeParameters (if the method binds to CancellationToken).
            await invoker.InvokeAsync(invokeParameters);

            // Process any out parameters and persist any pending values.

            // Ensure IValueBinder.SetValue is called in BindOrder. This ordering is particularly important for
            // ensuring queue outputs occur last. That way, all other function side-effects are guaranteed to have
            // occurred by the time messages are enqueued.
            string[] parameterNamesInBindOrder = SortParameterNamesInStepOrder(parameters);

            foreach (string name in parameterNamesInBindOrder)
            {
                IValueProvider provider = parameters[name];
                IValueBinder   binder   = provider as IValueBinder;

                if (binder != null)
                {
                    object argument = invokeParameters[GetParameterIndex(parameterNames, name)];

                    try
                    {
                        // This could do complex things that may fail. Catch the exception.
                        await binder.SetValueAsync(argument, cancellationToken);
                    }
                    catch (OperationCanceledException)
                    {
                        throw;
                    }
                    catch (Exception exception)
                    {
                        string message = String.Format(CultureInfo.InvariantCulture,
                                                       "Error while handling parameter {0} after function returned:", name);
                        throw new InvalidOperationException(message, exception);
                    }
                }
            }
        }
示例#12
0
        /// <summary>
        /// Binds the specified collection of attributes.
        /// </summary>
        /// <typeparam name="TValue">The type to bind to.</typeparam>
        /// <param name="attributes">The collection of attributes to bind. The first attribute in the
        /// collection should be the primary attribute.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns></returns>
        public virtual async Task <TValue> BindAsync <TValue>(Attribute[] attributes, CancellationToken cancellationToken = default(CancellationToken))
        {
            var attribute            = attributes.First();
            var additionalAttributes = attributes.Skip(1).ToArray();

            IBinding binding = await _bindingSource.BindAsync <TValue>(attribute, additionalAttributes, cancellationToken);

            if (binding == null)
            {
                throw new InvalidOperationException("No binding found for attribute '" + attribute.GetType() + "'.");
            }

            // Create a clone of the binding context, so any binding data that was added
            // will be applied to the binding
            var ambientBindingContext = new AmbientBindingContext(_bindingSource.AmbientBindingContext.FunctionContext, _bindingData);
            var bindingContext        = new BindingContext(ambientBindingContext, cancellationToken);

            IValueProvider provider = await binding.BindAsync(bindingContext);

            if (provider == null)
            {
                return(default(TValue));
            }

            Debug.Assert(provider.Type == typeof(TValue));

            ParameterDescriptor parameterDesciptor = binding.ToParameterDescriptor();

            parameterDesciptor.Name = null; // Remove the dummy name "?" used for runtime binding.

            // Add even if watchable is null to show parameter descriptor in status.
            string     value     = provider.ToInvokeString();
            IWatchable watchable = provider as IWatchable;

            _watcher.Add(parameterDesciptor, value, watchable);

            IValueBinder binder = provider as IValueBinder;

            if (binder != null)
            {
                _binders.Add(binder);
            }

            IDisposable disposableProvider = provider as IDisposable;

            if (disposableProvider != null)
            {
                _disposable.Add(disposableProvider);
            }

            object result = await provider.GetValueAsync();

            return((TValue)result);
        }
        private Task <IValueBinder> CreateValueBinderAsync(RethinkDbAttribute attribute, Type type)
        {
            if (String.IsNullOrEmpty(attribute.Id))
            {
                throw new InvalidOperationException($"The '{nameof(RethinkDbAttribute.Id)}' property of a RethinkDB single-item input binding cannot be null or empty.");
            }

            ConnectionOptions connectionOptions = ConnectionOptionsBuilder.Build(attribute, _options);
            Type         valyeBinderType        = typeof(RethinkDbValueBinder <>).MakeGenericType(type);
            IValueBinder valueBinder            = (IValueBinder)Activator.CreateInstance(valyeBinderType, attribute, _rethinkDBConnectionFactory.GetConnectionAsync(connectionOptions));

            return(Task.FromResult(valueBinder));
        }
示例#14
0
        internal Task <IValueBinder> BindForItemAsync(CosmosAttribute attribute, Type type)
        {
            if (string.IsNullOrEmpty(attribute.Id))
            {
                throw new InvalidOperationException("The 'Id' property of a Cosmos single-item input binding cannot be null or empty.");
            }

            CosmosContext context = CreateContext(attribute);

            Type         genericType = typeof(CosmosItemValueBinder <>).MakeGenericType(type);
            IValueBinder binder      = (IValueBinder)Activator.CreateInstance(genericType, context);

            return(Task.FromResult(binder));
        }
示例#15
0
        public async Task GetValueAsync_Throws_WhenErrorResponse()
        {
            // Arrange
            IValueBinder binder = CreateBinder <Item>(out Mock <ICosmosDBService> mockService);

            mockService
            .Setup(m => m.ReadDocumentAsync(_expectedUri, null))
            .ThrowsAsync(CosmosDBTestUtility.CreateDocumentClientException(HttpStatusCode.ServiceUnavailable));

            // Act
            // TODO: Fix this up so it exposes the real exception
            var ex = await Assert.ThrowsAsync <DocumentClientException>(() => binder.GetValueAsync());

            // Assert
            Assert.Equal(HttpStatusCode.ServiceUnavailable, ex.StatusCode);
        }
示例#16
0
        public async Task GetValueAsync_DoesNotThrow_WhenResponseIsNotFound()
        {
            // Arrange
            IValueBinder binder = CreateBinder <Item>(out Mock <ICosmosDBService> mockService);

            mockService
            .Setup(m => m.ReadDocumentAsync(_expectedUri, null))
            .ThrowsAsync(CosmosDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound));

            // Act
            var value = await binder.GetValueAsync();

            // Assert
            Assert.Null(value);
            mockService.VerifyAll();
        }
示例#17
0
        public async Task GetValueAsync_JObject_QueriesItem()
        {
            // Arrange
            IValueBinder binder = CreateBinder <Item>(out Mock <ICosmosDBService> mockService);

            mockService
            .Setup(m => m.ReadDocumentAsync(_expectedUri, null))
            .ReturnsAsync(new Document());

            // Act
            var value = (await binder.GetValueAsync()) as Item;

            // Assert
            mockService.VerifyAll();
            Assert.NotNull(value);
        }
        public void GetValue_DoesNotThrow_WhenResponseIsNotFound()
        {
            // Arrange
            Mock <IDocumentDBService> mockService;
            IValueBinder binder = CreateBinder <Item>(out mockService);

            mockService
            .Setup(m => m.ReadDocumentAsync <Item>(_expectedUri, null))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.NotFound));

            // Act
            var value = binder.GetValue();

            // Assert
            Assert.Null(value);
            mockService.VerifyAll();
        }
示例#19
0
        public async Task <TValue> BindAsync <TValue>(Attribute attribute, CancellationToken cancellationToken)
        {
            IBinding binding = await _bindingSource.BindAsync <TValue>(attribute, cancellationToken);

            if (binding == null)
            {
                throw new InvalidOperationException("No binding found for attribute '" + attribute.GetType() + "'.");
            }

            IValueProvider provider = await binding.BindAsync(new BindingContext(
                                                                  _bindingSource.AmbientBindingContext, cancellationToken));

            if (provider == null)
            {
                return(default(TValue));
            }

            Debug.Assert(provider.Type == typeof(TValue));

            ParameterDescriptor parameterDesciptor = binding.ToParameterDescriptor();

            parameterDesciptor.Name = null; // Remove the dummy name "?" used for runtime binding.

            string value = provider.ToInvokeString();

            IWatchable watchable = provider as IWatchable;

            // Add even if watchable is null to show parameter descriptor in status.
            _watcher.Add(parameterDesciptor, value, watchable);

            IValueBinder binder = provider as IValueBinder;

            if (binder != null)
            {
                _binders.Add(binder);
            }

            IDisposable disposableProvider = provider as IDisposable;

            if (disposableProvider != null)
            {
                _disposable.Add(disposableProvider);
            }

            return((TValue)provider.GetValue());
        }
        public void GetValue_JObject_QueriesItem()
        {
            // Arrange
            Mock <IDocumentDBService> mockService;
            IValueBinder binder = CreateBinder <Item>(out mockService);

            mockService
            .Setup(m => m.ReadDocumentAsync(_expectedUri, null))
            .ReturnsAsync(new Document());

            // Act
            var value = binder.GetValue() as Item;

            // Assert
            mockService.VerifyAll();
            Assert.NotNull(value);
        }
        public void GetValue_Throws_WhenErrorResponse()
        {
            // Arrange
            Mock <IDocumentDBService> mockService;
            IValueBinder binder = CreateBinder <Item>(out mockService);

            mockService
            .Setup(m => m.ReadDocumentAsync(_expectedUri, null))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.ServiceUnavailable));

            // Act
            // TODO: Fix this up so it exposes the real exception
            var ex = Assert.Throws <AggregateException>(() => binder.GetValue());

            // Assert
            Assert.Equal(HttpStatusCode.ServiceUnavailable, ((DocumentClientException)ex.InnerException).StatusCode);
        }
示例#22
0
        public async Task GetValueAsync_JObject_QueriesItem_WithPartitionKey()
        {
            // Arrange
            string       partitionKey      = "partitionKey";
            string       partitionKeyValue = string.Format("[\"{0}\"]", partitionKey);
            IValueBinder binder            = CreateBinder <Item>(out Mock <ICosmosDBService> mockService, partitionKey);

            mockService
            .Setup(m => m.ReadDocumentAsync(_expectedUri, It.Is <RequestOptions>(r => r.PartitionKey.ToString() == partitionKeyValue)))
            .ReturnsAsync(new Document());

            // Act
            var value = (await binder.GetValueAsync()) as Item;

            // Assert
            mockService.VerifyAll();
            Assert.NotNull(value);
        }
        public async Task GetValueAsync_JObject_Throws_WhenErrorResponse()
        {
            // Arrange
            var parameter = MobileAppTestHelper.GetInputParameter <JObject>();
            Mock <IMobileServiceTable>  mockTable;
            Mock <IMobileServiceClient> mockClient;
            IValueBinder binder = CreateJObjectBinder(parameter, out mockClient, out mockTable);

            mockTable
            .Setup(m => m.LookupAsync("abc123"))
            .Throws(new MobileServiceInvalidOperationException(string.Empty, null, new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)));

            // Act
            var ex = await Assert.ThrowsAsync <MobileServiceInvalidOperationException>(() => binder.GetValueAsync());

            // Assert
            Assert.Equal(HttpStatusCode.ServiceUnavailable, ex.Response.StatusCode);
        }
        public void GetValue_Poco_Throws_WhenErrorResponse()
        {
            // Arrange
            var parameter = EasyTableTestHelper.GetInputParameter <TodoItem>();
            Mock <IMobileServiceTable <TodoItem> > mockTable;
            Mock <IMobileServiceClient>            mockClient;
            IValueBinder binder = CreatePocoBinder(parameter, out mockClient, out mockTable);

            mockTable
            .Setup(m => m.LookupAsync("abc123"))
            .Throws(new MobileServiceInvalidOperationException(string.Empty, null, new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)));

            // Act
            var ex = Assert.Throws <MobileServiceInvalidOperationException>(() => binder.GetValue());

            // Assert
            Assert.Equal(HttpStatusCode.ServiceUnavailable, ex.Response.StatusCode);
        }
        internal Task <IValueBinder> BindForItemAsync(FunctionsHttpClientAttribute attribute, Type type)
        {
            if (string.IsNullOrWhiteSpace(attribute?.RequestUrl))
            {
                throw new InvalidOperationException($"{nameof(attribute.RequestUrl)} cannot be null");
            }

            FunctionsHttpClientContext context = CreateContext(attribute);

            if (type == typeof(string))
            {
                return(Task.FromResult <IValueBinder>(new FunctionsHttpClientItemValueBinderString(CreateContext(attribute))));
            }

            Type         genericType = typeof(FunctionsHttpClientItemValueBinder <>).MakeGenericType(type);
            IValueBinder binder      = (IValueBinder)Activator.CreateInstance(genericType, context);

            return(Task.FromResult(binder));
        }
        public void GetValue_JObject_DoesNotThrow_WhenResponseIsNotFound()
        {
            // Arrange
            var parameter = EasyTableTestHelper.GetInputParameter <JObject>();
            Mock <IMobileServiceTable>  mockTable;
            Mock <IMobileServiceClient> mockClient;
            IValueBinder binder = CreateJObjectBinder(parameter, out mockClient, out mockTable);

            mockTable
            .Setup(m => m.LookupAsync("abc123"))
            .ThrowsAsync(new MobileServiceInvalidOperationException(string.Empty, null, new HttpResponseMessage(HttpStatusCode.NotFound)));

            // Act
            var value = binder.GetValue();

            // Assert
            Assert.Null(value);
            mockTable.VerifyAll();
            mockClient.VerifyAll();
        }
        public async Task GetValueAsync_Poco_DoesNotThrow_WhenResponseIsNotFound()
        {
            // Arrange
            var parameter = MobileAppTestHelper.GetInputParameter <TodoItem>();
            Mock <IMobileServiceTable <TodoItem> > mockTable;
            Mock <IMobileServiceClient>            mockClient;
            IValueBinder binder = CreatePocoBinder(parameter, out mockClient, out mockTable);

            mockTable
            .Setup(m => m.LookupAsync("abc123"))
            .ThrowsAsync(new MobileServiceInvalidOperationException(string.Empty, null, new HttpResponseMessage(HttpStatusCode.NotFound)));

            // Act
            var value = await binder.GetValueAsync();

            // Assert
            Assert.Null(value);
            mockTable.VerifyAll();
            mockClient.VerifyAll();
        }
        public void GetValue_JObject_QueriesItem()
        {
            // Arrange
            var parameter = EasyTableTestHelper.GetInputParameter <JObject>();
            Mock <IMobileServiceTable>  mockTable;
            Mock <IMobileServiceClient> mockClient;
            IValueBinder binder   = CreateJObjectBinder(parameter, out mockClient, out mockTable);
            JToken       response = new JObject() as JToken;

            mockTable
            .Setup(m => m.LookupAsync("abc123"))
            .Returns(Task.FromResult(response));

            // Act
            var value = binder.GetValue();

            // Assert
            Assert.Same(response, value);
            mockTable.VerifyAll();
            mockClient.VerifyAll();
        }
        public async Task GetValueAsync_Poco_QueriesItem()
        {
            // Arrange
            var parameter = MobileAppTestHelper.GetInputParameter <TodoItem>();
            Mock <IMobileServiceTable <TodoItem> > mockTable;
            Mock <IMobileServiceClient>            mockClient;
            IValueBinder binder   = CreatePocoBinder(parameter, out mockClient, out mockTable);
            var          response = new TodoItem();

            mockTable
            .Setup(m => m.LookupAsync("abc123"))
            .Returns(Task.FromResult(response));

            // Act
            var value = await binder.GetValueAsync();

            // Assert
            Assert.Same(response, value);
            mockTable.VerifyAll();
            mockClient.VerifyAll();
        }
        internal static async Task ExecuteWithWatchersAsync(IFunctionInstance instance,
                                                            IReadOnlyDictionary <string, IValueProvider> parameters,
                                                            TraceWriter traceWriter,
                                                            CancellationTokenSource functionCancellationTokenSource)
        {
            IFunctionInvoker       invoker        = instance.Invoker;
            IReadOnlyList <string> parameterNames = invoker.ParameterNames;
            IDelayedException      delayedBindingException;

            object[] invokeParameters = PrepareParameters(parameterNames, parameters, out delayedBindingException);

            if (delayedBindingException != null)
            {
                // This is done inside a watcher context so that each binding error is publish next to the binding in
                // the parameter status log.
                delayedBindingException.Throw();
            }

            // if the function is a Singleton, aquire the lock
            SingletonLock singleton = null;

            if (TryGetSingletonLock(parameters, out singleton))
            {
                await singleton.AcquireAsync(functionCancellationTokenSource.Token);
            }

            // Create a source specifically for timeouts
            using (CancellationTokenSource timeoutTokenSource = new CancellationTokenSource())
            {
                MethodInfo       method           = instance.FunctionDescriptor.Method;
                TimeoutAttribute timeoutAttribute = TypeUtility.GetHierarchicalAttributeOrNull <TimeoutAttribute>(method);
                bool             throwOnTimeout   = timeoutAttribute == null ? false : timeoutAttribute.ThrowOnTimeout;
                var      timer         = StartFunctionTimeout(instance, timeoutAttribute, timeoutTokenSource, traceWriter);
                TimeSpan timerInterval = timer == null ? TimeSpan.MinValue : TimeSpan.FromMilliseconds(timer.Interval);
                try
                {
                    await InvokeAsync(invoker, invokeParameters, timeoutTokenSource, functionCancellationTokenSource,
                                      throwOnTimeout, timerInterval, instance);
                }
                finally
                {
                    if (timer != null)
                    {
                        timer.Stop();
                        timer.Dispose();
                    }
                }
            }

            // Process any out parameters and persist any pending values.
            // Ensure IValueBinder.SetValue is called in BindStepOrder. This ordering is particularly important for
            // ensuring queue outputs occur last. That way, all other function side-effects are guaranteed to have
            // occurred by the time messages are enqueued.
            string[] parameterNamesInBindOrder = SortParameterNamesInStepOrder(parameters);
            foreach (string name in parameterNamesInBindOrder)
            {
                IValueProvider provider = parameters[name];
                IValueBinder   binder   = provider as IValueBinder;

                if (binder != null)
                {
                    object argument = invokeParameters[GetParameterIndex(parameterNames, name)];

                    try
                    {
                        // This could do complex things that may fail. Catch the exception.
                        await binder.SetValueAsync(argument, functionCancellationTokenSource.Token);
                    }
                    catch (OperationCanceledException)
                    {
                        throw;
                    }
                    catch (Exception exception)
                    {
                        string message = String.Format(CultureInfo.InvariantCulture,
                                                       "Error while handling parameter {0} after function returned:", name);
                        throw new InvalidOperationException(message, exception);
                    }
                }
            }

            if (singleton != null)
            {
                await singleton.ReleaseAsync(functionCancellationTokenSource.Token);
            }
        }