Bindicate 1.5.1

dotnet add package Bindicate --version 1.5.1
NuGet\Install-Package Bindicate -Version 1.5.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bindicate" Version="1.5.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Bindicate --version 1.5.1
#r "nuget: Bindicate, 1.5.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Bindicate as a Cake Addin
#addin nuget:?package=Bindicate&version=1.5.1

// Install Bindicate as a Cake Tool
#tool nuget:?package=Bindicate&version=1.5.1

Bindicate ๐Ÿงท

'A blend of "Bind" and "Indicate"'.

NuGet NuGet

Features ๐ŸŒŸ

  • Automatic registration of services using custom attributes.
  • Automatic registration and configuration of options via IOptions<T>.
  • Provides clear visibility and reduces boilerplate code.
  • Simple integration with the built-in .NET IoC container.

Supported types

<center>

| Type | Available | Keyed (.NET 8) | |--------------------|----------|------------------------------| |AddTransient |โœ”๏ธ |โœ”๏ธ | |TryAddTransient |โœ”๏ธ |โŒ | |AddScoped |โœ”๏ธ |โœ”๏ธ | |TryAddScoped |โœ”๏ธ |โŒ | |AddSingleton |โœ”๏ธ |โœ”๏ธ | |TryAddSingleton |โœ”๏ธ |โŒ | |TryAddEnumerable |โŒ |โŒ | </center>

Installation ๐Ÿ“ฆ

Via NuGet

Install-Package Bindicate

or

dotnet add package Bindicate

Usage

You can check out the example project too!

Autowire dependencies

Register Services

Add this line in a project to register all decorated services. You can repeat this line and pass any assembly. To also configure options, use .WithOptions(). You can also use the ServiceCollectionExtension pattern and use IConfiguration as a parameters for your extension method if they have options to register.

Example in host project

// Register all decorated services in the current project
builder.Services
    .AddAutowiringForAssembly(Assembly.GetExecutingAssembly())
    .Register();

// Also register Keyed Services (.NET 8)
builder.Services
    .AddAutowiringForAssembly(Assembly.GetExecutingAssembly())
    .ForKeyedServices()
    .Register();

// Also register Options as IOptions<T>
builder.Services
    .AddAutowiringForAssembly(Assembly.GetExecutingAssembly())
    .ForKeyedServices()
    .WithOptions(Configuration)  //Pass builder.Configuration here
    .Register();

// Register types and options from referenced project
builder.Services
    .AddAutowiringForAssembly(Assembly.GetAssembly(typeof(IInterface)))
    .WithOptions(Configuration)
    .Register();

Example with ServiceCollectionExtensions

// Hosting project:
var configuration = builder.Configuration;

builder.Services.AddSecondProject(configuration);

// In other project
public static IServiceCollection AddSecondProject(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddAutowiringForAssembly(Assembly.GetExecutingAssembly())
                .WithOptions(configuration)
                .Register();

        return services;
    }

Decorate your services:

Basic usage

For class-only registrations:

Simple decorate your class with the attribute to register. You can use an attribute for a specific lifetime.

[AddTransient]
public class SimpleTaskRunner
{
    public void RunTask()
    {
        // ...
    }
}

[TryAddSingleton]
public class SimpleService
{
    public void DoThing()
    {
        // ...
    }
}

When using interfaces:

Decorate your class with the attribute and provide the interface

[AddScoped(typeof(IMyTaskRunner))]
public class TaskRunner : IMyTaskRunner
{
    public void Run()
    {
        // ...
    }
}

public interface IMyTaskRunner
{
    void Run();
}

When using keyed services:

Decorate your class with the attribute and provide the key

[AddScoped("myKey")]
public class KeyedService
{
	public void Run()
	{
		// ...
	}
}

[AddScoped("key", typeof(IKeyedService))]
public class KeyedService : IKeyedService
{
	public void Run()
	{
		// ...
	}
}

[AddScoped("anotherKey")]
public class AnotherKeyedService : IKeyedService
{
	public void Run()
	{
		// ...
	}
}

Options Registration

Decorate your class containing the options with [RegisterOptions] and specify the corresponding section in appsettings.json.

[RegisterOptions("testOptions")]
public class TestOptions
{
    public string Test { get; set; } = "";
}

//appsettings.json:
{
  "testOptions": {
    "test": "test"
  }
}

Now you can use this value when injecting IOptions<TestOptions> in your service

Generics

Define a generic interface:

Decorate the generic interface with the [RegisterGenericInterface] attribute.

[RegisterGenericInterface]
public interface IRepository<T> where T : BaseEntity
{
    void add(T entity);
}

Create the implementation:

[AddTransient(typeof(IRepository<>))]
public class Repository<T> : IRepository<T> where T : BaseEntity
{
    public Repository()
    {
    }

    public void add(T entity)
    {
        // Implementation here
    }
}

How to use

You can now resolve instances of this type from IServiceProvider

var customerRepo = serviceProvider.GetService<IRepository<Customer>>();
var productRepo = serviceProvider.GetService<IRepository<Product>>();

Both customerRepo and productRepo will be instances of Repository<T> but will operate on Customer and Product types, respectively.

License

This project is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.5.1 218 1/3/2024
1.5.0 109 1/2/2024
1.3.0 117 12/21/2023
1.2.0 300 10/23/2023
1.1.9 129 10/18/2023
1.1.8 100 10/16/2023
1.1.7 86 10/16/2023
1.1.6 106 10/11/2023
1.1.5 120 10/10/2023
1.1.0 106 10/9/2023
1.0.0 107 10/9/2023

Add support for IOptions