FluentDI 1.0.0
dotnet add package FluentDI --version 1.0.0
NuGet\Install-Package FluentDI -Version 1.0.0
<PackageReference Include="FluentDI" Version="1.0.0" />
<PackageVersion Include="FluentDI" Version="1.0.0" />
<PackageReference Include="FluentDI" />
paket add FluentDI --version 1.0.0
#r "nuget: FluentDI, 1.0.0"
#:package FluentDI@1.0.0
#addin nuget:?package=FluentDI&version=1.0.0
#tool nuget:?package=FluentDI&version=1.0.0
______ _ _ ______ _____
| ___|| | | | | _ \|_ _|
| |_ | | _ _ ___ _ __ | |_ | | | | | |
| _| | || | | | / _ \| '_ \ | __|| | | | | |
| | | || |_| || __/| | | || |_ | |/ / _| |_
\_| |_| \__,_| \___||_| |_| \__||___/ \___/
What is FluentDI
FluentDI is an utility library that allows for Dependency Injection through Attributes, making code cleaner and easier to find whether a service is being injected or not.
How does it work?
First of all, after installing the package, FluentDI must be enabled during app building.
using FluentDI;
WebApplication Application;
var builder = WebApplication.CreateBuilder(args);
//Add fluent Dependency Injection
builder.AddFluentDI();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
FluentDI is compatible with regular Dependency Injection
Once enabled, in order to load the services, the Attribute Injector
must be added to the desired services
[Injector<ITimeOfDayService>]
public class EveningOfDay : ITimeOfDayService
{}
If no value is provided, default lifetime is Scoped
, this can be modified
[Injector<IStatusService>(ServiceLifetime.Singleton)]
public class StatusService : IStatusService
{}
---
[Injector<TimeService>(ServiceLifetime.Transient)]
public class TimeService
{}
Pretty simple right? 😃
Conditional Injection
Additionally, this library allows for injection of multiple implementations of a same interface, and decide which one to load based on a condition, supplied by a class implementation
public class TimeOfDaySelector : IRuntimeDependencySelector<ITimeOfDayService>
{
public Func<ITimeOfDayService, object[], bool> CanInject => (s, param) =>
{
if (param.Length == 0 || param[0] is not DateTime date)
throw new ArgumentException("Param must be a date");
var time = TimeOnly.FromDateTime(date);
return time >= s.From && time <= s.To;
};
}
Then add the Injector
attribute to all implementations
[Injector<ITimeOfDayService>]
public class MorningOfDayService : ITimeOfDayService
{
public TimeOnly From => new TimeOnly(8, 0, 0);
public TimeOnly To => new TimeOnly(12, 0, 0);
public string GetTimeOfDay()
{
return "It's daytime!";
}
}
[Injector<ITimeOfDayService>]
public class NightOfDay : ITimeOfDayService
{
public TimeOnly From => new TimeOnly(18, 0, 0);
public TimeOnly To => new TimeOnly(23, 59, 0);
public string GetTimeOfDay()
{
return "It's nighttime!";
}
}
Lastly, when requesting for the service, it must be done through DependencyFactory<>
class
public TimeOfDayController(IDependencyFactory<ITimeOfDayService> dependencyFactory)
{
this.TimeOfDayService = dependencyFactory.Get(DateTime.Now);
}
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 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.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.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 109 | 6/8/2024 |