diff --git a/src/Autofac.Extras.AttributeMetadata/AutofacAttributeExtensions.cs b/src/Autofac.Extras.AttributeMetadata/AutofacAttributeExtensions.cs index cd17620..82a1204 100644 --- a/src/Autofac.Extras.AttributeMetadata/AutofacAttributeExtensions.cs +++ b/src/Autofac.Extras.AttributeMetadata/AutofacAttributeExtensions.cs @@ -63,4 +63,40 @@ public static IRegistrationBuilder + /// Registers metadata that is declared loosely using attributes marked with the + /// MetadataAttributeAttribute for an individual type registration. All of the marked + /// attributes are used together to create a common set of dictionary values that + /// constitute the metadata on the type. + /// + /// The type of the registration limit. + /// The registration builder containing registration data. + /// Registration builder allowing the registration to be configured. + /// + /// Thrown if is . + /// + /// + /// + /// This is the individual registration counterpart to the assembly scanning overload, so + /// RegisterType and RegisterAssemblyTypes can both opt in to attributed metadata + /// the same way. It applies to reflection-based registrations, where the implementation type is + /// known at registration time; for delegate registrations, use the + /// instead. + /// + /// + /// This can safely be combined with the . The module does + /// not overwrite metadata keys that are already present, so metadata is applied once. + /// + /// + public static IRegistrationBuilder + WithAttributedMetadata(this IRegistrationBuilder builder) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + return builder.WithMetadata(MetadataHelper.GetMetadata(builder.ActivatorData.ImplementationType)); + } } diff --git a/test/Autofac.Extras.AttributeMetadata.Test/SingleRegistrationAttributedMetadataTestFixture.cs b/test/Autofac.Extras.AttributeMetadata.Test/SingleRegistrationAttributedMetadataTestFixture.cs new file mode 100644 index 0000000..3ca220e --- /dev/null +++ b/test/Autofac.Extras.AttributeMetadata.Test/SingleRegistrationAttributedMetadataTestFixture.cs @@ -0,0 +1,81 @@ +// Copyright (c) Autofac Project. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System.Reflection; +using Autofac.Builder; +using Autofac.Extras.AttributeMetadata.Test.ScenarioTypes.WeakTypedMetadataAttributeScenario; +using Autofac.Features.Metadata; + +namespace Autofac.Extras.AttributeMetadata.Test; + +public class SingleRegistrationAttributedMetadataTestFixture +{ + [Fact] + public void Validate_attributed_metadata_on_generic_type_registration() + { + // arrange + var builder = new ContainerBuilder(); + builder.RegisterType().As().WithAttributedMetadata(); + + // act + var item = builder.Build().Resolve>(); + + // assert + Assert.Equal("Hello", item.Metadata["Name"]); + } + + [Fact] + public void Validate_attributed_metadata_on_non_generic_type_registration() + { + // arrange + var builder = new ContainerBuilder(); + builder.RegisterType(typeof(WeakTypedScenario)).As().WithAttributedMetadata(); + + // act + var item = builder.Build().Resolve>(); + + // assert + Assert.Equal("Hello", item.Metadata["Name"]); + } + + [Fact] + public void Validate_assembly_scanning_registration_still_resolves() + { + // arrange + var builder = new ContainerBuilder(); + builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) + .As() + .WithAttributedMetadata(); + + // act + var item = builder.Build().Resolve>(); + + // assert + Assert.Equal("Hello", item.Metadata["Name"]); + } + + [Fact] + public void Validate_no_conflict_when_combined_with_attributed_metadata_module() + { + // arrange + var builder = new ContainerBuilder(); + builder.RegisterModule(); + builder.RegisterType().As().WithAttributedMetadata(); + + // act + var item = builder.Build().Resolve>(); + + // assert + Assert.Equal("Hello", item.Metadata["Name"]); + } + + [Fact] + public void Validate_null_builder_throws() + { + // arrange + IRegistrationBuilder builder = null!; + + // act / assert + Assert.Throws(() => { builder.WithAttributedMetadata(); }); + } +}