Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: about 1 month 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 (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-01T18:13:43.000Z (about 1 month ago)
- Last Synced: 2024-10-04T13:18:04.764Z (about 1 month ago)
- Topics: attribute, dependency, di, injection, ioc, iservicecollection
- Language: C#
- Homepage: https://www.nuget.org/packages/DiAttributes
- Size: 148 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
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);
```
## HttpClientClasses 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).