Lily.DependencyInjection 1.0.1

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

// Install Lily.DependencyInjection as a Cake Tool
#tool nuget:?package=Lily.DependencyInjection&version=1.0.1

Lily.DependencyInjection

=============================

Extends .net dependency injection.

samples

  • IHello.cs

namespace Lily.Module1
{
    public interface IHello
    {
        string SayHello();
    }
}
  • Hello.cs

using Lily.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Lily.Module1
{
    [Scoped]
    public class Hello : IHello
    {
        private readonly Option1 _option1;

        public Hello(IOptionsMonitor<Option1> options)
        {
            _option1 = options.CurrentValue;
        }

        public string SayHello()
        {
            return _option1.Value;
        }
    }
}
  • Option1.cs

namespace Lily.Module1
{
    public class Option1
    {
        public string Value { get; set; } = string.Empty;
    }
}
  • Module1.cs

using Lily.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Lily.Module1
{
    public class Module1 : IModule
    {
        public void Configure(IServiceCollection services, IConfiguration? configuration)
        {
            if (configuration == null)
            {
                services.Configure<Option1>(option => option.Value = "");
            }
            else
            {
                services.Configure<Option1>(configuration.GetSection("Module1:Option1"));
            }
        }
    }
}
  • ApiModule.cs

using Lily.DependencyInjection;

namespace Lily.MyApi
{
    [DependsOn(
        typeof(Module1.Module1),
        typeof(Module2.Module2))]
    public class ApiModule : IModule
    {
        public void Configure(IServiceCollection services, IConfiguration? configuration)
        {
        }
    }
}
  • Program.cs

using Lily.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// load current assembly
builder.Services.Load(typeof(Program).Assembly, builder.Configuration);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

  • TestController.cs

using Lily.Module1;
using Lily.Module2;
using Microsoft.AspNetCore.Mvc;

namespace Lily.MyApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly IHello _hello;
        private readonly IWorld _world;

        public TestController(IHello hello, IWorld world)
        {
            _hello = hello;
            _world = world;
        }

        [HttpGet]
        public async Task<string> Index()
        {
            return await Task.FromResult($"{_hello.SayHello()} {_world.SayWorld()}");
        }
    }
}
  • appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Module1": {
    "Option1": {
      "Value": "hello"
    }
  },
  "Module2": {
    "Option2": {
      "Value": "world"
    }
  }
}
  • Test API

curl -X 'GET' \
  'https://localhost:7012/api/Test' \
  -H 'accept: text/plain'

[Response body]:
hello world
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. 
.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 was computed. 
.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
1.0.1 447 4/13/2022
1.0.0 409 3/29/2022

Enables to replace the inherited attributes and ignore the inherited dependency injection attributes.