Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

Awesome Lists containing this project

README

        

# DiAttributes

Super-small and super-simple library for registering classes with the ASP.NET Core `IServiceCollection` using attributes.


NuGet


CodeCov

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).