DiAttributes 1.2.0
dotnet add package DiAttributes --version 1.2.0
NuGet\Install-Package DiAttributes -Version 1.2.0
<PackageReference Include="DiAttributes" Version="1.2.0" />
paket add DiAttributes --version 1.2.0
#r "nuget: DiAttributes, 1.2.0"
// Install DiAttributes as a Cake Addin #addin nuget:?package=DiAttributes&version=1.2.0 // Install DiAttributes as a Cake Tool #tool nuget:?package=DiAttributes&version=1.2.0
DiAttributes
Super-small and super-simple library for registering classes with the ASP.NET Core IServiceCollection
using attributes.
<a href="https://www.nuget.org/packages/DiAttributes"> <img alt="NuGet" src="https://img.shields.io/nuget/v/DiAttributes?logo=nuget"> </a> <a href="https://codecov.io/gh/tobysmith568/di-attributes"> <img alt="CodeCov" src="https://codecov.io/gh/tobysmith568/di-attributes/branch/main/graph/badge.svg"/> </a>
GitHub: https://github.com/tobysmith568/di-attributes
NuGet: https://www.nuget.org/packages/DiAttributes
Scoped, Transient, and Singleton
Classes can be registered as any of these three types of dependency via the respective attributes:
using DiAttributes;
[Scoped]
public class MyService
{ ... }
This is the equivalent of having the following in your Startup.cs
:
services.Scoped<MyService>();
You can also pass in a type as an argument to register the class against:
using DiAttributes;
public interface IMyService
{ ... }
[Scoped(typeof(IMyService))]
public class MyService : IMyService
{ ... }
This is the equivalent of having the following in your Startup.cs
:
services.Scoped<IMyService, MyService>();
The use of these attributes will require you to add the following line once to your Startup.cs
file:
services.RegisterDiAttributes();
Configuration
Classes can be automatically bound to sections of your app's configuration using the [Configuration]
attribute.
If your appsettings.json
looks like this:
{
"Outer": {
"Inner": {
"MySetting": "My Value"
}
}
}
Then you can bind a class to the Inner
object (for example) and register it with the IServiceCollection
like this:
using DiAttributes;
[Configuration("Outer:Inner")]
public class MyInnerOptions
{
public string MySetting { get; set; }
}
To use this attribute you will need to pass an ICollection
instance to the RegisterDiAttributes
call in your Startup.cs
file:
services.RegisterDiAttributes(Configuration);
HttpClient
Classes can be registered as HttpClients using the HttpClient
attribute.
As per the Microsoft Docs, these classes will be registered as transient.
using DiAttributes;
[HttpClient]
public class MyHttpClient
{
public MyHttpClient(HttpClient httpClient)
{ ... }
}
This is the equivalent of having the following in your Startup.cs
:
services.AddHttpClient<MyHttpClient>();
You can also pass in a type as an argument to register the class against:
public interface IMyHttpClient
{ ... }
[Scoped(typeof(IMyHttpClient))]
public class MyHttpClient : IMyHttpClient
{
public MyHttpClient(HttpClient httpClient)
{ ... }
}
This is the equivalent of having the following in your Startup.cs
:
services.AddHttpClient<IMyHttpClient, MyHttpClient>();
Licence
DiAttributes is licensed under the ISC License.
<a href="https://app.fossa.com/projects/custom%2B29651%2Fgithub.com%2Ftobysmith568%2Fdi-attributes?ref=badge_large" alt="FOSSA Status"> <img alt="FOSSA Report" src="https://app.fossa.com/api/projects/custom%2B29651%2Fgithub.com%2Ftobysmith568%2Fdi-attributes.svg?type=large"/> </a>
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.Configuration.Abstractions (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Http (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.