Skip to content

0xF6/Ivy.Extensions.DependencyInjection.LightProps

Repository files navigation

Microsoft Dependency Property Injection

Release Nuget Telegram

Default Usage

using Microsoft.Extensions.DependencyInjection;

public class SampleClass 
{
    [Inject]
    public ILogger<SampleClass> logger { get; set; }

    public SampleClass(IServiceProvider provider) // need IServiceProvider for auto resolve props
        => provider.InjectFor(this);

}

// in DI configure


services.AddScoped<SampleClass>(); // or etc lifetime

Mvc usage or etc

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;

public abstract class MyGreatSuperWebController : Controller
{
    protected MyGreatSuperWebController(IServiceProvider provider)
        => provider.InjectFor(this);
}


// in other controllers


public class WeatherController : MyGreatSuperWebController
{
    public WeatherController(IServiceProvider provider) : base(provder) {}
    
    [Inject] protected ILogger<WeatherController> logger { get; set; }
    [Inject] protected myBeautifulDbContext { get; set; }
    
    
    public async Task<IActionResult> ViewWeather([FromQuery] string location)
    {
        logger.LogInformation($"call ViewWeather with location '{location}' 💫!!1");
        return View(await myBeautifulDbContext.Set<Weather>().ToListAsync());
    }
}

Install

Install-Package Ivy.Extensions.DependencyInjection.LightProps