Bindicate 1.6.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Bindicate --version 1.6.1
                    
NuGet\Install-Package Bindicate -Version 1.6.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.6.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bindicate" Version="1.6.1" />
                    
Directory.Packages.props
<PackageReference Include="Bindicate" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bindicate --version 1.6.1
                    
#r "nuget: Bindicate, 1.6.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.
#:package Bindicate@1.6.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bindicate&version=1.6.1
                    
Install as a Cake Addin
#tool nuget:?package=Bindicate&version=1.6.1
                    
Install as a Cake Tool

image

'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.
  • Supports Keyed Services

Supported types

<center>

| Type | Available | Keyed (.NET 8) | Decorators |Interceptors | Activators | |--------------------|----------|------------------------------|---------|------------|------------| |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();

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

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

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

[AddKeyedScoped("anotherKey", typeof(IKeyedService))]
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.

Decorators

Bindicate allows you to register decorators using attributes. Decorators wrap existing services to add additional behavior while preserving the original service's interface.

Defining a decorator:

Decorate your class with the [RegisterDecorator] attribute, specifying the service type it decorates.

[RegisterDecorator(typeof(IMyService))]
public class MyServiceDecorator : IMyService
{
    private readonly IMyService _innerService;

    public MyServiceDecorator(IMyService innerService)
    {
        _innerService = innerService;
    }

    public void DoSomething()
    {
        // Add pre-processing logic here

        _innerService.DoSomething();

        // Add post-processing logic here
    }
}
Registering decorators:

Decorators are automatically applied when you call .Register() in your service registration.

Order of decorators:

If you have multiple decorators for the same service, you can specify the order in which they are applied using the Order parameter.

[RegisterDecorator(typeof(IMyService), Order = 1)]
public class FirstDecorator : IMyService
{
    // ...
}

[RegisterDecorator(typeof(IMyService), Order = 2)]
public class SecondDecorator : IMyService
{
    // ...
}

Decorators with lower Order values are applied first. In this example, FirstDecorator will wrap MyService, and SecondDecorator will wrap FirstDecorator.

Using the decorated service:

When you resolve IMyService from the service provider, you will get the outermost decorator.

var myService = serviceProvider.GetRequiredService<IMyService>();
myService.DoSomething();

Example with a logging decorator:

[RegisterDecorator(typeof(IMyService))]
public class LoggingDecorator : IMyService
{
    private readonly IMyService _innerService;

    public LoggingDecorator(IMyService innerService)
    {
        _innerService = innerService;
    }

    public void DoSomething()
    {
        Console.WriteLine("Before DoSomething");
        _innerService.DoSomething();
        Console.WriteLine("After DoSomething");
    }
}

Decorators with generics:

You can also create decorators for generic services.

[RegisterDecorator(typeof(IRepository<>))]
public class RepositoryLoggingDecorator<T> : IRepository<T> where T : BaseEntity
{
    private readonly IRepository<T> _innerRepository;

    public RepositoryLoggingDecorator(IRepository<T> innerRepository)
    {
        _innerRepository = innerRepository;
    }

    public void Add(T entity)
    {
        Console.WriteLine($"Adding entity of type {typeof(T).Name}");
        _innerRepository.Add(entity);
        Console.WriteLine($"Added entity of type {typeof(T).Name}");
    }
}

Now, when you resolve IRepository<Customer> or IRepository<Product>, the RepositoryLoggingDecorator<T> will be applied.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.7.1 264 2/17/2025
1.7.0 222 10/15/2024
1.6.1 2,285 10/11/2024
1.5.1 1,785 1/3/2024
1.5.0 252 1/2/2024
1.3.0 253 12/21/2023
1.2.0 435 10/23/2023
1.1.9 246 10/18/2023
1.1.8 221 10/16/2023
1.1.7 200 10/16/2023
1.1.6 223 10/11/2023
1.1.5 233 10/10/2023
1.1.0 220 10/9/2023
1.0.0 220 10/9/2023

Add support for Decorators