Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move WithMetadata with mapping for OpenGenericAssemblyScanning #1299

Merged
merged 3 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,59 +250,5 @@ public static IRegistrationBuilder<TLimit, OpenGenericScanningActivatorData, TRe
.Where(candidateType => candidateType.IsOpenGenericTypeOf(openGenericServiceType))
.As(candidateType => (Service)new KeyedService(serviceKeyMapping(candidateType), candidateType));
}

/// <summary>
/// Specify how an open generic type from a scanned assembly provides metadata.
/// </summary>
/// <typeparam name="TLimit">Registration limit type.</typeparam>
/// <typeparam name="TRegistrationStyle">Registration style.</typeparam>
/// <param name="registration">Registration to set metadata on.</param>
/// <param name="metadataMapping">A function mapping the type to a list of metadata items.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
public static IRegistrationBuilder<TLimit, OpenGenericScanningActivatorData, TRegistrationStyle>
WithMetadata<TLimit, TRegistrationStyle>(
this IRegistrationBuilder<TLimit, OpenGenericScanningActivatorData, TRegistrationStyle> registration,
Func<Type, IEnumerable<KeyValuePair<string, object?>>> metadataMapping)
{
registration.ActivatorData.ConfigurationActions.Add((t, rb) => rb.WithMetadata(metadataMapping(t)));
return registration;
}

/// <summary>
/// Use the properties of an attribute (or interface implemented by an attribute) on the scanned type
/// to provide metadata values.
/// </summary>
/// <remarks>Inherited attributes are supported; however, there must be at most one matching attribute
/// in the inheritance chain.</remarks>
/// <typeparam name="TAttribute">The attribute applied to the scanned type.</typeparam>
/// <param name="registration">Registration to set metadata on.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
public static IRegistrationBuilder<object, OpenGenericScanningActivatorData, DynamicRegistrationStyle>
WithMetadataFrom<TAttribute>(
this IRegistrationBuilder<object, OpenGenericScanningActivatorData, DynamicRegistrationStyle> registration)
{
var attrType = typeof(TAttribute);
var metadataProperties = attrType
.GetRuntimeProperties()
.Where(pi => pi.CanRead);

return registration.WithMetadata(t =>
{
var attrs = t.GetCustomAttributes(true).OfType<TAttribute>().ToList();

if (attrs.Count == 0)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationExtensionsResources.MetadataAttributeNotFound, typeof(TAttribute), t));
}

if (attrs.Count != 1)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationExtensionsResources.MultipleMetadataAttributesSameType, typeof(TAttribute), t));
}

var attr = attrs[0];
return metadataProperties.Select(p => new KeyValuePair<string, object?>(p.Name, p.GetValue(attr, null)));
});
}
}
}
60 changes: 60 additions & 0 deletions src/Autofac/RegistrationExtensions.OpenGenericAssemblyScanning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Autofac.Builder;
Expand Down Expand Up @@ -298,5 +299,64 @@ public static IRegistrationBuilder<TLimit, OpenGenericScanningActivatorData, Dyn
{
return registration.As(t => t.GetOpenGenericImplementedInterfaces());
}

/// <summary>
/// Specify how a type from a scanned assembly provides metadata.
/// </summary>
/// <typeparam name="TLimit">Registration limit type.</typeparam>
/// <typeparam name="TRegistrationStyle">Registration style.</typeparam>
/// <param name="registration">Registration to set metadata on.</param>
/// <param name="metadataMapping">A function mapping the type to a list of metadata items.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
public static IRegistrationBuilder<TLimit, OpenGenericScanningActivatorData, TRegistrationStyle>
WithMetadata<TLimit, TRegistrationStyle>(
this IRegistrationBuilder<TLimit, OpenGenericScanningActivatorData, TRegistrationStyle> registration,
Func<Type, IEnumerable<KeyValuePair<string, object?>>> metadataMapping)
{
if (registration == null)
{
throw new ArgumentNullException(nameof(registration));
}

registration.ActivatorData.ConfigurationActions.Add((t, rb) => rb.WithMetadata(metadataMapping(t)));
return registration;
}

/// <summary>
/// Use the properties of an attribute (or interface implemented by an attribute) on the scanned type
/// to provide metadata values.
/// </summary>
/// <remarks>Inherited attributes are supported; however, there must be at most one matching attribute
/// in the inheritance chain.</remarks>
/// <typeparam name="TAttribute">The attribute applied to the scanned type.</typeparam>
/// <param name="registration">Registration to set metadata on.</param>
/// <returns>Registration builder allowing the registration to be configured.</returns>
public static IRegistrationBuilder<object, OpenGenericScanningActivatorData, DynamicRegistrationStyle>
WithMetadataFrom<TAttribute>(
this IRegistrationBuilder<object, OpenGenericScanningActivatorData, DynamicRegistrationStyle> registration)
{
var attrType = typeof(TAttribute);
var metadataProperties = attrType
.GetRuntimeProperties()
.Where(pi => pi.CanRead);

return registration.WithMetadata(t =>
{
var attrs = t.GetCustomAttributes(true).OfType<TAttribute>().ToList();

if (attrs.Count == 0)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationExtensionsResources.MetadataAttributeNotFound, typeof(TAttribute), t));
}

if (attrs.Count != 1)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationExtensionsResources.MultipleMetadataAttributesSameType, typeof(TAttribute), t));
}

var attr = attrs[0];
return metadataProperties.Select(p => new KeyValuePair<string, object?>(p.Name, p.GetValue(attr, null)));
});
}
}
}