private static IObjectToTypeConverter <TableEntityContext> CreateConverter(IStorageTableClient client,
                                                                            IBindableTableEntityPath path)
 {
     return(new CompositeObjectToTypeConverter <TableEntityContext>(
                new EntityOutputConverter <TableEntityContext>(new IdentityConverter <TableEntityContext>()),
                new EntityOutputConverter <string>(new StringToTableEntityContextConverter(client, path))));
 }
예제 #2
0
        public async Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            ParameterInfo  parameter      = context.Parameter;
            TableAttribute tableAttribute = parameter.GetCustomAttribute <TableAttribute>(inherit: false);

            if (tableAttribute == null)
            {
                return(null);
            }

            string          tableName = Resolve(tableAttribute.TableName);
            IStorageAccount account   = await _accountProvider.GetStorageAccountAsync(context.Parameter, context.CancellationToken, _nameResolver);

            // requires storage account with table support
            account.AssertTypeOneOf(StorageAccountType.GeneralPurpose);

            StorageClientFactoryContext clientFactoryContext = new StorageClientFactoryContext
            {
                Parameter = context.Parameter
            };
            IStorageTableClient client = account.CreateTableClient(clientFactoryContext);

            bool     bindsToEntireTable = tableAttribute.RowKey == null;
            IBinding binding;

            if (bindsToEntireTable)
            {
                IBindableTablePath path = BindableTablePath.Create(tableName);
                path.ValidateContractCompatibility(context.BindingDataContract);

                IStorageTableArgumentBinding argumentBinding = _tableBindingProvider.TryCreate(parameter);

                if (argumentBinding == null)
                {
                    throw new InvalidOperationException("Can't bind Table to type '" + parameter.ParameterType + "'.");
                }

                binding = new TableBinding(parameter.Name, argumentBinding, client, path);
            }
            else
            {
                string partitionKey           = Resolve(tableAttribute.PartitionKey);
                string rowKey                 = Resolve(tableAttribute.RowKey);
                IBindableTableEntityPath path = BindableTableEntityPath.Create(tableName, partitionKey, rowKey);
                path.ValidateContractCompatibility(context.BindingDataContract);

                IArgumentBinding <TableEntityContext> argumentBinding = _entityBindingProvider.TryCreate(parameter);

                if (argumentBinding == null)
                {
                    throw new InvalidOperationException("Can't bind Table entity to type '" + parameter.ParameterType + "'.");
                }

                binding = new TableEntityBinding(parameter.Name, argumentBinding, client, path);
            }

            return(binding);
        }
 public TableEntityBinding(string parameterName, IArgumentBinding <TableEntityContext> argumentBinding,
                           IStorageTableClient client, IBindableTableEntityPath path)
 {
     _parameterName   = parameterName;
     _argumentBinding = argumentBinding;
     _client          = client;
     _accountName     = TableClient.GetAccountName(client);
     _path            = path;
     _converter       = CreateConverter(client, path);
 }
 public TableEntityBinding(string parameterName, IArgumentBinding<TableEntityContext> argumentBinding,
     IStorageTableClient client, IBindableTableEntityPath path)
 {
     _parameterName = parameterName;
     _argumentBinding = argumentBinding;
     _client = client;
     _accountName = TableClient.GetAccountName(client);
     _path = path;
     _converter = CreateConverter(client, path);
 }
        private IBinding TryCreate(BindingProviderContext context)
        {
            ParameterInfo parameter      = context.Parameter;
            var           tableAttribute = TypeUtility.GetResolvedAttribute <TableAttribute>(context.Parameter);

            if (tableAttribute == null)
            {
                return(null);
            }

            string tableName = Resolve(tableAttribute.TableName);
            var    account   = _accountProvider.Get(tableAttribute.Connection, _nameResolver);
            // requires storage account with table support
            // account.AssertTypeOneOf(StorageAccountType.GeneralPurpose); $$$

            CloudTableClient client = account.CreateCloudTableClient();

            bool     bindsToEntireTable = tableAttribute.RowKey == null;
            IBinding binding;

            if (bindsToEntireTable)
            {
                // This should have been caught by the other rule-based binders.
                // We never expect this to get thrown.
                throw new InvalidOperationException("Can't bind Table to type '" + parameter.ParameterType + "'.");
            }
            else
            {
                string partitionKey           = Resolve(tableAttribute.PartitionKey);
                string rowKey                 = Resolve(tableAttribute.RowKey);
                IBindableTableEntityPath path = BindableTableEntityPath.Create(tableName, partitionKey, rowKey);
                path.ValidateContractCompatibility(context.BindingDataContract);

                IArgumentBinding <TableEntityContext> argumentBinding = _entityBindingProvider.TryCreate(parameter);

                if (argumentBinding == null)
                {
                    throw new InvalidOperationException("Can't bind Table entity to type '" + parameter.ParameterType + "'.");
                }

                binding = new TableEntityBinding(parameter.Name, argumentBinding, client, path);
            }

            return(binding);
        }
예제 #6
0
        private IBinding TryCreate(BindingProviderContext context)
        {
            ParameterInfo parameter      = context.Parameter;
            var           tableAttribute = TypeUtility.GetResolvedAttribute <TableAttribute>(context.Parameter);

            if (tableAttribute == null)
            {
                return(null);
            }

            // JArray is bound by the next binding
            if (parameter.ParameterType == typeof(JArray))
            {
                return(null);
            }

            string tableName          = Resolve(tableAttribute.TableName);
            var    account            = _accountProvider.Get(tableAttribute.Connection, _nameResolver);
            bool   bindsToEntireTable = tableAttribute.RowKey == null;

            if (bindsToEntireTable)
            {
                // This should have been caught by the other rule-based binders.
                // We never expect this to get thrown.
                throw new InvalidOperationException("Can't bind Table to type '" + parameter.ParameterType + "'.");
            }

            string partitionKey           = Resolve(tableAttribute.PartitionKey);
            string rowKey                 = Resolve(tableAttribute.RowKey);
            IBindableTableEntityPath path = BindableTableEntityPath.Create(tableName, partitionKey, rowKey);

            path.ValidateContractCompatibility(context.BindingDataContract);
            IArgumentBinding <TableEntityContext> argumentBinding = TryCreatePocoBinding(parameter, _converterManager);

            if (argumentBinding == null)
            {
                throw new InvalidOperationException("Can't bind Table entity to type '" + parameter.ParameterType + "'.");
            }

            return(new TableEntityBinding(parameter.Name, argumentBinding, account, path));
        }
예제 #7
0
 public StringToTableEntityContextConverter(TableServiceClient client, IBindableTableEntityPath defaultPath)
 {
     _client      = client;
     _defaultPath = defaultPath;
 }
 private static IObjectToTypeConverter<TableEntityContext> CreateConverter(IStorageTableClient client, IBindableTableEntityPath path)
 {
     return new CompositeObjectToTypeConverter<TableEntityContext>(
         new EntityOutputConverter<TableEntityContext>(new IdentityConverter<TableEntityContext>()),
         new EntityOutputConverter<string>(new StringToTableEntityContextConverter(client, path)));
 }