FluentDI 1.0.0

dotnet add package FluentDI --version 1.0.0
                    
NuGet\Install-Package FluentDI -Version 1.0.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="FluentDI" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentDI" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FluentDI" />
                    
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 FluentDI --version 1.0.0
                    
#r "nuget: FluentDI, 1.0.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.
#:package FluentDI@1.0.0
                    
#: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=FluentDI&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FluentDI&version=1.0.0
                    
Install as a Cake Tool
______  _                      _   ______  _____ 
|  ___|| |                    | |  |  _  \|_   _|
| |_   | | _   _   ___  _ __  | |_ | | | |  | |  
|  _|  | || | | | / _ \| '_ \ | __|| | | |  | |  
| |    | || |_| ||  __/| | | || |_ | |/ /  _| |_ 
\_|    |_| \__,_| \___||_| |_| \__||___/   \___/ 

Static Badge Static Badge


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 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.0.0 109 6/8/2024