Skip to content
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Project specific files
artifacts/
BenchmarkDotNet.Artifacts/

# User-specific files
*.suo
Expand Down Expand Up @@ -108,7 +109,6 @@ publish/
*.pubxml

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
packages/

# Windows Azure Build Output
Expand All @@ -133,6 +133,7 @@ node_modules/
bower_components/
wwwroot/
project.lock.json
*.Designer.cs

# RIA/Silverlight projects
Generated_Code/
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: "e72a3ca1632f0b11a07d171449fe447a7ff6795e" # frozen: v0.48.0
rev: "5b5dddc4fb0f83c3ea1fc5616fa63e115dce83e0" # frozen: v0.49.1
hooks:
- id: markdownlint
args:
- --fix
- repo: https://github.com/tillig/json-sort-cli
rev: "2b7e147e0933bd30b58133b6f287e5c695ff4f0e" # frozen: v3.0.1
rev: "f628ee730c3de493fe79688696555a31364f0a10" # frozen: v3.0.3
hooks:
- id: json-sort
args:
Expand Down
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"davidanson.vscode-markdownlint",
"editorconfig.editorconfig",
"ms-dotnettools.csharp"
"ms-dotnettools.csdevkit",
"travisillig.vscode-json-stable-stringify"
]
}
27 changes: 26 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,30 @@
"autofac",
"xunit"
],
"omnisharp.enableEditorConfigSupport": true
"coverage-gutters.coverageBaseDir": "artifacts/logs",
"coverage-gutters.coverageFileNames": [
"**/coverage.cobertura.xml"
],
"dotnet.defaultSolution": "Autofac.Extras.AttributeMetadata.sln",
"dotnet.unitTestDebuggingOptions": {
"enableStepFiltering": false,
"justMyCode": false,
"requireExactSource": false,
"sourceLinkOptions": {
"*": {
"enabled": true
}
},
"suppressJITOptimizations": true,
"symbolOptions": {
"searchNuGetOrgSymbolServer": true
}
},
"explorer.fileNesting.enabled": true,
"explorer.fileNesting.patterns": {
"*.resx": "$(capture).*.resx, $(capture).designer.cs, $(capture).designer.vb"
},
"files.watcherExclude": {
"**/target": true
}
}
74 changes: 69 additions & 5 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
{
"linux": {
"options": {
"shell": {
"args": [
"-NoProfile",
"-Command"
],
"executable": "pwsh"
}
}
},
"osx": {
"options": {
"shell": {
"args": [
"-NoProfile",
"-Command"
],
"executable": "/usr/local/bin/pwsh"
}
}
},
"tasks": [
{
"command": "If (Test-Path ${workspaceFolder}/artifacts/logs) { Remove-Item ${workspaceFolder}/artifacts/logs -Recurse -Force }; New-Item -Path ${workspaceFolder}/artifacts/logs -ItemType Directory -Force | Out-Null",
"label": "create log directory",
"type": "shell"
},
{
"args": [
"build",
"Autofac.Extras.AttributeMetadata.sln",
"${workspaceFolder}/Autofac.Extras.AttributeMetadata.sln",
"--tl:off",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
Expand All @@ -13,12 +41,48 @@
"kind": "build"
},
"label": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile",
"type": "shell"
},
{
"args": [
"test",
"${workspaceFolder}/Autofac.Extras.AttributeMetadata.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary",
"--results-directory",
"artifacts/logs",
"--logger:trx",
"--collect:XPlat Code Coverage",
"--settings:build/Coverage.runsettings",
"--filter",
"FullyQualifiedName!~Bench"
],
"command": "dotnet",
"dependsOn": [
"create log directory"
],
"group": {
"isDefault": true,
"kind": "test"
},
"label": "test",
"problemMatcher": "$msCompile",
"type": "process"
}
],
"version": "2.0.0"
"version": "2.0.0",
"windows": {
"options": {
"shell": {
"args": [
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command"
],
"executable": "pwsh.exe"
}
}
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Autofac Project
Copyright © 2014 Autofac Project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,62 @@

Attribute metadata support for Autofac IoC

[![Build status](https://github.com/autofac/Autofac.Extras.AttributeMetadata/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/autofac/Autofac.Extras.AttributeMetadata/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/Autofac/Autofac.Extras.AttributeMetadata/branch/develop/graph/badge.svg)](https://codecov.io/gh/Autofac/Autofac.Extras.AttributeMetadata) [![Open in Visual Studio Code](https://open.vscode.dev/badges/open-in-vscode.svg)](https://open.vscode.dev/autofac/Autofac.Extras.AttributeMetadata)
[![Build status](https://github.com/autofac/Autofac.Extras.AttributeMetadata/actions/workflows/main.yml/badge.svg)](https://github.com/autofac/Autofac.Extras.AttributeMetadata/actions/workflows/main.yml) [![codecov](https://codecov.io/gh/Autofac/Autofac.Extras.AttributeMetadata/branch/develop/graph/badge.svg)](https://codecov.io/gh/Autofac/Autofac.Extras.AttributeMetadata) [![NuGet](https://img.shields.io/nuget/v/Autofac.Extras.AttributeMetadata.svg)](https://nuget.org/packages/Autofac.Extras.AttributeMetadata)

Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.

- [Documentation](https://autofac.readthedocs.io/en/latest/advanced/metadata.html)
- [NuGet](https://www.nuget.org/packages/Autofac.Extras.AttributeMetadata)
- [Contributing](https://autofac.readthedocs.io/en/latest/contributors.html)
- [Open in Visual Studio Code](https://open.vscode.dev/autofac/Autofac.Extras.AttributeMetadata)

## Quick Start

First, create a metadata attribute - any `System.Attribute` that has MEF's `[MetadataAttribute]` applied. Every publicly readable property becomes one metadata name/value pair, so the attribute below provides `Age` metadata:

```csharp
[MetadataAttribute]
public class AgeMetadataAttribute : Attribute
{
public int Age { get; private set; }

public AgeMetadataAttribute(int age)
{
Age = age;
}
}
```

Apply it to the component implementation rather than to the service interface:

```csharp
public interface IArtwork
{
void Display();
}

[AgeMetadata(100)]
public class CenturyArtwork : IArtwork
{
public void Display() { ... }
}
```

Then register the `AttributedMetadataModule` so the container reads those attributes, and consume the metadata as you would any other:

```csharp
var builder = new ContainerBuilder();
builder.RegisterModule<AttributedMetadataModule>();
builder.RegisterType<CenturyArtwork>().As<IArtwork>();
var container = builder.Build();

var artwork = container.Resolve<IEnumerable<Meta<IArtwork>>>()
.First(a => a.Metadata["Age"].Equals(100));
artwork.Value.Display();
```

[You can read more details in the documentation.](https://autofac.readthedocs.io/en/latest/advanced/metadata.html)

## Get Help

**Need help with Autofac?** We have [a documentation site](https://autofac.readthedocs.io/) as well as [API documentation](https://autofac.org/apidoc/). We're ready to answer your questions on [Stack Overflow](https://stackoverflow.com/questions/tagged/autofac) or check out the [discussion forum](https://groups.google.com/forum/#forum/autofac).
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
codecov:
branch: develop
require_ci_to_pass: yes
require_ci_to_pass: true
coverage:
status:
project:
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"rollForward": "latestFeature",
"version": "10.0.203"
"version": "10.0.302"
}
}
Original file line number Diff line number Diff line change
@@ -1,61 +1,81 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- Assembly metadata -->
<AssemblyName>Autofac.Extras.AttributeMetadata</AssemblyName>
<AssemblyTitle>Autofac.Extras.AttributeMetadata</AssemblyTitle>
<Description>Autofac allows you to register metadata along with dependencies for additional resolution flexibility. This extension enables that metadata to be registered through attributes.</Description>
<Copyright>Copyright © 2015 Autofac Contributors</Copyright>
<Authors>Autofac Contributors</Authors>
<Company>Autofac</Company>
<Product>Autofac</Product>
<AssemblyOriginatorKeyFile>../../Autofac.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<NeutralLanguage>en-US</NeutralLanguage>
<!-- Frameworks and language features -->
<TargetFrameworks>net10.0;net8.0;netstandard2.1;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<NoWarn>$(NoWarn);IDE0008</NoWarn>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Autofac.Extras.AttributeMetadata</AssemblyName>
<AssemblyOriginatorKeyFile>../../Autofac.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<CodeAnalysisRuleSet>../../build/Source.ruleset</CodeAnalysisRuleSet>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Packaging -->
<PackageId>Autofac.Extras.AttributeMetadata</PackageId>
<PackageTags>autofac;di;ioc;dependencyinjection;aspnet;aspnetcore</PackageTags>
<PackageReleaseNotes>Release notes are at https://github.com/autofac/Autofac.Extras.AttributeMetadata/releases</PackageReleaseNotes>
<PackageIcon>icon.png</PackageIcon>
<PackageProjectUrl>https://autofac.org</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/autofac/Autofac.Extras.AttributeMetadata</RepositoryUrl>
<AssemblyTitle>Autofac.Extras.AttributeMetadata</AssemblyTitle>
<NeutralLanguage>en-US</NeutralLanguage>
<Copyright>Copyright © 2015 Autofac Contributors</Copyright>
<CodeAnalysisRuleSet>../../build/Source.ruleset</CodeAnalysisRuleSet>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<ContinuousIntegrationBuild Condition="'$(CI)' != '' ">true</ContinuousIntegrationBuild>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedAllSources>true</EmbedAllSources>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Features>$(Features);IOperation</Features>
<Authors>Autofac Contributors</Authors>
<Company>Autofac</Company>
<Product>Autofac</Product>
<ImplicitUsings>enable</ImplicitUsings>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<!-- OmniSharp/VS Code resource generation -->
<CoreCompileDependsOn>PrepareResources;$(CompileDependsOn)</CoreCompileDependsOn>
</PropertyGroup>

<ItemGroup>
<Using Include="System.Diagnostics.CodeAnalysis" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\build\icon.png" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="../../build/stylecop.json" Link="stylecop.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Autofac.Mef" Version="6.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.300" Condition="Exists('$(MSBuildThisFileDirectory)../../.git')">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PackageReference Include="Autofac" Version="9.3.2" />
<PackageReference Include="Autofac.Mef" Version="8.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.400" Condition="Exists('$(MSBuildThisFileDirectory)../../.git')">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.27.0.140913">
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.32.0.713">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.ComponentModel.Composition" Version="10.0.8" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.ComponentModel.Composition" Version="10.0.11" />
</ItemGroup>
<ItemDefinitionGroup>
<EmbeddedResource>
<Generator>MSBuild:Compile</Generator>
<StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
<StronglyTypedFileName>$(IntermediateOutputPath)%(Filename).Designer.cs</StronglyTypedFileName>
<StronglyTypedClassName>%(Filename)</StronglyTypedClassName>
</EmbeddedResource>
</ItemDefinitionGroup>
<ItemGroup>
<EmbeddedResource Update="MetadataModuleResources.resx">
<StronglyTypedNamespace>Autofac.Extras.AttributeMetadata</StronglyTypedNamespace>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName).Test, PublicKey=00240000048000009400000006020000002400005253413100040000010001008728425885ef385e049261b18878327dfaaf0d666dea3bd2b0e4f18b33929ad4e5fbc9087e7eda3c1291d2de579206d9b4292456abffbe8be6c7060b36da0c33b883e3878eaf7c89fddf29e6e27d24588e81e86f3a22dd7b1a296b5f06fbfb500bbd7410faa7213ef4e2ce7622aefc03169b0324bcd30ccfe9ac8204e4960be6</_Parameter1>
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac.Extras.AttributeMetadata/IMetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ public interface IMetadataProvider
/// </summary>
/// <param name="targetType">Target <see cref="Type"/> for which metadata should be retrieved.</param>
/// <returns>Metadata dictionary to merge with existing metadata.</returns>
IDictionary<string, object> GetMetadata(Type targetType);
IDictionary<string, object?> GetMetadata(Type targetType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IMetadataRegistrar<in TInterface, in TMetadata>
/// <summary>
/// Gets the builder used to build an <see cref="IContainer"/> from component registrations.
/// </summary>
ContainerBuilder ContainerBuilder
ContainerBuilder? ContainerBuilder
{
get;
}
Expand Down
Loading
Loading