aspnet / DependencyInjection

[Archived] Contains common DI abstractions that ASP.NET Core and Entity Framework Core use. Project moved to https://github.com/aspnet/Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: property injection

TimRowe opened this issue · comments

commented

property injection is useful in base class, is there any plan to add this feature?

We are not planning on adding property injection to Microsoft.Extensions.DependencyInjection or making it a requirement for containers to implement it. You can still use other container implementation that supports it (http://docs.autofac.org/en/latest/register/prop-method-injection.html, http://structuremap.github.io/setter-injection/)

@TimRowe I think, you can to use Scrutor

var serviceCollection = new ServiceCollection();
serviceCollection
    .AddTransient<IFirstService, FirstService>()
    .AddTransient<ISecondService, SecondService>()
    .AddTransient<IMyService, MyService>()
    .Decorate<IMyService>((myService, provider) => {
        myService.SecondService = provider.GetService<ISecondService>();
        return myService;
    });

var serviceProvider = serviceCollection.BuildServiceProvider();
var service = serviceProvider.GetService<IMyService>();

Believe me when I say that you should not be using property injection. You are hiding your dependencies and that is a really bad thing to do :) You can use constructor injection in base classes too :)

@seesharper Tasks are different
Why I can't to use property injection, if I can this class?

    [FindBy(Id = "registrationForm")]
    public class RegistrationForm : Form
    {
        [FindBy(Id = "name")]
        public IInput Name { get; set; }

        [FindBy(Id = "lastName")]
        public IInput LastName { get; set; }

        [FindBy(Id = "login")]
        public IInput Login { get; set; }

        [FindBy(Id = "register")]
        public IButton RegisterButton { get; set; }

        [FindBy(Id = "cancel")]
        public IButton CancelButton { get; set; }
    }

This issue was moved to dotnet/aspnetcore#2331