https://github.com/tobysmith568/di-attributes
Super-small and super-simple library for registering classes with the ASP.NET Core IServiceCollection using attributes.
https://github.com/tobysmith568/di-attributes
attribute dependency di injection ioc iservicecollection
Last synced: 10 months ago
JSON representation
Super-small and super-simple library for registering classes with the ASP.NET Core IServiceCollection using attributes.
- Host: GitHub
- URL: https://github.com/tobysmith568/di-attributes
- Owner: tobysmith568
- License: isc
- Created: 2021-09-26T14:24:04.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-08-10T18:00:47.000Z (10 months ago)
- Last Synced: 2025-08-10T20:17:34.248Z (10 months ago)
- Topics: attribute, dependency, di, injection, ioc, iservicecollection
- Language: C#
- Homepage: https://www.nuget.org/packages/DiAttributes
- Size: 188 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# DiAttributes
Super-small and super-simple library for registering classes with the ASP.NET Core `IServiceCollection` using attributes.
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:
```cs
using DiAttributes;
[Scoped]
public class MyService
{ ... }
```
This is the equivalent of having the following in your `Startup.cs`:
```cs
services.Scoped();
```
You can also pass in a type as an argument to register the class against:
```cs
using DiAttributes;
public interface IMyService
{ ... }
[Scoped(typeof(IMyService))]
public class MyService : IMyService
{ ... }
```
This is the equivalent of having the following in your `Startup.cs`:
```cs
services.Scoped();
```
The use of these attributes will require you to add the following line once to your `Startup.cs` file:
```cs
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:
```json
{
"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:
```cs
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:
```cs
services.RegisterDiAttributes(Configuration);
```
## HttpClient
Classes can be registered as HttpClients using the `HttpClient` attribute.
As per the [Microsoft Docs](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0#typed-clients), these classes will be registered as transient.
```cs
using DiAttributes;
[HttpClient]
public class MyHttpClient
{
public MyHttpClient(HttpClient httpClient)
{ ... }
}
```
This is the equivalent of having the following in your `Startup.cs`:
```cs
services.AddHttpClient();
```
You can also pass in a type as an argument to register the class against:
```cs
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`:
```cs
services.AddHttpClient();
```
## Licence
DiAttributes is licensed under the [ISC License](https://github.com/tobysmith568/di-attributes/blob/main/LICENSE.md).