AutoWire.DI
1.2.1
dotnet add package AutoWire.DI --version 1.2.1
NuGet\Install-Package AutoWire.DI -Version 1.2.1
<PackageReference Include="AutoWire.DI" Version="1.2.1" />
<PackageVersion Include="AutoWire.DI" Version="1.2.1" />
<PackageReference Include="AutoWire.DI" />
paket add AutoWire.DI --version 1.2.1
#r "nuget: AutoWire.DI, 1.2.1"
#addin nuget:?package=AutoWire.DI&version=1.2.1
#tool nuget:?package=AutoWire.DI&version=1.2.1
AutoWire.DI
AutoWire.DI is a powerful, lightweight dependency injection (DI) library for .NET applications featuring automatic registration capabilities. This library simplifies the process of injecting dependencies into your classes by automating service registration, allowing you to focus more on your application logic without the boilerplate associated with manual DI setup.
Key Features
- Automatic Service Registration: Effortlessly register services with minimal configuration using the
[AutoInject]
attribute. - Flexible Service Lifetimes: Support for
Singleton
,Scoped
, andTransient
lifetimes. - Keyed Service Registration: Introduced support for
[KeyedAutoInject]
attribute, allowing services to be registered with keys derived from the names of their classes. More details on this feature can be found in the GitHub Wiki. - No Manual Configuration: Forget about manually adding each service to your container—just decorate your classes.
- Error-Free Registration: Built-in exception handling for duplicate and ambiguous registrations, ensuring a smooth DI experience.
- Support for .NET 8.0 and 9.0: Fully compatible with the latest versions of .NET.
Getting Started
Installation
To install AutoWire.DI
into your project, run the following command in your terminal or package manager console:
dotnet add package AutoWire.DI
Simple Usage
Define Your Service Class Simply add the
[AutoInject]
attribute to your class, and it will be automatically registered in the DI container. You can also specify the service lifetime and service type.using AutoWire.DI.Attributes; [AutoInject("myServiceKey", ServiceLifetime.Singleton)] public class MyService { public void DoSomething() { // Service implementation details Console.WriteLine("MyService is doing something!"); } }
- No Parameters? No Problem!
If no parameters are specified in the
[AutoInject]
attribute, the service is registered based on its implemented interface or class type.
[AutoInject] public class AnotherService { public void Execute() { Console.WriteLine("AnotherService is executing!"); } }
- No Parameters? No Problem!
If no parameters are specified in the
Using Keyed AutoInject You can also use the
[KeyedAutoInject]
attribute to register a service with a key derived from the class name.using AutoWire.DI.Attributes; [KeyedAutoInject] public class KeyedService { public void PerformAction() { Console.WriteLine("KeyedService is performing an action!"); } }
Register Services in Your DI Container In your
Startup.cs
(orProgram.cs
if you're using .NET 6+), call theRegisterAutoInjectableServices
method to automatically scan and register all services decorated with[AutoInject]
and[KeyedAutoInject]
.using AutoWire.DI.Extensions; using Microsoft.Extensions.DependencyInjection; public void ConfigureServices(IServiceCollection services) { services.RegisterAutoInjectableServices(); }
Using the Services Once registered, you can resolve your services through dependency injection:
public class MyController { private readonly MyService _myService; private readonly KeyedService _keyedService; public MyController(MyService myService, KeyedService keyedService) { _myService = myService; _keyedService = keyedService; } public void ExecuteServices() { _myService.DoSomething(); _keyedService.PerformAction(); } }
Exception Handling
- AmbiguousServiceTypeException: This exception is thrown when a class implements multiple interfaces but doesn't specify which one to use. To avoid this, always specify the service type in such cases.
- DuplicateServiceRegistrationException: This exception is thrown if a service is being registered with the same service type and key, leading to a conflict. Ensure that you don't have conflicting registrations.
Why Choose AutoWire.DI?
- Simplify Dependency Injection: Get rid of repetitive DI configuration and make your code cleaner and more maintainable.
- Automatic and Flexible: Works seamlessly with both simple services and more complex configurations.
- Error-Free Registration: With built-in checks for duplicate and ambiguous registrations, you avoid the common pitfalls of manual DI setup.
Documentation
For more in-depth guidance on using AutoWire.DI and advanced configurations, check out the full documentation on our GitHub Wiki.
Contributing
Contributions to AutoWire.DI are welcome! If you have suggestions, bug fixes, or improvements, please submit an issue or a pull request. We appreciate your help in making this project better!
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
Inspired by various dependency injection principles and frameworks in the .NET ecosystem.
Product | Versions 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 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.