Campsis.AutoInject 0.1.0

dotnet add package Campsis.AutoInject --version 0.1.0                
NuGet\Install-Package Campsis.AutoInject -Version 0.1.0                
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="Campsis.AutoInject" Version="0.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Campsis.AutoInject --version 0.1.0                
#r "nuget: Campsis.AutoInject, 0.1.0"                
#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 Campsis.AutoInject as a Cake Addin
#addin nuget:?package=Campsis.AutoInject&version=0.1.0

// Install Campsis.AutoInject as a Cake Tool
#tool nuget:?package=Campsis.AutoInject&version=0.1.0                

AutoInject - Simplified Dependency Injection for .NET

AutoInject is a lightweight and efficient dependency injection automation library for .NET. It eliminates the need for manual service registration by automatically discovering and injecting services using custom attributes.

🚀 Features

  • Automatic Service Registration: Registers Singleton, Scoped, and Transient services dynamically.
  • Supports Keyed Dependencies: Inject services based on custom keys.
  • Minimal Configuration: Just add [Singleton], [Scoped], or [Transient] attributes.
  • Seamless Integration: Works with .NET's built-in IServiceCollection.

📦 Installation

.NET CLI

dotnet add package AutoInject

Or Package Manager Console:

Install-Package AutoInject

🔧 Usage

1️⃣ Add AutoInject to Your DI Container

Call UseAutoInjection() in Program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.UseAutoInjection();

var app = builder.Build();
app.Run();

Or specify a target assembly:

builder.Services.UseAutoInjection(typeof(MyService).Assembly);

2️⃣ Annotate Services with Attributes

Define your services with [Singleton], [Scoped], or [Transient] attributes:

[Singleton(typeof(IMyService))]
public class MyService : IMyService { }

[Scoped(typeof(IRepository))]
public class Repository : IRepository { }

[Transient(typeof(ILogger))]
public class Logger : ILogger { }

3️⃣ Keyed Injection

AutoInject supports keyed dependency injection:

[Singleton(typeof(IStorageBroker), "SQL")]
    public class SqlStorageBroker : IStorageBroker
    {
        public async ValueTask<string> InsertStudentAsync(string student)
        {
            await Task.Delay(10); // Simulate async operation
            return $"Student {student} inserted sql.";
        }
    }

    [Singleton(typeof(IStorageBroker), "MONGO")]
    public class MongoStorageBroker : IStorageBroker
    {
        public async ValueTask<string> InsertStudentAsync(string student)
        {
            await Task.Delay(10); // Simulate async operation
            return $"Student {student} inserted to mongo.";
        }
    }

Then resolve the service by key:

var cache = serviceProvider.GetRequiredKeyedService<ICacheService>("Redis");

Or consume the services in other classes:

[Singleton(typeof(IStudentService))]
public class StudentService(
    [FromKeyedServices("SQL")] IStorageBroker sqlStorageBroker,
    [FromKeyedServices("MONGO")] IStorageBroker mongoStorageBroker) : IStudentService
{
    public ValueTask<string> InsertStudentIntoMongoAsync(string student) =>
        mongoStorageBroker.InsertStudentAsync(student);

    public ValueTask<string> InsertStudentIntoSQLAsync(string student) =>
        sqlStorageBroker.InsertStudentAsync(student);
}

🔍 How It Works

  1. Scans the assembly for classes with [Singleton], [Scoped], or [Transient] attributes.
  2. Registers services in the DI container with their appropriate lifetimes.
  3. Supports keyed registrations if a WithKey parameter is provided.

📌 Notes

  • By default, UseAutoInjection() scans the calling assembly.
  • Use UseAutoInjection(Assembly assembly) to target a specific assembly.
  • Make sure your service interfaces are correctly referenced in attributes.

📄 License

AutoInject is licensed under the MIT License.


👥 Contributing

We welcome contributions! Feel free to submit issues or pull requests.


💬 Need Help?

  • GitHub Issues: Report bugs and feature requests.
  • Discussions: Share ideas and improvements.

Happy coding! 🚀

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
0.1.0 31 1/29/2025

First release of AutoInject.