Skip to content

Commit

Permalink
Some refactoring/renames for binding extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed May 19, 2017
1 parent 0dd6f73 commit b064455
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.Azure.WebJobs.Host.Bindings
{
// General rule for binding parameters to an AsyncCollector.
// Supports the various flavors like IAsyncCollector, ICollector, out T, out T[].
internal class AsyncCollectorBindingProvider<TAttribute, TType> : FluentBindingProvider<TAttribute>, IBindingProvider, IRuleProvider
internal class AsyncCollectorBindingProvider<TAttribute, TType> : FluentBindingProvider<TAttribute>, IBindingProvider, IBindingRuleProvider
where TAttribute : Attribute
{
private readonly INameResolver _nameResolver;
Expand Down Expand Up @@ -146,7 +146,7 @@ private static Type[] MakeArray(params Type[] types)
return types.Where(type => type != null).ToArray();
}

private static void AddRulesForType(Type type, List<Rule> rules)
private static void AddRulesForType(Type type, List<BindingRule> rules)
{
var typeIAC = typeof(IAsyncCollector<>).MakeGenericType(type);

Expand All @@ -158,41 +158,41 @@ private static void AddRulesForType(Type type, List<Rule> rules)
}

rules.Add(
new Rule
new BindingRule
{
SourceAttribute = typeof(TAttribute),
Converters = MakeArray(intermediateType),
UserType = new ConverterManager.ExactMatch(typeIAC)
});

rules.Add(
new Rule
new BindingRule
{
SourceAttribute = typeof(TAttribute),
Converters = MakeArray(intermediateType, typeIAC),
UserType = new ConverterManager.ExactMatch(typeof(ICollector<>).MakeGenericType(type))
});

rules.Add(
new Rule
new BindingRule
{
SourceAttribute = typeof(TAttribute),
Converters = MakeArray(intermediateType, typeIAC),
UserType = new ConverterManager.ExactMatch(type.MakeByRefType())
});

rules.Add(
new Rule
new BindingRule
{
SourceAttribute = typeof(TAttribute),
Converters = MakeArray(intermediateType, typeIAC),
UserType = new ConverterManager.ExactMatch(type.MakeArrayType().MakeByRefType())
});
}

public IEnumerable<Rule> GetRules()
public IEnumerable<BindingRule> GetRules()
{
var rules = new List<Rule>();
var rules = new List<BindingRule>();
AddRulesForType(typeof(TType), rules);

var cm = (ConverterManager)_converterManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.Azure.WebJobs.Host.Bindings
// General rule for binding to input parameters.
// Can invoke Converter manager.
// Can leverage OpenTypes for pattern matchers.
internal class BindToInputBindingProvider<TAttribute, TType> : FluentBindingProvider<TAttribute>, IBindingProvider, IRuleProvider
internal class BindToInputBindingProvider<TAttribute, TType> : FluentBindingProvider<TAttribute>, IBindingProvider, IBindingRuleProvider
where TAttribute : Attribute
{
private readonly INameResolver _nameResolver;
Expand Down Expand Up @@ -53,12 +53,12 @@ public Task<IBinding> TryCreateAsync(BindingProviderContext context)
return Task.FromResult<IBinding>(binding);
}

public IEnumerable<Rule> GetRules()
public IEnumerable<BindingRule> GetRules()
{
var cm = (ConverterManager)_converterManager;
var types = cm.GetPossibleDestinationTypesFromSource(typeof(TAttribute), typeof(TType));

yield return new Rule
yield return new BindingRule
{
SourceAttribute = typeof(TAttribute),
UserType = new ConverterManager.ExactMatch(typeof(TType))
Expand All @@ -68,7 +68,7 @@ public IEnumerable<Rule> GetRules()

foreach (var type in types)
{
yield return new Rule
yield return new BindingRule
{
SourceAttribute = typeof(TAttribute),
Converters = converters,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;

namespace Microsoft.Azure.WebJobs.Host.Bindings
{
/// <summary>
/// Describes a binding rule. See <see cref="IBindingRuleProvider"/>.
/// </summary>
internal class BindingRule
{
public static readonly BindingRule[] Empty = new BindingRule[0];

/// <summary>
/// Gets or sets the binding rule filter.
/// </summary>
public string Filter { get; set; }

/// <summary>
/// Gets or sets the source attribute type.
/// </summary>
public Type SourceAttribute { get; set; }

/// <summary>
/// Gets or sets the intermediate converters used to
/// get to the <see cref="UserType"/>.
/// </summary>
public Type[] Converters { get; set; }

/// <summary>
/// Gets or sets the user type this rule can bind to.
/// </summary>
public OpenType UserType { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.Azure.WebJobs.Host.Bindings
{
internal class CompositeBindingProvider : IBindingProvider, IRuleProvider
internal class CompositeBindingProvider : IBindingProvider, IBindingRuleProvider
{
private readonly IEnumerable<IBindingProvider> _providers;

Expand All @@ -32,9 +32,9 @@ public async Task<IBinding> TryCreateAsync(BindingProviderContext context)
return null;
}

public IEnumerable<Rule> GetRules()
public IEnumerable<BindingRule> GetRules()
{
foreach (var provider in _providers.OfType<IRuleProvider>())
foreach (var provider in _providers.OfType<IBindingRuleProvider>())
{
foreach (var rule in provider.GetRules())
{
Expand All @@ -45,7 +45,7 @@ public IEnumerable<Rule> GetRules()

public Type GetDefaultType(Attribute attribute, FileAccess access, Type requestedType)
{
foreach (var provider in _providers.OfType<IRuleProvider>())
foreach (var provider in _providers.OfType<IBindingRuleProvider>())
{
var type = provider.GetDefaultType(attribute, access, requestedType);
if (type != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Azure.WebJobs.Host.Bindings
{
// Useful for extension that need some special case type restriction.
// If the user parameter type passes the predicate, then chain to an inner an provider.
internal class FilteringBindingProvider<TAttribute> : IBindingProvider, IRuleProvider
internal class FilteringBindingProvider<TAttribute> : IBindingProvider, IBindingRuleProvider
where TAttribute : Attribute
{
private readonly Func<TAttribute, Type, bool> _predicate;
Expand Down Expand Up @@ -54,10 +54,10 @@ public Task<IBinding> TryCreateAsync(BindingProviderContext context)
return _inner.TryCreateAsync(context);
}

public IEnumerable<Rule> GetRules()
public IEnumerable<BindingRule> GetRules()
{
var filter = _description;
IRuleProvider inner = _inner as IRuleProvider;
IBindingRuleProvider inner = _inner as IBindingRuleProvider;
if (inner != null)
{
foreach (var rule in inner.GetRules())
Expand All @@ -82,7 +82,7 @@ public Type GetDefaultType(Attribute attribute, FileAccess access, Type requeste
}

// Must apply filter
IRuleProvider inner = _inner as IRuleProvider;
IBindingRuleProvider inner = _inner as IBindingRuleProvider;
if (inner != null)
{
return inner.GetDefaultType(attribute, access, requestedType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Azure.WebJobs.Host.Bindings
// Composite binder for a specific attribute.
// Ignore parameters that don't have the attribute - other binders will get them.
// If it does have the attribue, but none of the binders handle it, then throw an error.
internal class GenericCompositeBindingProvider<TAttribute> : IBindingProvider, IRuleProvider
internal class GenericCompositeBindingProvider<TAttribute> : IBindingProvider, IBindingRuleProvider
where TAttribute : Attribute
{
private readonly IEnumerable<IBindingProvider> _providers;
Expand Down Expand Up @@ -70,9 +70,9 @@ public async Task<IBinding> TryCreateAsync(BindingProviderContext context)
throw new InvalidOperationException("Can't bind " + resourceName + " to type '" + context.Parameter.ParameterType + "'.");
}

public IEnumerable<Rule> GetRules()
public IEnumerable<BindingRule> GetRules()
{
return _providers.OfType<IRuleProvider>().SelectMany(rule => rule.GetRules());
return _providers.OfType<IBindingRuleProvider>().SelectMany(rule => rule.GetRules());
}

public Type GetDefaultType(Attribute attribute, FileAccess access, Type requestedType)
Expand All @@ -82,7 +82,7 @@ public Type GetDefaultType(Attribute attribute, FileAccess access, Type requeste
return null;
}

foreach (var provider in _providers.OfType<IRuleProvider>())
foreach (var provider in _providers.OfType<IBindingRuleProvider>())
{
var type = provider.GetDefaultType(attribute, access, requestedType);
if (type != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;

namespace Microsoft.Azure.WebJobs.Host.Bindings
{
/// <summary>
/// Interface for providing binding rules.
/// </summary>
/// <remarks>
/// This enables <see cref="IBindingProvider"/>s to be self describing which
/// supports tooling scenarios.
/// </remarks>
internal interface IBindingRuleProvider
{
/// <summary>
/// Gets the <see cref="BindingRule"/>s for this binding.
/// </summary>
/// <returns></returns>
IEnumerable<BindingRule> GetRules();

/// <summary>
/// Gets the default binding type for the specified binding options.
/// </summary>
/// <param name="attribute">The binding attribute. This does not need to be resolved.</param>
/// <param name="access">The direction of the binding.</param>
/// <param name="requestedType">The requested binding type. Specify object if none is requested,
/// otherwise provide a specific type (e.g. string, byte[], stream, JObject).
/// </param>
/// <returns>Null if unknown. Else a type that the parameter can be bound to. </returns>
Type GetDefaultType(Attribute attribute, FileAccess access, Type requestedType);
}
}

This file was deleted.

26 changes: 0 additions & 26 deletions src/Microsoft.Azure.WebJobs.Host/Bindings/BindingProviders/Rule.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Microsoft.Azure.WebJobs.Host.Blobs.Bindings
{
internal class BlobAttributeBindingProvider : IBindingProvider, IRuleProvider
internal class BlobAttributeBindingProvider : IBindingProvider, IBindingRuleProvider
{
private readonly INameResolver _nameResolver;
private readonly IStorageAccountProvider _accountProvider;
Expand Down Expand Up @@ -147,7 +147,7 @@ private static IBlobArgumentBindingProvider CreateConverterProvider<TValue, TCon
{
return new ConverterArgumentBindingProvider<TValue>(new TConverter());
}
public IEnumerable<Rule> GetRules()
public IEnumerable<BindingRule> GetRules()
{
// Once we have a BindToStream rule, we shouldn't need this.
// https://github.com/Azure/azure-webjobs-sdk/issues/1001
Expand All @@ -166,7 +166,7 @@ public IEnumerable<Rule> GetRules()
typeof(byte[]).MakeByRefType()
})
{
yield return new Rule
yield return new BindingRule
{
SourceAttribute = typeof(BlobAttribute),
UserType = new ConverterManager.ExactMatch(type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public FluentBindingRule<TAttribute> AddValidator(Action<TAttribute, Type> valid

internal void DebugDumpGraph(TextWriter output)
{
var binding = CreateBinding() as IRuleProvider;
var binding = CreateBinding() as IBindingRuleProvider;
JobHostMetadataProvider.DumpRule(binding, output);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.Azure.WebJobs.Host/DefaultExtensionRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace Microsoft.Azure.WebJobs.Host
{
internal class DefaultExtensionRegistry : IExtensionRegistry
{
private readonly JobHostMetadataProvider _tooling;
private readonly JobHostMetadataProvider _metadataProvider;

private ConcurrentDictionary<Type, ConcurrentBag<object>> _registry = new ConcurrentDictionary<Type, ConcurrentBag<object>>();

public DefaultExtensionRegistry(JobHostMetadataProvider tooling = null)
public DefaultExtensionRegistry(JobHostMetadataProvider metadataProvider = null)
{
_tooling = tooling;
_metadataProvider = metadataProvider;
}

public void RegisterExtension(Type type, object instance)
Expand All @@ -35,12 +35,12 @@ public void RegisterExtension(Type type, object instance)
throw new ArgumentOutOfRangeException("instance");
}

if (_tooling != null)
if (_metadataProvider != null)
{
IExtensionConfigProvider extension = instance as IExtensionConfigProvider;
if (extension != null)
{
_tooling.AddExtension(extension);
_metadataProvider.AddExtension(extension);
}
}

Expand Down
Loading

0 comments on commit b064455

Please sign in to comment.